Board index » delphi » Displaying Image from SQL Server

Displaying Image from SQL Server


2004-07-10 09:20:52 AM
delphi11
I've inherited a table with a field of type "Image" that has (supposedly)
photos in it that need to be displayed in an existing app. When I try to
assign a DBImage to the field, however, I get an error of "Bitmap image is
not valid". I am not even sure that the image that was originally stored was
a bitmap - it could have been a JPG.
Is there a trick to reading the field out of the SQLServer table to display
it on a TImage??
Using Delphi 7 and SQLServer 2000 if it matters.
Jeff
 
 

Re:Displaying Image from SQL Server

Jeff Swanberg writes:
Quote
Is there a trick to reading the field out of the SQLServer table to
display it on a TImage??
You could save the field "as is" and inspect that file with an image tool
like IrfanView.
BTW: JPEG images usually start with "JFIF"
--
Ben
 

Re:Displaying Image from SQL Server

Also, If you happen to be using the Northwind database. The images
contained therein were stored as OLE objects and contain a 78 byte
header. you will need to skip this header portion when streaming the
image to a file.
x
--
Xavier Pacheco
President, Consultant
Xapware Technologies Inc
www.xapware.com
---
Author - Delphi for .NET Developer's Guide
www.amazon.com/exec/obidos/ASIN/0672324431/
 

Re:Displaying Image from SQL Server

Xavier,
While I am not using the NW sample DB, it does look like there is some sort
of header on the image field I am trying to access but I have NO idea on how
to stream it at all - let alone how to strip out the header. Do you know of
any code samples that might show an example of how to do this??
I'm also assuming that, using this technique, I would stream the field contents
to a file on the local drive and then assign that file to the TImage
component?
js
Quote
contained therein were stored as OLE objects and contain a 78 byte
header. you will need to skip this header portion when streaming the
image to a file.

 

Re:Displaying Image from SQL Server

I usually just use a couple of helper functions (below). you will have to
adjust where you seek into the memory stream based in the data
contained in the blob field.
- x
function BufferFromBlobField(var ABuffer; const ASize: Integer;
const ABlobField: TBlobField): Integer;
var
MemoryStream: TMemoryStream;
begin
MemoryStream := TMemoryStream.Create;
try
ABlobField.SaveToStream(MemoryStream);
MemoryStream.Seek(0,0);
Result := MemoryStream.Read(ABuffer, ASize);
finally
MemoryStream.Free;
end;
end;
function BlobFieldFromBuffer(const ABuffer; const ASize: Integer;
ABlobField: TBlobField): Integer;
var
MemoryStream: TMemoryStream;
begin
MemoryStream := TMemoryStream.Create;
try
Result := MemoryStream.Write(ABuffer, ASize);
MemoryStream.Seek(0,0);
ABlobField.LoadFromStream(MemoryStream);
finally
MemoryStream.Free;
end;
end;
--
Xavier Pacheco
President, Consultant
Xapware Technologies Inc
www.xapware.com
---
Author - Delphi for .NET Developer's Guide
www.amazon.com/exec/obidos/ASIN/0672324431/