Home > Archive > ASP .NET > October 2004 > database driven photo gallery with upload
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
database driven photo gallery with upload
|
|
| bob garbados 2004-10-29, 3:58 pm |
| I am trying to create a database-driven photo gallery for a friend with an
admin form to upload images... I can upload a file to the web server, but I
want to store the image in a database and I want to resize the image before
I save it... How do I take the uploaded .jpg and shrink it to a thumbnail?
How do I pass the uploaded .jpg to a stored procedure that will store the
image as an image datatype in SQL Server 2000? I'm developing this without
Visual Studio.
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
End Sub
Sub Button_Click(S as Object, E as EventArgs)
fsoUploadFile.PostedFile.SaveAs(" C:\FasterSolutions\Clients\spiritmt\www\
pho
togallery\NewFile.jpg")
End Sub
</script>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<form id="frmUpload" method="post" runat="server"
enctype="multipart/form-data">
<input type="file" id="fsoUploadFile" runat="server"><br/>
<asp:button Text="Upload File" OnClick="Button_Click" runat="server"/>
</form>
</body>
</html>
| |
| Steve C. Orr [MVP, MCSD] 2004-10-29, 3:58 pm |
| Here's an article I wrote that describes how to upload images into a
database and get them back out again.
http://steve.orr.net/content/asp200307so_f.asp
Also, here's an image resize routine I wrote:
/*shrink the image proportionately so that neither height nor width is more
than [NewSize] pixels*/
public Image ShrinkImage(Bitmap bmp, int NewSize)
{
double NewWidth;
double NewHeight;
double ShrinkPercent;
System.Drawing.Image.GetThumbnailImageAbort myCallback =
new System.Drawing.Image. GetThumbnailImageAbort(ThumbnailCallback
);
if (bmp.Width>bmp.Height)
{
NewWidth=NewSize;
ShrinkPercent=(NewWidth/bmp.Width)*100;
NewHeight=(ShrinkPercent/100)*bmp.Height;
}
else
{
NewHeight=NewSize;
ShrinkPercent=(NewHeight/bmp.Height)*100;
NewWidth=(ShrinkPercent/100)*bmp.Width;
}
System.Drawing.Image myShrunkenImage =
bmp. GetThumbnailImage((int)NewWidth,(int)New
Height,myCallback,IntPtr.Zero);
return myShrunkenImage;
}
public bool ThumbnailCallback(){return false;}
--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"bob garbados" <bobgarbados.Quit.Spamming.Me@yahoo.com> wrote in message
news:10o4rbj5hhjfbd2@corp.supernews.com...
>I am trying to create a database-driven photo gallery for a friend with an
> admin form to upload images... I can upload a file to the web server, but
> I
> want to store the image in a database and I want to resize the image
> before
> I save it... How do I take the uploaded .jpg and shrink it to a thumbnail?
> How do I pass the uploaded .jpg to a stored procedure that will store the
> image as an image datatype in SQL Server 2000? I'm developing this
> without
> Visual Studio.
>
> <%@ Page Language="VB" %>
>
> <script language="VB" runat="server">
>
> Sub Page_Load(Source As Object, E As EventArgs)
> End Sub
>
> Sub Button_Click(S as Object, E as EventArgs)
>
> fsoUploadFile.PostedFile.SaveAs(" C:\FasterSolutions\Clients\spiritmt\www\
pho
> togallery\NewFile.jpg")
> End Sub
>
> </script>
> <html>
> <head>
> <title>Image Upload</title>
> </head>
> <body>
> <form id="frmUpload" method="post" runat="server"
> enctype="multipart/form-data">
> <input type="file" id="fsoUploadFile" runat="server"><br/>
> <asp:button Text="Upload File" OnClick="Button_Click" runat="server"/>
> </form>
> </body>
> </html>
>
>
| |
| bob garbados 2004-10-29, 3:58 pm |
| Thanks Steve. I actually found your article and used it as a guide to write
my code for uploading the image to the database. I'm working on retreiving
the images now and the resize routine looks great, I'll implement that once
everything else is working.
"Steve C. Orr [MVP, MCSD]" <Steve@Orr.net> wrote in message
news:u6WgBYdvEHA.200@TK2MSFTNGP11.phx.gbl...
> Here's an article I wrote that describes how to upload images into a
> database and get them back out again.
> http://steve.orr.net/content/asp200307so_f.asp
>
> Also, here's an image resize routine I wrote:
>
> /*shrink the image proportionately so that neither height nor width is
more
> than [NewSize] pixels*/
>
> public Image ShrinkImage(Bitmap bmp, int NewSize)
>
> {
>
> double NewWidth;
>
> double NewHeight;
>
> double ShrinkPercent;
>
> System.Drawing.Image.GetThumbnailImageAbort myCallback =
>
> new System.Drawing.Image. GetThumbnailImageAbort(ThumbnailCallback
);
>
> if (bmp.Width>bmp.Height)
>
> {
>
> NewWidth=NewSize;
>
> ShrinkPercent=(NewWidth/bmp.Width)*100;
>
> NewHeight=(ShrinkPercent/100)*bmp.Height;
>
> }
>
> else
>
> {
>
> NewHeight=NewSize;
>
> ShrinkPercent=(NewHeight/bmp.Height)*100;
>
> NewWidth=(ShrinkPercent/100)*bmp.Width;
>
> }
>
> System.Drawing.Image myShrunkenImage =
>
bmp. GetThumbnailImage((int)NewWidth,(int)New
Height,myCallback,IntPtr.Zero);
>
> return myShrunkenImage;
>
> }
>
> public bool ThumbnailCallback(){return false;}
>
>
> --
> I hope this helps,
> Steve C. Orr, MCSD, MVP
> http://Steve.Orr.net
>
>
>
> "bob garbados" <bobgarbados.Quit.Spamming.Me@yahoo.com> wrote in message
> news:10o4rbj5hhjfbd2@corp.supernews.com...
an[color=darkred]
but[color=darkred]
thumbnail?[color=darkred]
the[color=darkred]
fsoUploadFile.PostedFile.SaveAs(" C:\FasterSolutions\Clients\spiritmt\www\
pho[color=darkred]
>
>
| |
| bob garbados 2004-10-29, 8:56 pm |
| Steve,
I can save the image in the database, but I can't retrieve it. Here's my
code:
Dim con as SqlConnection
Dim strConnectionString as String
strConnectionString = ConfigurationSettings.AppSettings("connection")
con= New SqlConnection(strConnectionString)
Dim dr As System.Data.SqlClient.SqlDataReader
Dim cmdGetPhoto as new SqlCommand("usp_GetPhotos", con)
con.Open()
dr = cmdGetPhoto.ExecuteReader
If dr.Read Then
Response.Write("Photo is here...")
Response.Write("Photo Name: " & dr("PhotoTitle") & "<br/>")
Response.ContentType = dr("PhotoThumbContentType").ToString
Response.OutputStream.Write(CType(dr("PhotoThumb"), Byte()), 0,
CInt(dr("PhotoThumbSize")))
Response.AddHeader("Content-Disposition", dr("PhotoTitle").ToString())
Else
Response.Write("File Not Found.")
End If
'close down the connection
con.Close()
I stepped through the code and it executes the correct lines of code, but
doesn't write anything and the image doesn't show up. What does the
'inherits="CIT.ViewAttachment"' line in your code do?
"Steve C. Orr [MVP, MCSD]" <Steve@Orr.net> wrote in message
news:u6WgBYdvEHA.200@TK2MSFTNGP11.phx.gbl...
> Here's an article I wrote that describes how to upload images into a
> database and get them back out again.
> http://steve.orr.net/content/asp200307so_f.asp
>
> Also, here's an image resize routine I wrote:
>
> /*shrink the image proportionately so that neither height nor width is
more
> than [NewSize] pixels*/
>
> public Image ShrinkImage(Bitmap bmp, int NewSize)
>
> {
>
> double NewWidth;
>
> double NewHeight;
>
> double ShrinkPercent;
>
> System.Drawing.Image.GetThumbnailImageAbort myCallback =
>
> new System.Drawing.Image. GetThumbnailImageAbort(ThumbnailCallback
);
>
> if (bmp.Width>bmp.Height)
>
> {
>
> NewWidth=NewSize;
>
> ShrinkPercent=(NewWidth/bmp.Width)*100;
>
> NewHeight=(ShrinkPercent/100)*bmp.Height;
>
> }
>
> else
>
> {
>
> NewHeight=NewSize;
>
> ShrinkPercent=(NewHeight/bmp.Height)*100;
>
> NewWidth=(ShrinkPercent/100)*bmp.Width;
>
> }
>
> System.Drawing.Image myShrunkenImage =
>
bmp. GetThumbnailImage((int)NewWidth,(int)New
Height,myCallback,IntPtr.Zero);
>
> return myShrunkenImage;
>
> }
>
> public bool ThumbnailCallback(){return false;}
>
>
> --
> I hope this helps,
> Steve C. Orr, MCSD, MVP
> http://Steve.Orr.net
>
>
>
> "bob garbados" <bobgarbados.Quit.Spamming.Me@yahoo.com> wrote in message
> news:10o4rbj5hhjfbd2@corp.supernews.com...
an[color=darkred]
but[color=darkred]
thumbnail?[color=darkred]
the[color=darkred]
fsoUploadFile.PostedFile.SaveAs(" C:\FasterSolutions\Clients\spiritmt\www\
pho[color=darkred]
>
>
| |
| bob garbados 2004-10-29, 8:56 pm |
| I lied... removed the response.write and everything works beautifully.
"bob garbados" <bobgarbados.Quit.Spamming.Me@yahoo.com> wrote in message
news:10o599a3dl8mv6f@corp.supernews.com...
> Steve,
>
> I can save the image in the database, but I can't retrieve it. Here's my
> code:
>
> Dim con as SqlConnection
> Dim strConnectionString as String
>
> strConnectionString = ConfigurationSettings.AppSettings("connection")
>
> con= New SqlConnection(strConnectionString)
>
> Dim dr As System.Data.SqlClient.SqlDataReader
> Dim cmdGetPhoto as new SqlCommand("usp_GetPhotos", con)
>
> con.Open()
> dr = cmdGetPhoto.ExecuteReader
> If dr.Read Then
> Response.Write("Photo is here...")
> Response.Write("Photo Name: " & dr("PhotoTitle") & "<br/>")
> Response.ContentType = dr("PhotoThumbContentType").ToString
> Response.OutputStream.Write(CType(dr("PhotoThumb"), Byte()), 0,
> CInt(dr("PhotoThumbSize")))
> Response.AddHeader("Content-Disposition", dr("PhotoTitle").ToString())
> Else
> Response.Write("File Not Found.")
> End If
>
> 'close down the connection
> con.Close()
>
> I stepped through the code and it executes the correct lines of code, but
> doesn't write anything and the image doesn't show up. What does the
> 'inherits="CIT.ViewAttachment"' line in your code do?
>
>
> "Steve C. Orr [MVP, MCSD]" <Steve@Orr.net> wrote in message
> news:u6WgBYdvEHA.200@TK2MSFTNGP11.phx.gbl...
> more
>
bmp. GetThumbnailImage((int)NewWidth,(int)New
Height,myCallback,IntPtr.Zero);
> an
> but
> thumbnail?
> the
>
fsoUploadFile.PostedFile.SaveAs(" C:\FasterSolutions\Clients\spiritmt\www\
pho
>
>
| |
| bob garbados 2004-10-29, 8:56 pm |
| It's Friday and I've worked too long this w , but I can't figure this one
out... When I try to save the file to the database from my browser pointed
at localhost, it all works fine. When I try to do the same from a remote
machine, it doesn't work. It tries to upload the file from the server's C:\
drive instead of the client's C:\ drive. Any ideas? Is there a setting I'm
missing somewhere?d
"Steve C. Orr [MVP, MCSD]" <Steve@Orr.net> wrote in message
news:u6WgBYdvEHA.200@TK2MSFTNGP11.phx.gbl...
> Here's an article I wrote that describes how to upload images into a
> database and get them back out again.
> http://steve.orr.net/content/asp200307so_f.asp
>
> Also, here's an image resize routine I wrote:
>
> /*shrink the image proportionately so that neither height nor width is
more
> than [NewSize] pixels*/
>
> public Image ShrinkImage(Bitmap bmp, int NewSize)
>
> {
>
> double NewWidth;
>
> double NewHeight;
>
> double ShrinkPercent;
>
> System.Drawing.Image.GetThumbnailImageAbort myCallback =
>
> new System.Drawing.Image. GetThumbnailImageAbort(ThumbnailCallback
);
>
> if (bmp.Width>bmp.Height)
>
> {
>
> NewWidth=NewSize;
>
> ShrinkPercent=(NewWidth/bmp.Width)*100;
>
> NewHeight=(ShrinkPercent/100)*bmp.Height;
>
> }
>
> else
>
> {
>
> NewHeight=NewSize;
>
> ShrinkPercent=(NewHeight/bmp.Height)*100;
>
> NewWidth=(ShrinkPercent/100)*bmp.Width;
>
> }
>
> System.Drawing.Image myShrunkenImage =
>
bmp. GetThumbnailImage((int)NewWidth,(int)New
Height,myCallback,IntPtr.Zero);
>
> return myShrunkenImage;
>
> }
>
> public bool ThumbnailCallback(){return false;}
>
>
> --
> I hope this helps,
> Steve C. Orr, MCSD, MVP
> http://Steve.Orr.net
>
>
>
> "bob garbados" <bobgarbados.Quit.Spamming.Me@yahoo.com> wrote in message
> news:10o4rbj5hhjfbd2@corp.supernews.com...
an[color=darkred]
but[color=darkred]
thumbnail?[color=darkred]
the[color=darkred]
fsoUploadFile.PostedFile.SaveAs(" C:\FasterSolutions\Clients\spiritmt\www\
pho[color=darkred]
>
>
|
|
|
|
|