0

I am trying to take a database binary entry where images are stored and convert it back to display in a PictureBox on a Windows Form. I have looked around and tried to piece a couple methods together however i get no errors and also no image in the PictureBox when trying to use the below code.

Dim MyByte = varReader("BinaryData")
Dim MyImg As Image

If MyByte IsNot Nothing Then
   MyImg = bytesToImage(MyByte)
   PictureBox1.Image = MyImg
End If

Public Function bytesToImage(ByVal byteArrayIn As Byte()) As Image
    Dim ms As MemoryStream = New MemoryStream(byteArrayIn)
    Dim returnImage As Image = Image.FromStream(ms)
    Return returnImage
End Function

I know the Binary data is good as i have a different piece of code that has created it in the first place to store in the DB and the image displays fine on the Website which uses this data.

Any help or advice where i have gone wrong would be much appreciated!!

Many Thanks

EDITED::

The database column holding the information is varbinary(MAX)

This is the code i use to convert the image to the binary (I won't include the DB update part as this is just a basic update to the DB Column)

Dim varPictureBinary As Byte()

Dim filePath As String = "ImageFilePath"

Dim fStream As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)

Dim br As BinaryReader = New BinaryReader(fStream)

Dim fileInfo As FileInfo = New FileInfo(filePath)

varPictureBinary = br.ReadBytes(fileInfo.Length)

I then want to take a binary entry from this column and turn it back to an image and display it in a picture box in my program.

  • The following may be helpful: https://stackoverflow.com/a/70307947/10024425 and https://stackoverflow.com/a/66616751/10024425 – Tu deschizi eu inchid Nov 15 '22 at 21:45
  • What Web Site? If the data is stored correctly, you can show those images anywhere. Post the code that's storing he images and the definition of the db Column-- What is the content of `MyByte` when you get the data back? You should test `DbNull.Value` -- If there's actually some content, can you post the first 12 bytes? – Jimi Nov 15 '22 at 22:01
  • @Jimi I have added some extra details to the initial post with how i create the Binary to store in the DB. I get 137 80 78 71 13 10 26 10 0 0 0 13 73 from reading the first 12 bytes. – Martin Bullock Nov 16 '22 at 08:50
  • Your code performs highly redundant operations (aka, bloated). To get the image bytes you need `Dim varPictureBinary = File.ReadAllBytes("ImageFilePath")`, to retrieve the image, you need `Dim someImage = DirectCast(New ImageConverter().ConvertFrom(MyByte), Image)`, that's all -- I see you have a PNG image there, the signature is not corrupted, so the code to assign the Image to PictureBox is `PictureBox1.Image?.Dispose() PictureBox1.Image = DirectCast(New ImageConverter().ConvertFrom(MyByte), Image) PictureBox1.Refresh()` – Jimi Nov 16 '22 at 09:29
  • If you still don't see the image, try to save it to disk first (`File.WriteAllBytes("[Image path].png"), MyByte)`) and use the default image viewer to open it. If you can now see it, then there's something else in your code that you haven't shown (e.g., often people try to assign a Control's property from a Thread other than the UI Thread), so you need to give context to the operations and also show the database storing procedure – Jimi Nov 16 '22 at 09:29
  • @Jimi Thanks for your help!! Excellent piece of code for Image to Binary, i found the code i was using 10 years ago when i was first starting. The Binary to Image is now working too so thanks for that!! It throws an "Object reference not set to an instance" when using the dispose for the first time so i will look at that. Thanks for your help and striping the code right down!! – Martin Bullock Nov 16 '22 at 11:53
  • Note that I've written `PictureBox1.Image?.Dispose()`, not `PictureBox1.Image.Dispose()`, note the Null-Conditional Operator (*Elvis*): `?.` -- If you have an old VB.Net version that doesn't support it, you have to write `If PictureBox1.Image IsNot Nothing then PictureBox1.Image.Dispose()` – Jimi Nov 16 '22 at 11:57

0 Answers0