Saving picture from DB

Hi!
How can save picture (Bitmap image) from database (Access)?
Best regards,
Yura.
[100 byte] By [YuraSV] at [2007-11-17 17:00:06]
# 1 Re: Saving picture from DB
Here is a sample code that you can work with.

I've tested this with a table called tblImage that has 2 columns. First column is ID (type: autonumber) and the second one is IMGDATA (type: OLE Data).

On Form1, I have 2 buttons, Button1 and Button2 to IMPORT from IMAGE file and EXPORT from DB respectively. I also have an ADO DATA CONTROL (Adodc1) that is pointing to the MDB and the .RECORDSOURCE set to tblImage.

' saves from IMG file into DB
Private Sub Button1_Click()
' get file name
dim szFile as string
szFile = InputBox("Enter image path")

' open file and read data
dim imgByte() as Byte
open szFile for Binary access read for #1
redim imgByte(LOF(1))

dim nPos as long
nPos = 0
while (not eof(1))
get #1, nPos+1, imgByte(nPos)
nPos = nPos + 1
wend

' save into db
dim adoRS as ADODB.RecordSet
set adoRS = Adodc1.RecordSet

With adoRS
.AddNew
.Fields(1).Value = imgByte
end with

' close everything
close #1
redim imgByte(0)
set adoRS = Nothing
End Sub

' Saves from DB into file
Private Sub Button2_Click()
' get file name
dim szFile as string
szFile = InputBox("Enter new image path")

' get current record
dim adoRS as ADODB.RecordSet
set adoRS = Adodc1.RecordSet

' get data
dim imgByte() as Byte
redim imgByte(adors.Fields(1).actualsize)
imgByte = adoRs.Fields(1).value

' write data
open szfile for binary access write as #1
put #1, , imgByte
close #1

set adoRS = nothing
End Sub

Good Luck,
-Cool Bizs

[Cimperiali colorized for better reading]
coolbiz at 2007-11-10 0:23:03 >
# 2 Re: Saving picture from DB
Thanks a lot!
YuraSV at 2007-11-10 0:24:06 >
# 3 Re: Saving picture from DB
I don`t understand where I can take Adodc1.

I tryed to do all this using simply Database and recordset. Everythig was perfectly except results :)) Files that appears on my disk are not files that I have put into OLE field. It include ole object as I understand.

Dim byProg() As Byte
byProg = rsSystem.Fields(3).Value


Dim nFreeFiles As Integer
nFreeFiles = FreeFile

Dim sFileName As String
sFileName = sFolder & "\" & sDir & "\" & sName

Open sFileName For Binary Access Write As #nFreeFiles
Put #nFreeFiles, , byProg
Close #nFreeFiles


How can I do it?

Regards,
Yura.
YuraSV at 2007-11-10 0:25:04 >
# 4 Re: Saving picture from DB
Here is a sample for you.

Good luck,
-Cool Bizs
coolbiz at 2007-11-10 0:26:10 >
# 5 Re: Saving picture from DB
I`m sorry for my stupid question, but how have you added Binary Data into the OLE object field?

Yura
YuraSV at 2007-11-10 0:27:02 >
# 6 Re: Saving picture from DB
Just assign the BYTE array to the recordset value. For example,

Sub SaveBinary(adoRS as RecordSet)
' byte array
dim bytData(100) as Byte
... fill up the bytData with data ...

' assuming the field index 1 is OLE DATA type
adoRS.Fields(1).Value = bytData
End Sub

Have fun,
-Cool Bizs
coolbiz at 2007-11-10 0:28:12 >
# 7 Re: Saving picture from DB
I also have created an application that reads photos from a database and can add them too. Its a different solution, to the one mentioned above.

I used common dialog 1 to retrieve the filename of the picture and embed the picture in the ole object. As follows

Private Sub olePhoto_Click()

CommonDialog1.Filter = "Photo Files(*.bmp,*.jpg,*.gif)|*.bmp;*.jpg;*.gif"
CommonDialog1.CancelError = True
On Error GoTo errorhandler
CommonDialog1.ShowOpen
olePhoto.CreateEmbed CommonDialog1.FileName
Exit Sub

errorhandler:
Exit Sub

End Sub

I had already set up an access 2000 database with an ole photo field to store the picture and linked this to my application using a data control called dtaControl. Then I set the datasource of olePhoto to dtaControl, and set the datafield to photo (the ole photo field set up with access). The only other thing to do is update the database. I did this with a command button as follows:

Private Sub cmdUpdate_Click()
dtaControl.UpdateRecord
End Sub

Hope this helps someone :)
HairyMonkey at 2007-11-10 0:29:07 >
# 8 Re: Saving picture from DB
hello.
I've done all the things that coolbiz have shown in his message, and I used also the database sample that he has posted in your previos reply.
first of all - thanks a lot, coolbiz.
second, it works well, but only if I save the picture into an access data base with the code that coolbiz gave, or import an image within MS access.
my problem is that I have allready many many images in an access 2000 table, in an ole object field, and all the images where saved with cut & paste.
in the access table inside the ole object field - "picture" is written, while in coolbiz's database "long bynary data" is written.
when I use his code to save an image file from the "long bynary data", to a file, it saves the picture ok,
BUT when I use this code to save images that I inserted with cut & paste, where "picture" is written inside the ole object field, i recieve a file of unknown format. it does save the file with the correct size, but image applications cannot show the file.

please, does anybody how can I export all my images from my access 2000 DataBase, to files??

thaks a lot
shmid
shmid at 2007-11-10 0:30:11 >
# 9 Re: Saving picture from DB
how about using the insert statment instead of the addnew? thanx.
beakman at 2007-11-10 0:31:09 >
# 10 Re: Saving picture from DB
Hi!
I'm a newbie of VB programming. What is the difference between the two methods above and, e.g., to save the path and filename in a text field of db?

Thanks!

Alexspin
alexspin at 2007-11-10 0:32:16 >
# 11 Re: Saving picture from DB
I have downloaded sample in C drive, it is not adding any new picture in the table when I click save from file .. I have given image file name also please help. it is working for savetofile method...
kcmohd at 2007-11-10 0:33:11 >