Saving Pictures to Firebird lazarus / 16-Databases ---------------------------------------------------------------------- goldfish999 · 2015-02-13 20:58 I have a form with a DBImage component.  I can get a jpeg picture to load into the component but I cant save it as a blob to the Database. Please help ---------------------------------------------------------------------- balazsszekely · 2015-02-13 22:39 Firebird? Cool! You should declare the picture field like this:  BLOB SUB_TYPE 0 SEGMENT SIZE 80 Code: [Select]uses db; var   st: TStream;   JPEG: TJPEGImage; begin    JPG := TJPEGImage.Create;    try       //load the jpg here       st := qPicture.CreateBlobStream(qPicture.FieldByName('PICTURE'), bmWrite);       JPG.SaveToStream(st);    finally      JPG.Free;    end; end You don't have to free st...it will be freed internally. ---------------------------------------------------------------------- goldfish999 · 2015-02-14 05:27 Thanks for that but I have always been afraid to mess with Streams. This is what I have interpreted your code to:- procedure TFrmGardenA.BitBtnSavePictureClick(Sender: TObject); var   st: TStream;       JPEG:  TJPEGImage; begin         JPEG := TJPEGImage.Create;         try            //load the jpg here //           st := qPicture.CreateBlobStream(qPicture.FieldByName('PICTURE'), bmWrite);            st := DM1.ZTableVariety.CreateBlobStream(DM1.ZTableVariety. FieldByName('PICTURE'), bmWrite);            JPEG.SaveToStream(st);         finally           JPEG.Free;         end; end; I guess I got it wrong because nothing happens. I don't even get an error message. Where have I gone wrong.  As you see I don't understand streams Thanks ---------------------------------------------------------------------- fpcoder · 2015-02-14 05:54 "TStream is the base (abstract) class type for stream objects. Being abstract means that TStream should never be used as such." Try TMemoryStream instead. http://delphi.about.com/od/vclusing/l/aa110803a.htm ---------------------------------------------------------------------- balazsszekely · 2015-02-14 06:58 Hi all, Quote@kapibara TStream is the base (abstract) class type for stream objects. Being abstract means that TStream should never be used as such. Try TMemoryStream instead. TStream has a lot of virtual methods, which are implemented in the child class(Read, Write, Seek, GetPosition, etc). Since you don't want to access those method, in this particular case TStream will do just fine, even CreateBlobStream returns a TStream.  But yes, generally speaking you're right, TStream is indeed an abstract class, so you can replace it with TBlobStream(TMemoryStream) if you like, but that's not the reason why the save fails! QuoteI guess I got it wrong because nothing happens. I don't even get an error message. Where have I gone wrong.  As you see I don't understand streams What is ZTableVariety? A TZTable(Zeos)? You should assign a connection to it. After you saved the image, post the changes(which is a logical transaction-->visible to other connections, but not permanently saved to database).  After the post, call ZConnection1.Commit method to permanently save the data. Something like this: Code: [Select]procedure TFrmGardenA.BitBtnSavePictureClick(Sender: TObject); var    st: TStream;    JPEG:  TJPEGImage; begin     JPEG := TJPEGImage.Create;     try        //load the jpg here        st := DM1.ZTableVariety.CreateBlobStream(DM1.ZTableVariety.Fiel dByName('PICTURE'), bmWrite);        JPEG.SaveToStream(st);     finally       JPEG.Free;     end;     DM1.ZTableVariety.Post; //<--logical transaction     DM1.ZTableVariety.ApplyUpdates; //<-- some database components require this line,  I don't know about zeos(I personally never used)     ZConnection1.Commit; //permanently save the changes end; regards, GetMem ---------------------------------------------------------------------- goldfish999 · 2015-02-14 07:49 Ok tried to do that. Looks like this now. procedure TFrmGardenA.BitBtnSavePictureClick(Sender: TObject); var   st: TStream;       JPEG:  TJPEGImage; begin             JPEG := TJPEGImage.Create;             try //load the jpg here                         DM1.ZTableVariety.Edit ;                         st := DM1.ZTableVariety.CreateBlobStream(DM1.Z TableVariety.FieldByName('PICTURE'), bmWrite);                         JPEG.SaveToStream(st);                 DM1.ZTableVariety.Post; //<--logical transaction                   DM1.ZTableVariety.ApplyUpdates; //<-- some database components require this line,                           //I don't know about zeos(I personally never used)                   DM1.ZTableVariety.CommitUpdates; //permanently save the changes        finally             finally                         JPEG.Free;             end;             JPEG.Free; end; Now I get this error:=  Project raised exception class 'External: SIGSEGV' ---------------------------------------------------------------------- balazsszekely · 2015-02-14 07:57 You free JPEG twice....and please use the code tag. [ code ]Enter your code here [ /code ] Just remove the spaces inside []. ---------------------------------------------------------------------- goldfish999 · 2015-02-14 13:02 Ive removed the second ' JPEG.Free' but nothing happens now.  No Errors but the picture is not saved and the DBImage is cleared and goes blank.  Im sorry but im not very sure about what ive done wrong ---------------------------------------------------------------------- goldfish999 · 2015-02-14 13:16 ZTableVariety is just a table with the Various Details about a specific variety of plant (ie name, common name , height etc and I want to add pictures of the plant ---------------------------------------------------------------------- hallenberger · 2015-02-14 13:18 Hello! Here is my way to save pictures into a firebird table: Code: [Select]Type   { TCBild }   TCBild = Class(TObject)   Private     Fangelegt: Tdatetime;     Fautor: String;     Fbstichwort: TStringList;     Fbstichworttabelle: String;     Fbtabelle: String;     Fdatei: Tmemorystream;     Fid: Int64;     Ftabstwbild: String;     Procedure Setangelegt(Const Avalue: Tdatetime);     Procedure Setautor(Const Avalue: String);     Procedure Setbstichwort(Const Avalue: TStringList);     Procedure Setbstichworttabelle(Const Avalue: String);     Procedure Setbtabelle(Const Avalue: String);     Procedure Setdatei(Const Avalue: Tmemorystream);     Procedure Setid(Const Avalue: Int64);     Procedure Settabstwbild(Const Avalue: String);   Public ....     Property Datei: tmemorystream read FDatei write SetDatei;     Procedure Save2DB; ...     Constructor Create;     Destructor Destroy;       Override;   End; Implementation ... Procedure Tcbild.Save2db; Var   Sql: String; Begin   Try     Sql := 'insert into ' + Fbtabelle + ' (autor, bild)' +       ' values(:autor, :bild) returning id;';     dm.tFSBild.StartTransaction;     dm.qFSBild.sql.Text := sql;     dm.qFSBild.Params.ByNameAsString['autor'] := UTF8Decode(Autor);     dm.qFSBild.ParamsSetBlob('bild', Fdatei);     dm.qFSBild.Execute;     ID := dm.qFSBild.fields.AsInt64[0];     dm.tFSBild.Commit;   Except     on E: Exception Do     Begin       dm.tFSBild.RollBack;       ShowMessage(E.Message);     End;   End; End; regards tom