Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

save uploaded image thumbnail to sql db image field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dodgeyb - 14 Dec 2007 06:03 GMT
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.PostedFile.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.ConfigurationManager.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")
George Ter-Saakov - 14 Dec 2007 16:29 GMT
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

> Hi there,
> Trying to allow client to upload image, thumbnail it, and save it into
[quoted text clipped - 41 lines]
>        cnn.Dispose()
>        Response.Write("done")

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.