Ad:
 
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 2041
  • ASP.NET RSS
Similar Threads
Feb 25th, 2010
0

How to store and retrieve images in mySQL (.net)

Expand Post »
I would like to understand how to retrieve an Image stored in a mySQL database as a longblob. Once retrieved, assign that image asp.net image control.

Heres the code that uploads the pic to the MYSQL database

ASP.NET Syntax (Toggle Plain Text)
  1. If (Not myups.HasFile) Then
  2. Return -1
  3. End If
  4. 'FileName.PostedFile.InputStream()
  5.  
  6. Try
  7. Dim fs As FileStream = Nothing
  8.  
  9. 'Dim img As FileUpload = CType(imgUpload, FileUpload)
  10. Dim imgByte As Byte() = Nothing
  11. 'If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
  12.  
  13. 'To create a PostedFile
  14. Dim File As HttpPostedFile = myups.PostedFile
  15. 'Create byte Array with file len
  16. imgByte = New Byte(File.ContentLength - 1) {}
  17. 'force the control to load data in array
  18. File.InputStream.Read(imgByte, 0, File.ContentLength)

When I pull the raw data from the database how do I convert that raw data back to an value that can be assigned to an aspt.net image.

ASP.NET Syntax (Toggle Plain Text)
  1. Private Function CreateImage(ByRef pPanel As Panel, ByVal RawData As Byte) As Panel
  2. Dim myimage As New Image
  3. myimage.ImageUrl = ?
  4. pPanel.Controls.Add(myimage)
  5. Return pPanel
  6. End Function
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jasystweb is offline Offline
9 posts
since Feb 2010
Feb 25th, 2010
0

Re: How to store and retrieve images in mySQL (.net)

Have a look at codeproject article - http://www.codeproject.com/KB/aspnet/image_asp.aspx
Moderator
Reputation Points: 1818
Solved Threads: 981
Posting Expert
adatapost is offline Offline
5,248 posts
since Oct 2008
Feb 27th, 2010
0

Re: How to store and retrieve images in mySQL (.net)

So in the end what I needed to do was to create a handler that could deal with the byte and write it to memory stream. Below is how I grab the LONGBLOB from MYSQL and tie it to an image control to display it.

Handler:

ASP.NET Syntax (Toggle Plain Text)
  1.  
  2. ''Call this sub from the page load event of your handler page
  3. Private Sub RenderImage(ByVal ID As String)
  4.  
  5. Dim ImgID As Int32
  6. ImgID = Request.QueryString("PicID")
  7.  
  8. Context.Response.ContentType = "image/jpeg"
  9. Dim strm As IO.MemoryStream = GetMemStream(ID)
  10. Dim buffer As Byte() = New Byte(4095) {}
  11. Dim byteSeq As Integer = strm.Read(buffer, 0, 4096)
  12.  
  13. Do While byteSeq > 0
  14. Context.Response.OutputStream.Write(buffer, 0, byteSeq)
  15. byteSeq = strm.Read(buffer, 0, 4096)
  16.  
  17. Loop
  18. Context.Response.BinaryWrite(buffer)
  19. Return
  20.  
  21. End Sub
  22.  
  23.  
  24.  
  25. Public Function GetMemStream(ByVal Imgno As Integer) As IO.Stream
  26. Dim conn As String = ""
  27. Dim connection As New OdbcConnection(conn)
  28. Dim sql As String = "
  29. Dim PicData As Byte
  30. Dim Cmd As New OdbcCommand(sql, connection)
  31.  
  32. Cmd.CommandType = CommandType.Text
  33. Cmd.Parameters.AddWithValue("@ID", empno)
  34. connection.Open()
  35. Dim img As Object
  36. img = Cmd.ExecuteScalar
  37. 'img = Cmd.ExecuteReader
  38.  
  39. Try
  40. Return New IO.MemoryStream(CType(img, Byte()))
  41.  
  42. Catch
  43. Return Nothing
  44. Finally
  45. connection.Close()
  46. End Try
  47. End Function
  48.  

Apply the url of the handler in place of the image url. Picture handler is the name of the empty .aspx page I used as a handler.

ASP.NET Syntax (Toggle Plain Text)
  1. <img src=""PictureHandler.aspx?Width=500&amp;Height=500&amp;IMGID=" ' + the id of your database image.
  2.  

This is how I solved my problem I hope this helps people in the future.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jasystweb is offline Offline
9 posts
since Feb 2010
This thread is solved. Perhaps start a new thread instead?
This thread is more than three months old. Perhaps start a new thread instead?
Message:
Previous Thread in ASP.NET Forum Timeline: How to use Ajax with ASP.net?
Next Thread in ASP.NET Forum Timeline: Restricting a user to save a web page





About Us | Contact Us | Advertise | Acceptable Use Policy
Build Custom RSS Feed


Follow us on Twitter


© 2010 DaniWeb® LLC