Access 97 Images

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm looking for an example of how to extract images from an Access 97 blob into an asp datagrid. Anybody have examples?
 
Here's some code extracted from appdev courseware (www.appdev.com)
that was written by Andy Baron, Ken Getz and myself. When you're
working with blobs that originated in Access, you have to strip off
the header info that Access uses for OLE fields. This sample was
designed for a windows form and works with SQLS Northwind, but the
concept is the same -- you have to stream in the data and strip off
those header bytes before you can use the image for anything. I can
probably dig up the VB code from somewhere if you can't figure out the
C#.

--mary

private void LoadEmployeePhoto(int employeeID)
{
SqlDataAdapter da = null;
DataTable dt = new DataTable();
string strCnn =
"Data Source=(local);Database=Northwind;"
+ "Integrated Security=SSPI";
string strSQL =
"SELECT Photo FROM dbo.Employees"
+ " WHERE EmployeeID = "
+ employeeID.ToString();
MemoryStream msPic;
// Signature bytes of an
// OLE container header.
const byte OLEbyte0 = 21;
const byte OLEbyte1 = 28;
// Number of bytes in
// an OLE container header.
const int OLEheaderLength = 78;

da = new SqlDataAdapter(strSQL, strCnn);
da.Fill(dt);
if (dt.Rows.Count == 0)
return;

// Move binary picture data into the byte array
byte[] abytPic = (byte[])dt.Rows[0]["Photo"];

// Test for an OLE container header
if ((abytPic[0] == OLEbyte0)
&& (abytPic[1] == OLEbyte1))
{
// Use a second array to strip off the header.
// Make it big enough to hold
// the bytes after the header.
byte[] abytStripped =
new byte[abytPic.Length - OLEheaderLength];
// Strip off the header by copying the bytes
// after the header.
System.Buffer.BlockCopy(
abytPic, OLEheaderLength, abytStripped,
0, abytPic.Length - OLEheaderLength);

// Load the new byte array
// into a MemoryStream.
msPic = new MemoryStream(abytStripped);
}
else
{
// Load the original byte array into a MemoryStream
msPic = new MemoryStream(abytPic);
}
// Set the picture box image, using the stream.
picEmployee.Image = Image.FromStream(msPic);
}
 
Mary,

Thanx for your answer. I tried your suggestions and of course can get it
to work with SQL Northwind, but not Access Northwind. I do not believe
images are stored the same in each database. IN SQL "Photo" is an int16,
and there is a column labled PhotoPath which is an nvarchar 255. In
Access there is only a "Photo" column which is an OLE object.
Do you happen to know where I can find the format of the Access OLE
object described?
Again, thank you for taking time to answer my question.
Bob
 
Hi Bob,

Are you sure it is in Access97 a blob field and not just a path on disk?

Cor
 
Let me get back to you on this -- I don't think we ever tested it
against Access directly, so it would be nice to know if the header
fields were of a different size (or whatever). It's basically just
streaming in a bunch of bytes, so the actual storage might be slightly
different between Jet and SQLS.

--mary
 
Hi Bob,

Sorry,

Now I see I misreaded it.

I was very happy with that sample from Mary. I made from it in VBnet a
generic blob conversion, sample. And I was happy I had now at last a sample
which could do both (that 21 and 28 does that). However now there seems to
be a thirth format, so I am also curious for that. (Not more than curious).

I made it also workable as a background for an asp datagrid. However what do
you mean with pictures in an asp.datagrid?

Cor
 
¤
¤ Mary,
¤
¤ Thanx for your answer. I tried your suggestions and of course can get it
¤ to work with SQL Northwind, but not Access Northwind. I do not believe
¤ images are stored the same in each database. IN SQL "Photo" is an int16,
¤ and there is a column labled PhotoPath which is an nvarchar 255. In
¤ Access there is only a "Photo" column which is an OLE object.
¤ Do you happen to know where I can find the format of the Access OLE
¤ object described?
¤ Again, thank you for taking time to answer my question.

There are several factors here that will affect your ability to retrieve this data. First, if it was
stored in an OLE object field, how was it stored? It could have been stored with a databound OLE
control via the native application for the file, or, it could have been stored as *chunks* or a
*stream* of binary bytes. The latter method is the best case scenario.

If it was stored using the OLE control, then the OLE header information (that will need to be
removed) will vary depending upon the file type and application used to store it. This is the worst
case scenario.

Maybe you identify which method was used.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Paul,

The Access97 Images are from an OLE bound control that I paste gif
images into. Can you point me to when I can figure out how to extract
the gif file?
Thank you for your help.

Bob
 
¤
¤ Paul,
¤
¤ The Access97 Images are from an OLE bound control that I paste gif
¤ images into. Can you point me to when I can figure out how to extract
¤ the gif file?
¤ Thank you for your help.

I will have to take a look at this. If it's a GIF file then it was probably stored with OLE headers
for Microsoft Photo Editor. If you could take a peek at the contents of the buffer using the code
Mary provided and look for "MSPhotoEd.3" this would confirm whether Photo Editor was used.

If Photo Editor wasn't used, we would probably need to see first 50 characters or so of the buffer.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Back
Top