Forum > Databases

Load blob from database and save to disk

(1/4) > >>

hamacker:
Hi All,
I have pictures in my database, almost in png and jpg. So I need to load in DBGrid inside a cell, buts FAILs.
So I Create a test function to transfer blob to disk and test an image. But all picture that I save to disk was not recognize in Gimp or any image editor. I see that is correct bacause I can load into TDBImage. Whats wrong in this code, Why my blob was save in corrupted format:
procedure TfmLista_EPI.Load_All_Pics;
var
  q1:TZQuery;
  St: TStream;
  fs: TFileStream;
  sFileName:String;
  i:Integer;
begin
  q1:=TZQuery.Create(nil);
  q1.Connection:=dmPrincipal.sesmt_db;
  q1.sql.add('select uuid_epi, foto, foto_formato from epi');
  q1.sql.add('where (foto is not null) and (coalesce(foto_formato,'''')<>'''')  ');
  try
    q1.Open;
    if not q1.IsEmpty then
    begin
      i:=ImageList_Foto.Count;
      while not q1.EOF do
      begin
        // salvar no disco para debug
        sFileName:='c:\temp\'+q1.FieldByName('uuid_epi').AsString+q1.FieldByName('foto_formato').AsString;
        try
          fs := TFileStream.Create(sFileName, fmCreate );
          St := q1.CreateBlobStream(q1.FieldByName('foto'), bmRead);
          st.Position := 0;
          fs.CopyFrom(st,st.Size);
        finally
          st.Free;
          fs.free;
        end;
        q1.Next;
        Application.processMessages;
      end;
    end;
  except
    on e:exception do raise exception.Create(e.message);
  end;
  q1.Free;
end;

pcurtis:
  MyStream : TMemoryStream;
  MyQuery : TZQuery;

  TBlobField(MyQuery.FieldByName('fnPIC')).SaveToStream(MyStream);

jamie:
if memory servers Blobs have a limited size.

wp:
TDBImage reads in its LoadPicture a few bytes (the file extension) before the real image data to identify the blob format. I think this is the reason why DBImage can display your image field data, but a written file cannot.

Open the file in a hex editor, and find out where the typical header of the image format starts. Bitmaps, for example, always start with a "BM". Determine the offset to the "BM", and then, instead of fully rewinding the stream before writing to file (st.Position := 0), move it to this offset (st.position := offs). This way you write the file without these leading bytes.

loaded:

--- Quote from: wp on October 31, 2021, 01:56:58 am ---TDBImage reads in its LoadPicture a few bytes (the file extension) before the real image data to identify the blob format. I think this is the reason why DBImage can display your image field data, but a written file cannot.

--- End quote ---

Wp master, I didn't know that. Me too, unknowingly;
Before saving the file data as a stream, in a stream that I defined myself;
I am saving the data of file extension + file as a whole.
This makes it easier to save and restore all file formats without the need to create a separate column for the file extension.

As for hamacker problem, you can directly see the source of the problem by comparing the file you saved and the original file with a hex editor like 010.   https://www.sweetscape.com/010editor/

Navigation

[0] Message Index

[#] Next page

Go to full version