Loading image in UserForm from Binary Data Stream

alegault2002

New Member
Joined
Apr 3, 2017
Messages
3
I try to load an image control with a data stream read from SQL Server. I have the stream (loaded) but cannot set it to load to the image control.

I have tried
Code:
Dim myImage As Object
myImage = LoadPicture(mstream)
Image1.Picture = myImage
with no success, and when I try to save as a temporary file, I get Error 3004, cannot write file.

I read many other things on encoding 64, decoding 64 but with not success either.

Can anyone help me, please?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
UPDATE:

I resolved the Error 3004, permission issue... When I try to get the image back, I get a message saying that I have an Incorrect Image...

Here is what I have done to retrieve the image (jpg):

Code:
Open "C:\Users\Documents\test.bin" For Binary As #1
   Put #1, 1, encodeBase64(mstream.Read)
Close #1

Image1.Picture = LoadPicture("C:\Users\Documents\test.bin")
I try to load an image control with a data stream read from SQL Server. I have the stream (loaded) but cannot set it to load to the image control.

I have tried
Code:
Dim myImage As Object
myImage = LoadPicture(mstream)
Image1.Picture = myImage
with no success, and when I try to save as a temporary file, I get Error 3004, cannot write file.

I read many other things on encoding 64, decoding 64 but with not success either.

Can anyone help me, please?
 
Upvote 0
FINAL UPDATE (RESOLVED)
Here is the code I finally patched to get it working...

Code:
strSQL = "Select  Photo FROM dbo.tblPhotoEqpt where ideqpt=14"
rst.Open strSQL, cnt, adOpenStatic
Dim adoStream As Object
 Set adoStream = CreateObject("ADODB.Stream")
Dim TempPath As String
TempPath = Environ("temp") & "\"
adoStream.Type = adTypeBinary
     adoStream.Open
     adoStream.Write rst!Photo.Value
     adoStream.SaveToFile TempPath & strFileName, adSaveCreateOverWrite
rst.Close
cnt.Close
adoStream.Close

Image1.Picture = LoadPicture(TempPath & strFileName)

UPDATE:

I resolved the Error 3004, permission issue... When I try to get the image back, I get a message saying that I have an Incorrect Image...

Here is what I have done to retrieve the image (jpg):

Code:
Open "C:\Users\Documents\test.bin" For Binary As #1
   Put #1, 1, encodeBase64(mstream.Read)
Close #1

Image1.Picture = LoadPicture("C:\Users\Documents\test.bin")
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top