Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread BLOB image type
Sun, Jul 8 2012 7:43 AMPermanent Link

IQA

Hi Tim & Team,

Just a quick question, how can you tell what image type is stored in a
BLOB ?

Just looking at EDB Manager and when I click on a BLOB and open up the
Edit BLOB window, it automatically shows the jpeg or png or bmp preview,
so I'm thinking there must be some way to know the format as it's
displaying a preview and this could be handy for retrieving the client
logo which could several image formats.

Thanks,

Phil.
Sun, Jul 8 2012 8:36 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Phil


Without checking I doubt its in the engine, much more likely in EDBManager. This is what I use to get the image type from a stream



function ImageFileTypeFromStream(ms: TMemoryStream; const default: string = 'jpeg'): string;
var
ChkChars: array[0..20] of char;
jpgCheck: array[0..4] of char;
CharsToRead: integer;
begin
Result := default;
ms.Position := 0;
if ms.Size > 20 then CharsToRead := 20 else CharsToRead := ms.Size;
ms.ReadBuffer(ChkChars, CharsToRead);
if Pos('BM', ChkChars) = 1 then Result := 'bmp'
else if Pos('GIF', ChkChars) = 1 then Result := 'gif'
else if Pos('PNG', ChkChars) = 5 then Result := 'png'
else if (Copy(ChkChars, 1, 2) = 'II') and (ChkChars[3] = '*') then Result := 'tiff'
else if (Copy(ChkChars, 1, 2) = 'MM') and (ChkChars[4] = '*') then Result := 'tiff'
else if (ms.Size > 10) and (Ord(ChkChars[0]) = 255) and (Ord(ChkChars[1]) = 216) and (Ord(ChkChars[2]) = 255) and (Ord(ChkChars[3]) = 224) then begin
 ms.Position := 6;
 ms.ReadBuffer(jpgCheck, 4);
 if jpgCheck = 'JFIF' then Result := 'jpg';
end;
end;

Basically you look at the image data and see if an appropriate header is in there. It works fine on the 5 types I test for.

Roy Lambert [Team Elevate]
Sun, Jul 8 2012 11:06 PMPermanent Link

IQA

Thanks Roy that was very useful to know...

Just wondering whats the best way to go about saving a physical image
file into a blob using code?

Would I use a FileStream, then somehow get the filestream into the BLOB
field?

I've been using a TDBImage, but the trouble is it keeps converting the
image to BMP all the time, so I want to keep the original file format,
thus load the file contents selected by the user into the blob.
Mon, Jul 9 2012 12:52 AMPermanent Link

Terry Swiers

Phil,

> Just wondering whats the best way to go about saving a physical image file
> into a blob using code? Would I use a FileStream, then somehow get the
> filestream into the BLOB
field?

Yes.  Open a file stream to the image that you want to load, create a blob
stream, and then write the file stream to the blob stream.

procedure PutDBImage(aFileStream : TFileStream; Table: TEDBDataSet; const
FieldName: String);
var
BS : TEDBBlobStream;
begin
try
if Table.State = dsBrowse then Table.Edit;
except
Exit;
end;

try
BS := TEDBBlobStream.Create( TBlobField(Table.FieldByName(FieldName)),
bmWrite );
aFileStream.SaveToStream(BS);
BS.Free;
except
end;

end;


---------------------------------------
Terry Swiers
Millennium Software, Inc.
http://www.1000years.com
http://www.atrex.com
---------------------------------------

Mon, Jul 9 2012 1:17 AMPermanent Link

IQA

Its all good... I've come up with this...

TFileStream *myFile = new TFileStream(LogoOpenDialog->FileName, fmOpenRead);

TEDBBlobStream *BlobStream;
BlobStream = new
TEDBBlobStream((TBlobField*)DM->MasterTable->FieldByName("logo"),bmWrite);
BlobStream->CopyFrom(myFile, myFile->Size);

pictype = DM->ImageFileTypeFromStream(BlobStream);

delete myFile, BlobStream;
Thu, Aug 2 2012 3:35 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Phil,

<< Just looking at EDB Manager and when I click on a BLOB and open up the
Edit BLOB window, it automatically shows the jpeg or png or bmp preview, so
I'm thinking there must be some way to know the format as it's displaying a
preview and this could be handy for retrieving the client logo which could
several image formats. >>

The EDB Manager uses this code:

  BLOB_BINARY = 0;
  BLOB_BITMAP = 1;
  BLOB_JPEG = 2;

function GetBlobType(BlobStream: TStream): Integer;
var
  TempByte: Byte;
  TempInteger: Integer;
begin
  Result:=BLOB_BINARY;
  with BlobStream do
     begin
     Position:=0;
     if (Read(TempByte,SizeOf(Byte))=SizeOf(Byte)) and
        (Chr(TempByte)='B') then
        begin
        if (Read(TempByte,SizeOf(Byte))=SizeOf(Byte)) and
           (Chr(TempByte)='M') then
           begin
           if (Read(TempInteger,SizeOf(Integer))=SizeOf(Integer)) and
              (TempInteger=Size) then
              begin
              Result:=BLOB_BITMAP;
              Exit;
              end;
           end;
        end;
     Position:=0;
     if (Read(TempByte,SizeOf(Byte))=SizeOf(Byte)) and
        (TempByte=$FF) then
        begin
        if (Read(TempByte,SizeOf(Byte))=SizeOf(Byte)) and
           (TempByte=$D8) then
           begin
           Position:=(Size-(SizeOf(Byte)*2));
           if (Read(TempByte,SizeOf(Byte))=SizeOf(Byte)) and
              (TempByte=$FF) then
              begin
              if (Read(TempByte,SizeOf(Byte))=SizeOf(Byte)) and
                 (TempByte=$D9) then
                 begin
                 Result:=BLOB_JPEG;
                 Exit;
                 end;
              end;
           end;
        end;
     end;
end;

If you have any other questions, please let me know.

Tim Young
Elevate Software
www.elevatesoft.com
Image