Page 1 of 1

How to save images in Access DB

Posted: Tue May 13, 2008 8:32 pm
by MCHSRAS
In the samples, there isn't any code on how to save an image in an Access db. Is there a way to save the image in an AccessDB?

Re: How to save images in Access DB

Posted: Thu May 15, 2008 2:28 pm
by Loïc
Hi,

You can download the last package of GdPicture Pro. It include an Access sample which shows how to load / save an image using an OLE field.



1 - Create a table (table1) with 3 fields:
ID: numeric (primary key)
Title: String (50)
Image: OLE

2 - into a form add:

Code: Select all

Private Sub Form_Load()
   Me.RecordSource = "SELECT * FROM table1"
End Sub
3 - Code to append the native image of an Imaging object to the OLE field:

Code: Select all

Dim arBytes() As Byte
      
If Imaging1.SaveAsByteArray(arBytes, 0, "jpg") = 0 Then
   Me.Recordset.Edit
   Me.Recordset.Fields("Image").AppendChunk arBytes
   Me.Recordset.Update
   MsgBox "Image saved"
End If
Erase arBytes

4 - Code to read an image from the OLE field:

Code: Select all

Dim arbyte() As Byte

arbyte = Me.Recordset.Fields("Image").GetChunk(0, LenB(Me.Recordset.Fields("Image")))
Call Imaging1.CreateImageFromByteArray(arbyte)
  
Erase arbyte
Best regards,

Loïc Carrère