Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 How to avoid Random Exceptions being raised when using TJPEGImage or the JPEG unit.


Sometimes when I try to load a JPEG into a TImage, I get an exception message even though I wrap the LoadFromFile call in a try...except. How can I avoid such exceptions, and why do they happen?   

    When loading a JPEG from a file, sometimes it will load fine, but contain a corrupt JPEG image. When an attempt is made to draw the image, an exception will be raised. Quite often, the programmer may try to suppress errors with a procedure such as:

    procedure TForm .btnOpenDefaultClick(Sender: TObject);
    begin
      if OpenPictureDialog .Execute then
      try
        Image .Picture.LoadFromFile(OpenPictureDialog .FileName);
      except end;
    end;
    

    But, unexpectedly, JPEG's sometimes display an exception message and it appears that the creator of the JPEG unit hard-coded a ShowMessage to display the exception. The problem is that the

    LoadFromFile
    procedure executes fine, and the actual drawing of the JPEG to the TImage is what is causing the problem. The drawing of the image is implicitly called after the image has loaded from the file.

    The way around this problem is to explicitly control when any possible errors happen. You can capture any errors that happen in the loading of the image, and/or in the drawing of the image.

    Below is an example of how to do this. The project consists of a unit (called MainFrm.pas). It has a TImage and an OpenPictureDialog placed on the form. Two buttons are also placed on it, one named

    btnOpenCatch
    and another called
    btnOpenDefault
    . The
    btnOpenCatchClick
    procedure demonstrates how each exception can be caught and displayed the the user. Alternatively, the exception could be "eaten" with an empty
    except
    section. The
    btnOpenDefaultClick
    displays the default way of opening an image and trying to eat an exception, which may still display a JPEG error. Also notice the addition of the JPEG unit to the uses clause.

    To create a corrupt JPEG that displays this error, open a JPEG image in a hex editor and change the first few bytes or the last few bytes.

    unit MainFrm;

    interface

    uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtDlgs, ExtCtrls, StdCtrls, JPEG;

    type TForm = class(TForm) btnOpenCatch: TButton; Image : TImage; OpenPictureDialog : TOpenPictureDialog; btnOpenDefault: TButton; procedure btnOpenCatchClick(Sender: TObject); procedure btnOpenDefaultClick(Sender: TObject); private { Private declarations } public { Public declarations } end;

    var Form : TForm ;

    implementation

    {$R *.DFM}

    procedure TForm .btnOpenCatchClick(Sender: TObject); var Picture: TPicture; Bitmap: TBitmap; begin if OpenPictureDialog .Execute then begin Picture := TPicture.Create; try // Try to load the picture from a file. // I'm wrapping it in a try..except to capture // any exceptions that happen in loading the image from // a stream. try Picture.LoadFromFile(OpenPictureDialog .FileName); except on E: EInvalidGraphic do begin ShowMessage('Capturing Load Error: ' + E.Message); Exit; // Exit the procedure end; end; Bitmap := TBitmap.Create; try try // Assign the Graphic to the Bitmap; any drawing errors/exceptions // will happen here, and will be captured and shown to the user Bitmap.Assign(Picture.Graphic); except // Eat invalid graphic messages (which may be raised // when a jpeg is being drawn) on E: EInvalidGraphic do begin ShowMessage('Capturing Assign (Drawing) Error: ' + E.Message); Exit; // Exit the procedure end; end; // Now draw the picture on the screen's TImage Image .Picture.Bitmap.Assign(Bitmap); finally Bitmap.Free; end; finally Picture.Free; end; end; // OpenPictureDialog .Execute end;

    procedure TForm .btnOpenDefaultClick(Sender: TObject); begin if OpenPictureDialog .Execute then try Image .Picture.LoadFromFile(OpenPictureDialog .FileName); except end; end;

    end.

    Another thing to notice is that the OpenPictureDialog does not contain this fix. It will display some JPEG error messages when you click on a corrupt JPEG and it attempts to display a preview image.  



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use