0

I have this code in my system which I use to retrieving images from a database by formatted blob. But some database records don't have pictures ([BLOB - 0 B]). Please list code for me to place an error picture in picture box1 when a picture is not available.

Dim bytes() as byte
bytes = (objdr("picture"))

Dim memStream as New MemoryStream(bytes)
PictureBox1.image = Drawing.Image.FromStream(memStream)

I'm using Microsoft Visual Basic 2008

Prestaul
  • 83,552
  • 10
  • 84
  • 84
ieyla
  • 25
  • 2
  • 6

1 Answers1

0

I will try with this code: DirectCast seems to be the correct way to read a Blob field from your database.
Then check if you have something to show or try to get an image from your resources

 ' Get the image from the database.
 bytes = DirectCast(objdr("picture"), Byte()) 
 If (Not bytes Is Nothing) Then
     Dim memStream as New MemoryStream(bytes)  
     PictureBox1.image = Drawing.Image.FromStream(memStream)       
 Else
     PictureBox1.image = My.Resources.<Name_of_res_file>.<Name_of_image_resource>
 End If
Steve
  • 213,761
  • 22
  • 232
  • 286
  • thank alot. but i have another error. i have error 'paramater not valid' at line > PictureBox1.Image = Drawing.Image.FromStream(memStream) – ieyla Apr 04 '12 at 08:56
  • The `parameter not valid` indicates that bytes are not valid to build an image. Perhaps if you show more of your code that connects and load the image (writing image to the database also). [See here](http://stackoverflow.com/questions/629955/parameter-not-valid-exception-loading-system-drawing-image) for an identical problem – Steve Apr 04 '12 at 13:29