Link to home
Start Free TrialLog in
Avatar of goldragon
goldragon

asked on

How to convert a Blob file to image

Hi, How do I convert a blob image file from a database into a Timage, cos I want to grab a picture from my paradox database and display that into a Timage object on the screen with out having to use TDBimage, any suggestion?
ASKER CERTIFIED SOLUTION
Avatar of TheLeader
TheLeader

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TheLeader
TheLeader

even if you would like components
http://www.torry.net/db_images.htm
Avatar of Mohammed Nasman
Hello

if you add the fields using field editor, you can use
Table1MyBlobField.SaveToFile('c:\myimg.bmp');

or
TBlobField(Table1['MyBLobField']).SaveToFile('c:\myimg.bmp');

from question asked few days ago... (!)
https://www.experts-exchange.com/questions/20810387/show-an-image-stored-in-a-database.html

uses jpeg;

function GetGraphicFromBlob(aField: TBlobField): TGraphic;
var
 s: TStream;
 buf: word;
begin
 if not assigned(aField) then exit;
 Result := nil;
 s := aField.DataSet.CreateBlobStream(aField, bmRead);
 try
   if s.read(buf, sizeof(buf)) <> sizeOf(buf) then exit;
   s.Position := 0;
   if buf = $D8FF then
     Result := TJPEGImage.Create
   else if buf = $4D42 then
     Result := TBitmap.Create
   else if buf = $CDD7 then
     Result := TMetafile.Create
   else if buf = 0 then
     Result := TIcon.Create;
   if assigned(Result) then begin
     try
       Result.LoadFromStream(s);
     except
       Result.Free;
       Result := nil;
     end;
   end;
 finally
   s.Free;  // must free stream
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  i := 1;
  with Table1 do begin
    Open;
    try
      // if image will not load from blob then result of getGraphicFromBlob
      // will be nil and Image1 will be empty
     // you can of course check result to give an error to user by using intermediate var
      Image1.Picture.Assign(GetGraphicFromBlob(TBlobField(FieldByName('img'))));
    finally
      close;
    end;
  end;
end;