467,146 Members | 1,240 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

save uploaded image thumbnail to sql db image field

Hi there,
Trying to allow client to upload image, thumbnail it, and save it into
sql table image field.
Code compiles & runs but image cannot be retrieved. any clues what I'm
doing wrong pls !
Dim origImage As System.Drawing.Image =
System.Drawing.Image.FromStream(FileUpload1.Posted File.InputStream)

' rewind the input stream to read it again to store the
original image in the database
FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin)

'make thumbnail

Dim thumbnail As System.Drawing.Image = New
System.Drawing.Bitmap(120, 120)
Dim thisFormat = origImage.RawFormat
Dim ms As MemoryStream = New MemoryStream()
thumbnail.Save(ms, thisFormat)
'System.Drawing.Imaging.ImageFormat.Jpeg)
ms.Seek(0, SeekOrigin.Begin)

Dim buffer(ms.Length) As Byte
ms.Read(buffer, 0, buffer.Length)

Dim cnn As New
SqlConnection(System.Configuration.ConfigurationMa nager.ConnectionStrings(1).ConnectionString)
cnn.Open()

Dim cmd As New SqlCommand("UPDATE STAFF SET PIC=@img WHERE
STAFF_ID=26", cnn)

Dim ImageContent(buffer.Length) As Byte
Dim intStatus As Integer
intStatus = ms.Read(ImageContent, 0, buffer.Length)
Dim prmImage As New SqlParameter("@img", SqlDbType.Image)
prmImage.Value = ImageContent
cmd.Parameters.Add(prmImage)

cmd.ExecuteNonQuery()
cmd.Dispose()
cnn.Close()
cnn.Dispose()
Response.Write("done")
Dec 14 '07 #1
  • viewed: 2852
Share:
1 Reply
Your code that puts image into DB looks good except i do not see anywere
where you actually put a thumbnail image into
variable 'thumbnail', you just create empty thumbnail and never make an
actual picture.
but may be you jsut skiped that code.
then probably youe code that reads image from DB is bad

PS: MemoryStream has GetBuffer method..

That would save you creation of ImageContent byte array.

George
"dodgeyb" <ch***@ctlsoft.comwrote in message
news:b8**********************************@s12g2000 prg.googlegroups.com...
Hi there,
Trying to allow client to upload image, thumbnail it, and save it into
sql table image field.
Code compiles & runs but image cannot be retrieved. any clues what I'm
doing wrong pls !
Dim origImage As System.Drawing.Image =
System.Drawing.Image.FromStream(FileUpload1.Posted File.InputStream)

' rewind the input stream to read it again to store the
original image in the database
FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin)

'make thumbnail

Dim thumbnail As System.Drawing.Image = New
System.Drawing.Bitmap(120, 120)
Dim thisFormat = origImage.RawFormat
Dim ms As MemoryStream = New MemoryStream()
thumbnail.Save(ms, thisFormat)
'System.Drawing.Imaging.ImageFormat.Jpeg)
ms.Seek(0, SeekOrigin.Begin)

Dim buffer(ms.Length) As Byte
ms.Read(buffer, 0, buffer.Length)

Dim cnn As New
SqlConnection(System.Configuration.ConfigurationMa nager.ConnectionStrings(1).ConnectionString)
cnn.Open()

Dim cmd As New SqlCommand("UPDATE STAFF SET PIC=@img WHERE
STAFF_ID=26", cnn)

Dim ImageContent(buffer.Length) As Byte
Dim intStatus As Integer
intStatus = ms.Read(ImageContent, 0, buffer.Length)
Dim prmImage As New SqlParameter("@img", SqlDbType.Image)
prmImage.Value = ImageContent
cmd.Parameters.Add(prmImage)

cmd.ExecuteNonQuery()
cmd.Dispose()
cnn.Close()
cnn.Dispose()
Response.Write("done")

Dec 14 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mark | last post: by
9 posts views Thread by Ivan Demkovitch | last post: by
5 posts views Thread by Brian Lowe | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.