Am I being stupid? How to load an image from SQL and display it as a label background?
-
Hi all, I'm using VB.net2008SP1, framework 3.51 All I want to do is get an image stored in a database and display it as a background on a label uielement in my grid (have I got the terminology right here?) The code I have is:
Using ms As New IO.MemoryStream With DirectCast(Data, Byte()) ms.Write(DirectCast(Data, Byte()), 0, .Length) End With ms.Seek(0, IO.SeekOrigin.Begin) Dim bmi As New BitmapImage bmi.BeginInit() bmi.StreamSource = ms bmi.EndInit() Dim ImageDrawing As New ImageBrush(bmi) iResult = ImageDrawing End Using
I then pass iResult to the background property of my label. The result is an empty box - no image, just the frame. If I were to load the image from a file using this code, it works.
Dim Bmp As New BitmapImage(New Uri(DR("BackgroundImage").ToString)) Dim BackBrush As New ImageBrush(Bmp) MyLabel.Background = BackBrush
Unfortunately, due to the licensing of the imagelibrary I am using, I cannot save these images in a temp folder or anything simple like that. I have tried googling, rummaging around code project, typing random commands, and have just tried a strong coffee, but I cant get it working. Can anyone help me????
-
Hi all, I'm using VB.net2008SP1, framework 3.51 All I want to do is get an image stored in a database and display it as a background on a label uielement in my grid (have I got the terminology right here?) The code I have is:
Using ms As New IO.MemoryStream With DirectCast(Data, Byte()) ms.Write(DirectCast(Data, Byte()), 0, .Length) End With ms.Seek(0, IO.SeekOrigin.Begin) Dim bmi As New BitmapImage bmi.BeginInit() bmi.StreamSource = ms bmi.EndInit() Dim ImageDrawing As New ImageBrush(bmi) iResult = ImageDrawing End Using
I then pass iResult to the background property of my label. The result is an empty box - no image, just the frame. If I were to load the image from a file using this code, it works.
Dim Bmp As New BitmapImage(New Uri(DR("BackgroundImage").ToString)) Dim BackBrush As New ImageBrush(Bmp) MyLabel.Background = BackBrush
Unfortunately, due to the licensing of the imagelibrary I am using, I cannot save these images in a temp folder or anything simple like that. I have tried googling, rummaging around code project, typing random commands, and have just tried a strong coffee, but I cant get it working. Can anyone help me????
Yes... I was being stupid...
bmi.BeginInit() **bmi.CacheOption = BitmapCacheOption.OnLoad** bmi.StreamSource = ms bmi.EndInit()
Setting the cacheoption to be onload sorts the problem out. I guess that the image was being created after the stream was closed (or something along those lines). :doh: