[SOLVED] Trying to block error dialog and do exception code, but can't lazarus / 16-Databases ---------------------------------------------------------------------- Elmug · 2012-07-21 06:07 Hi everyone, I thought I could maybe handle either .bmp or .jpg using a Try/Except block, but can't seem to get it to work. As shown below, the error dialog pops and the error has control, with no chance to the Except code, when the image is .bmp and would like to handle it with the except code. Try   Image1.Picture.Graphic:= TJpegImage.Create; Except   Image1.Picture.Graphic:= TBitMap.Create; End;    Image1.Picture.Graphic.LoadFromStream(BS); Would appreciate help in solving this, if possible. Thanks! ---------------------------------------------------------------------- taazz · 2012-07-21 07:15 Create does not raise any error, loadfromstream does you need to put that method in a try except and not the create. ---------------------------------------------------------------------- KpjComp · 2012-07-21 09:26 Like @taazz pointed out, it's the loading that will be raising the exception. But you should not really be using Try/Except blocks in this way. What's I've done in the past is examine the first few bytes of the the file.  I have some code at work I could dig up, I think it did JPEG/BMP/PNG detection.  It's the weekend now, so won't be at work till Monday, if you can wait I'll dig up the detection algorithm.  It's not very complicated, it usually involves just seeing if the bytes at XYZ contain ABC sort of thing. ---------------------------------------------------------------------- Elmug · 2012-07-21 14:12 Hi everyone, again, just to report this (I believe important reach): My application now DOES handle either JPEG or BitMap images in the same column, Blob-Type, without having to regard imagefile extension, nor having to decipher Blob-headers. In my application, the instructions will be to use only .bmp or JPEG files, which are loaded into the database by means of the loadpicture dialog. The loading of the image file it is known, does not discriminate: can load JPEG or .bmp by same code. These two image types, for me, are the most popular here. But the idea can possibly be extended to a few other types if needed, I believe. The usual problem has been, not the loading, but the SHOWING the Blob image when the Blob column can store Blobs of JPEG or .bmp files. Of course, the idea can be extended to include other image formats. The solution achieved is using Exception coding as follows: Code: [Select]// var //  BlobField: TField; //  BS: TStream;   with SQLQuery1 do     begin        BlobField := FieldByName('Pic'); {'Pic' is name of column with photo}        BS := CreateBlobStream(BlobField,bmRead);        Image1.Picture.Graphic:= TJpegImage.Create; {assume is Jpeg}     Try        Image1.Picture.Graphic.LoadFromStream(BS); {error if not Jpeg}       Except {repeat steps for BitMap}          BS.Free;          Image1.Picture.Graphic:= nil; {empty}          BlobField := FieldByName('Pic'); {'Pic' is name of column with photo}          BS := CreateBlobStream(BlobField,bmRead);          Image1.Picture.Graphic:= TBitMap.Create; {bitmap}          Image1.Picture.Graphic.LoadFromStream(BS);       end; {Try}       BS.Free;     end; {with SQLQuery} Thanks to taazz for the very good observation on WHERE to do the exception, and to all others as well.