Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > save uploaded image thumbnail to sql db image field

Reply
Thread Tools

save uploaded image thumbnail to sql db image field

 
 
dodgeyb
Guest
Posts: n/a
 
      12-14-2007
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")
 
Reply With Quote
 
 
 
 
George Ter-Saakov
Guest
Posts: n/a
 
      12-14-2007
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" <> wrote in message
news:b88ce90e-7acb-4f43-9d9c-...
> 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")



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
Displaying a Thumbnail vs. creating a thumbnail David Lozzi ASP .Net 1 02-01-2006 10:34 PM
how to convert httpPostedFile to and Drawing.Image (save a thumbnail) NATO24 ASP .Net 2 03-26-2005 03:52 AM
HowTo Send Uploaded File with INPUT Type file to Sql Server Image Data Type Roberto ASP General 3 10-18-2003 06:56 AM
Virus-Scanning uploaded files uploaded? Matt G ASP .Net 1 08-22-2003 05:44 AM



Advertisments