database driven photo gallery with upload

B

bob garbados

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>
 
S

Steve C. Orr [MVP, MCSD]

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)NewHeight,myCallback,IntPtr.Zero);

return myShrunkenImage;

}

public bool ThumbnailCallback(){return false;}
 
B

bob garbados

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 said:
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)NewHeight,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 said:
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>
 
B

bob garbados

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 said:
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)NewHeight,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 said:
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>
 
B

bob garbados

I lied... removed the response.write and everything works beautifully.


bob garbados said:
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 said:
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)NewHeight,myCallback,IntPtr.Zero);
return myShrunkenImage;

}

public bool ThumbnailCallback(){return false;}


--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
fsoUploadFile.PostedFile.SaveAs("C:\FasterSolutions\Clients\spiritmt\www\pho
 
B

bob garbados

It's Friday and I've worked too long this week, 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 said:
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)NewHeight,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 said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top