By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 759 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

database driven photo gallery with upload

P: n/a
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:\FasterSolution s\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>
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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(Thumbn ailCallback);

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" <bo**************************@yahoo.com> wrote in message
news:10*************@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:\FasterSolution s\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>

Nov 18 '05 #2

P: n/a
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]" <St***@Orr.net> wrote in message
news:u6*************@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(Thumbn ailCallback);

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" <bo**************************@yahoo.com> wrote in message
news:10*************@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:\FasterSolution s\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>


Nov 18 '05 #3

P: n/a
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]" <St***@Orr.net> wrote in message
news:u6*************@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(Thumbn ailCallback);

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" <bo**************************@yahoo.com> wrote in message
news:10*************@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:\FasterSolution s\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>


Nov 18 '05 #4

P: n/a
I lied... removed the response.write and everything works beautifully.
"bob garbados" <bo**************************@yahoo.com> wrote in message
news:10*************@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]" <St***@Orr.net> wrote in message
news:u6*************@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(Thumbn ailCallback);

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" <bo**************************@yahoo.com> wrote in message
news:10*************@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:\FasterSolution s\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>



Nov 18 '05 #5

P: n/a
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 [MVP, MCSD]" <St***@Orr.net> wrote in message
news:u6*************@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(Thumbn ailCallback);

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" <bo**************************@yahoo.com> wrote in message
news:10*************@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:\FasterSolution s\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>


Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.