How to display an image from a OLE Object field of an Access database ?

S

Sandi

I have a simple problem:
I have an Access database (images.mdb) that has 2 columns: one is the id if
the picture (an integer) and one (column named picture) is a field of type
OLE Object which contains an image (it can store jpg, bmp, gif, but I don't
know what image is stored inside).
I want to retrieve the picture stored in the database and identified by a
given id and display it in a web page (.aspx).
I write in Visual C#, but it does not matter, VB answers are just as
wellcome.
My problem is that this OLE Object field does not contain just the raw array
of bytes that form the image. So I can not just read the array of bytes and
output it to the browser.
no, it contains some extra information about the type of the file stored
(which would be good to know so I can know what kind of image it is). but I
don't know how to get this information.
I also don't know how to separate this information from the actual image.
Does anyone know how to solve this ?
 
N

Natty Gur

Hi,

This code works for me. I think i took it from Visual studio magazine.

int offset = 78;

cn.Open();
byte [] img = (byte[])
cmd.ExecuteScalar();
ms.Write(img, offset,
img.Length-offset);
cn.Close();

Bitmap bmp = null;
bmp = new Bitmap(ms);
Response.ContentType = "image/gif";
bmp.Save(Response.OutputStream,
ImageFormat.Gif);
ms.Close();


Natty Gur[MVP]
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377
 
S

Sandi

THANKS a LOT !!!!!!!!!

This is the best answer anyone gave me, but still...
If a bitmap is stored in the OLE Object, than indeed this offset (78) works,
because the header that Access adds to the image is indeed 78 bytes long.
However, if a .gif or a .jpg or a .png is stored, than this offset no longer
works because the header has a different length.
My problem is that I do not know what type of image I am retrieving. It can
be a .bmp but it can just as well be a .gif or a .jpg
So do you have any ideea what should be the offset for theese types of
images (.gif, .jpg) ???

And by the way, I did not design the database like this :(
I just have to read it...
 
N

Natty Gur

Hi,

Sorry don’t have any clue.

Try to write a program that increases the off set until you will get
viewable image. That the best I can offer?

Natty Gur[MVP]
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top