Load blob from database and save to disk lazarus / 16-Databases ---------------------------------------------------------------------- hamacker · 2021-10-30 18:07 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.F ieldByName('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 · 2021-10-30 18:18 MyStream : TMemoryStream;   MyQuery : TZQuery;   TBlobField(MyQuery.FieldByName('fnPIC')).SaveToStream(MyStream); ---------------------------------------------------------------------- wp · 2021-10-30 23:56 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 · 2021-10-31 06:29 Quote from: wp on October 31, 2021, 01:56:58 amTDBImage 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. 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/ ---------------------------------------------------------------------- Jurassic Pork · 2021-10-31 08:32 hello, in the Lazarus distribution, you have an example to manage images with a database (blobs and links). The example is in the folder Lazarus/examples/database/image_mushrooms) : it's a database of deadly mushrooms) using sqlite3 or firebird embedded.  Use SQLDBLaz package and lazReport for printing. Friendly, J.P ---------------------------------------------------------------------- wp · 2021-10-31 09:07 @JurassicPorc: This application contains images in a raw BLOB field where the OP's code works correctly. The observation that I described refers to fields created as ftGraphic (not ftBlob). Therefore: @loaded: Can you post a compilable simplified project which shows the issue? ---------------------------------------------------------------------- loaded · 2021-10-31 11:38 Quote from: wp on October 31, 2021, 10:07:29 am@loaded: Can you post a compilable simplified project which shows the issue? It's an honor for me.  The attached study was made according to the Postgresql Database. Let's not forget to set the username and password settings. I had a project that was used related to the subject, so I created a small application by quoting the content of the project in a way suitable for the purpose. ---------------------------------------------------------------------- wp · 2021-10-31 11:47 Sorry, I don't have Postgresql on my pc and do not want to install it just to help with a question. Could you convert the database to something which is more readily available, such as sqlite3? ---------------------------------------------------------------------- loaded · 2021-10-31 12:17 Quote from: wp on October 31, 2021, 12:47:43 pmSorry, I don't have Postgresql on my pc and do not want to install it just to help with a question. Could you convert the database to something which is more readily available, such as sqlite3? As I said before, it is an honor for me.  Sql lite and Postgresql are incredibly similar!!! I learned this now. In the example I sent, it is enough to change the Library location and protocol information from the properties of Zconnection. ---------------------------------------------------------------------- alpine · 2021-10-31 12:44 Quote from: loaded on October 31, 2021, 01:17:32 pm*snip* Sql lite and Postgresql are incredibly similar!!! I learned this now. In the example I sent, it is enough to change the Library location and protocol information from the properties of Zconnection. Perhaps you wanted to say "It is so easy to switch between sqlite and postgresql in TZConnection". ;) Because the two are actually quite different animals. ---------------------------------------------------------------------- loaded · 2021-10-31 12:54 Quote from: y.ivanov on October 31, 2021, 01:44:29 pmPerhaps you wanted to say "It is so easy to switch between sqlite and postgresql in TZConnection". Because the two are actually quite different animals. Yes you are right;  one is a kestrel, the other is an eagle. ---------------------------------------------------------------------- wp · 2021-10-31 14:38 For me there is no issue with the demo. What should I see? I don't want to spend much time in debugging it, but it is strange that the DB does not keep its data when the application terminates. ---------------------------------------------------------------------- loaded · 2021-10-31 14:56 I, Probably my way of explaining was wrong, so I was misunderstood. Because; I talked about my working method, not that I have a problem. Sorry. ---------------------------------------------------------------------- hamacker · 2021-11-08 13:51 I did see that example, but not helpfull because this project use dbimage and load from file, I have no problem with this. My problem is, why when I save to disk is a broken file. Quote from: Jurassic Pork on October 31, 2021, 09:32:20 amhello, in the Lazarus distribution, you have an example to manage images with a database (blobs and links). The example is in the folder Lazarus/examples/database/image_mushrooms) : it's a database of deadly mushrooms) using sqlite3 or firebird embedded.  Use SQLDBLaz package and lazReport for printing. Friendly, J.P ---------------------------------------------------------------------- hamacker · 2021-11-08 14:39 you were great, great tips. There should be more people like that. Quote from: loaded on October 31, 2021, 12:38:03 pmQuote from: wp on October 31, 2021, 10:07:29 am@loaded: Can you post a compilable simplified project which shows the issue? It's an honor for me.  The attached study was made according to the Postgresql Database. Let's not forget to set the username and password settings. I had a project that was used related to the subject, so I created a small application by quoting the content of the project in a way suitable for the purpose.