Click here to Skip to main content
14,244,275 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All

Hope someone can help...
I am storing images in an SQL database using a VARBINARY column.
It is stored OK and I can retrieve it but I cannot seem to get it to display in a PictureBox control.
When I set the picturebox image it seems to get set OK and I can actually save the picture box image to a file no problem but it will not display it.
Sure it is something obvious but for the life of me I cannot see what is wrong with the code:.

Dim bImage As Byte() = DirectCast(oCmd.ExecuteScalar(), Byte())
If bImage Is Nothing Then
    Return
End If

Dim ms As New IO.MemoryStream
ms.Write(bImage, 0, bImage.Length)
Dim bitmap As New Bitmap(ms)
pictureBox1.Image = bitmap

Dim sf As New SaveFileDialog
If sf.ShowDialog() = DialogResult.OK Then
    MsgBox(sf.FileName)
    pictureBox1.Image.Save(sf.FileName, Imaging.ImageFormat.Jpeg)
End If


As said, the SQL returns data which is then converted to a bitmap.
Everything looks OK when inspecting the objects, i.e. format, height, width etc...
The pictureBox1.Image.Save works perfectly and the original image can be viewed from file explorer just fine.

So... why does the pictureBox1 not show my image?


Thanks

What I have tried:

Checked my code carefully, sure it is OK, googled everything I can think of.
Tried all possible methods to load the image; FromStream / FromFile etc...
Posted
Updated 8-Aug-18 4:49am
Rate this:
Please Sign up or sign in to vote.

Solution 1

You are showing a file selection dialog just after assigning the bitmap which blocks drawing the new content of the picture box control.

If there was no error with the bitmap, the new image should be shown once the file selection dialog is closed and the function where you set the image returns.

To show the image before opening the file dialog, you can force a redraw:
pictureBox1.Image = bitmap
pictureBox1.Refresh
   
Comments
dnibbo 8-Aug-18 10:15am
   
Hi Jochen

Thanks for the reply.

I only added the save file to prove that the picture box image was as expected, which it was.
I removed the save file and tried the refresh method but still it will not show the image.

Thanks
Jochen Arndt 8-Aug-18 10:21am
   
Then the only reasons that comes to my mind is that the pricture box is overlayed by another control, is zero sized, or updating (redrawing) is suppressed (where I don't know how that could happen).
dnibbo 8-Aug-18 10:38am
   
Thanks, the picturebox is the only control on the dialog and is dopcked to full screen.
I did wonder if it had zero size or something so inspected the objects and all is good.
What I have discovered is the picture box is actually showing the ErrorImage.
Hard to tell as the default ErrorImage is the same as the default InitialImage.
I changed the error image and that is what is being displayed now.
So how do I diagnose why the error image is being used? The code is in a try/catch but no exceptions are displayed.
So I guess the question has changed a little... what makes a picturebox display the errorImage instead of the Image.

Thanks again
Jochen Arndt 8-Aug-18 10:43am
   
Your image is probably invalid. When loading from a memory stream it must be a valid bitmap (not another image format like GIF, PNG, JPEG and even not a bitmap file which has additional header bytes in front of the bitmap data).
Rate this:
Please Sign up or sign in to vote.

Solution 2

OK here is what happened and how to diagnose such errors just in case anyone finds this whilst searching.

To diagnose make sure your initial image, Image and Error Image are all different.
That way you can instantly see if it is erroring.

If it is erroring then use the picturebox.LoadComplete event.
Have a look at e.Error and voila the reason for the error image is revealed.
Would be better if the control threw an exception in my humble opinion but it is what it is...

Hope this helps someone.

BTW, so you can all laugh at me, the reason mine was failing was due to an invalid value in the image location property. I thought it meant where (location) in the picture box did I want the image to be rendered so I set it to centre LOL. It did not complain at design time but at runtime it was actually looking for a file location called c:/projectfolder/centre to load the image from. whoopsie.

Thanks for the replies.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100