Recent

Author Topic: [SOLVED] Trying to block error dialog and do exception code, but can't  (Read 2640 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
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!
« Last Edit: July 21, 2012, 04:12:52 pm by Elmug »

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: Trying to block error dialog and do exception code, but can't
« Reply #1 on: July 21, 2012, 09:15:42 am »
Create does not raise any error, loadfromstream does you need to put that method in a try except and not the create.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Trying to block error dialog and do exception code, but can't
« Reply #2 on: July 21, 2012, 11:26:32 am »
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

  • Hero Member
  • *****
  • Posts: 849
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.

« Last Edit: July 21, 2012, 04:35:59 pm by Elmug »