Read bytes from image

C

CyberDwarf

Hi

I need to read the bytes from an image file (jpg, bmp, etc) into a byte
array.

I've tried to do this with a stream object, but can't get it to work!

Anyone got any ideas?

All help appreciated

TIA

Steve
 
T

Terry Kreft

The following are snippets of code from a working project, the code snippets
have also been editted to simplify them

The essential lines of code are:-

' This gets the contents of the file into the stream object

Dim mstream As ADODB.Stream

Set mstream = New ADODB.Stream
With mstream
.Type = adTypeBinary
.Open
.LoadFromFile strFile
End With

' Then to get the stream contents into the field

Dim rs As ADODB.Recordset

' Initialise and instantiate the rs recordset object then ...

With rs
.AddNew
.Fields(fnFILE_NAME).Value = strFile
.Fields(fnPICTURE_DATA).Value = mstream.Read
.Update
End With

Note fnFILE_NAME etc are constants which identify the fields being updated.
 
C

CyberDwarf

Hi Terry

Thanks for your post

This was more or less what I have tried...

Current problem seems to be to get the ADODB.Recordset to open and read from
the Stream

TIA

Steve
 
S

Serkan Uzuner

Thanks but a question from me.
Ok, I read binary data but how can i display picture in my access form with
this stream data.
I know that I can display with temp a picture file. for example;
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs.Fields("PicData").Value
mstream.SaveToFile "c:\temp.jpg", adSaveCreateOverWrite
picture1.picture="c:\temp.jpg"

Can i display picture direct from stream data without a temp file?

Serkan
 
C

CyberDwarf

OK, I got the recordset to read from the stream.

I also got the recordset to write to the SQL image field

Only problem left: the SQL field now won't display the image, as it seems to
be the wrong data format!!

Code:-
=====
(Please note: this has been tested with BMP, JPG & GIF files)
Public Sub SaveImage(strFile As String)
Dim fLen As Integer
Dim bytes() As Byte
Dim f As Field
Dim s As Stream
Set s = New Stream
Dim mstream As ADODB.Stream
'Get file contents into stream
Set mstream = New ADODB.Stream
With mstream
.Type = adTypeBinary
.Open
.LoadFromFile strFile
End With
' Get the stream contents into the field
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
' Initialise and instantiate the rs recordset object then ...
With rs
rs.Open "Select * from photos where ph_compno=42346",
CurrentProject.Connection, adOpenDynamic, adLockPessimistic
rs.Fields("ph_photo").Value = mstream.Read
rs.Update
End With
rs.Close
End Sub
 
T

Terry Kreft

Well there's not much extra to the code really, the recordset (as I have it)
is opened like this

rs.Open "Select * from Pictures", cn, adOpenKeyset, adLockOptimistic
 
T

Terry Kreft

Which control type are you using for the picture1 control?

If I'm right in guessing that you are using an image control then what you
need to set is the picturedata property, this needs to be a device
independent bitmap though (dib).

I would recommend going to Stephen Lebans website and downloading his
LoadSaveJpeg example for whichever version of Access you are using.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top