Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Display image in mdb database in Image Control

10 views
Skip to first unread message

Ken Sappio

unread,
Dec 31, 2002, 8:44:03 PM12/31/02
to
I am looking for how to take a bmp or jpg image that's in a mdb database and
display it in an Image Control.

I started out by making a new database in Access and putting a bmp image in
a OLE field. To test if the image was really there I created a report with
an Image Control on pointed it to the field with the image in it. Sure
enough the Image Control displayed the image in the database.

But what I really want to do is write a Visual Basic 6 program that can
connect to that database and using ADO (through code, not the Data Control)
display the image in an Image Control on a Form.

So far I am unable to figure it out.

I have to do it this way because this is the way the images are given to me
to be displayed, they are in a mdb (Access 2000 format database).

I am hoping someone can show me the way.

Below is the code I have been working on but doesn't work.


Thanks
Ken Sappio

Private Sub Form_Load()

Dim sSQL As String


Set ADODBConn = New ADODB.Connection
Set rs = New ADODB.Recordset


'open connection to database
ADODBConn.Open _
"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & App.Path &
"\db1-2000.mdb;Jet oledb:Engine Type = 3;"

ADODBConn.CursorLocation = adUseClient


'open a recordset to the table in the database
rs.Open "SELECT * FROM tblImages", ADODBConn, adOpenStatic,
adLockOptimistic


'load the image in the field "Image" and display it in the Image Control
Image1.Picture = rs("Image")


End Sub


Brian

unread,
Jan 1, 2003, 9:57:43 AM1/1/03
to
What if you create a picture file, then add it to the imagecombo?
Something like this:


Dim FileName As String
Dim FileHnd As Integer

FileName = "C:\image.jpg"

FileHnd = FreeFile()

Open FileName For Output As #FileHnd
Print rs("Image")
Close #FileHnd

Image1.Picture = "C:\image.jpg"
Kill "C:\image.jpg"


That's the only way I can think of.

"Ken Sappio" <k...@netdotcom.com> wrote in
news:#N9chfTsCHA.2604@TK2MSFTNGP12:

Saga

unread,
Jan 1, 2003, 1:45:44 PM1/1/03
to
It just so happéns that I am doing this right now! In DAO, though, but I am
sure (100%) that you can do this the same way (perhaps with minor
adjustments)
in ADO. Here are the steps:

1. If image field is not null read image database field into byte array.
2. Write byte array to temp file
3. Load temp file into picture property using loadpicture
4. Kill temp file

Steps 2 to 4 are executed only if the image field is not null.

Here is the code I am using (in DAO of course)

Dim bytImage() As Byte
Dim intNum As Integer

'Check for signature.
If Not IsNull(rs!Signature) Then
intNum = FreeFile

Open "bitmap.dat" For Binary As #intNum
bytImage() = rs("Signature").GetChunk(0, rs("Signature").FieldSize)

'Read the data and close the file
Put #intNum, , bytImage
Close #1

Set PicSig.Picture = LoadPicture("bitmap.dat")

Kill "bitmap.dat"

End If

Good luck!
Saga

"Ken Sappio" <k...@netdotcom.com> wrote in message
news:#N9chfTsCHA.2604@TK2MSFTNGP12...

Mike D Sutton

unread,
Jan 2, 2003, 1:30:10 PM1/2/03
to
> I am looking for how to take a bmp or jpg image that's in a mdb database
and
> display it in an Image Control.
>
> I started out by making a new database in Access and putting a bmp image
in
> a OLE field. To test if the image was really there I created a report
with
> an Image Control on pointed it to the field with the image in it. Sure
> enough the Image Control displayed the image in the database.
>
> But what I really want to do is write a Visual Basic 6 program that can
> connect to that database and using ADO (through code, not the Data
Control)
> display the image in an Image Control on a Form.
>
> So far I am unable to figure it out.
>
> I have to do it this way because this is the way the images are given to
me
> to be displayed, they are in a mdb (Access 2000 format database).

You can do this without writing the file to disk (If you're storing Bitmap
data only though, for other formats you'll need to dig into other libraries)
Here's a simple example which demonstrates how to do this:

'***
Private Declare Function SetDIBitsToDevice Lib "gdi32" _
(ByVal hdc As Long, ByVal x As Long, ByVal y As Long, _
ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, _
ByVal SrcY As Long, ByVal Scan As Long, _
ByVal NumScans As Long, ByRef Bits As Any, _
ByRef BitsInfo As Any, ByVal wUsage As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal Length As Long)

Private Type BITMAPINFOHEADER '40 bytes
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type

Private Sub Form_Load()
Dim MyDB As Database
Dim MyRS As Recordset
Dim FNum As Integer
Dim ImgData As String
Dim FilePath As String
Dim FileSize As Long
Dim BMISize As Long
Dim BMIH As BITMAPINFOHEADER
Dim BinData() As Byte

' BITMAPFILEHEADER struct size
Const BMFHSize = 14

' Set the demo image path
This should error - You'll need to create the file:
FilePath = "C:\TestImg.bmp"
FNum = FreeFile

' Check the file size (And if it exists)
On Error Resume Next
FileSize = FileLen(FilePath)
On Error GoTo 0

' Quick if the file is empty or missing
If (FileSize = 0) Then Exit Sub

' Open the file and read the data into a local buffer
Open FilePath For Binary Access Read Lock Write As #FNum
ImgData = Space$(FileSize)
Get #FNum, , ImgData
Close #FNum

' Open the database and recordset
Again this should error, you'll need the database now with a table
called "tblImages" and a field called "Image" as "memo" data type.
It can be blank, this will add the image then read it back for you.
Set MyDB = OpenDatabase("C:\TestImgs.mdb")
Set MyRS = MyDB.OpenRecordset("SELECT * from tblImages")

' Add a new record and dump the image data into it
MyRS.AddNew
MyRS!Image = ImgData
MyRS.Update

' Clear image data (So I'm not cheating)
ImgData = ""

' Move back to the start of the database and read data
MyRS.MoveFirst
ImgData = MyRS!Image

' Close and kill recordset
Call MyRS.Close
Set MyRS = Nothing

' Close and kill database
Call MyDB.Close
Set MyDB = Nothing

' Convert the image data to binary data
BinData() = StrConv(ImgData, vbFromUnicode)
ImgData = "" ' Kill the string version so
' we're not taking up 4 times the memory!

' Fill in the BMIH structure from the image data
Call CopyMemory(BMIH, BinData(BMFHSize), Len(BMIH))

' Work out how big the entire BMI structure is
BMISize = Len(BMIH)
If (BMIH.biBitCount <= 8) Then _
BMISize = BMISize + (((2 ^ BMIH.biBitCount) - 1) * 4)

Picture1.AutoRedraw = True ' Persist drawing and render
Call SetDIBitsToDevice(Picture1.hdc, 0, 0, _
BMIH.biWidth, BMIH.biHeight, 0, 0, 0, _
BMIH.biHeight, BinData(BMISize + BMFHSize), _
BinData(BMFHSize), 0)
Picture1.Refresh ' Refresh image on screen
End Sub
'***

Hope this helps,

Mike


-- EDais --

- Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/
Work E-Mail: ED...@btclick.com
Other E-Mail: Mike....@btclick.com


0 new messages