Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

JPEG

How can I verify a JPEG file is valid or not ? Because it will halt if I pass a file name of invalid JPEG file to LoadFromFile, so, how can I prevent it halt again !

Andrew
0
andrewyu
Asked:
andrewyu
1 Solution
 
v_pasupuletiCommented:
Hi Andrew
Use a try exception in ur code.Put ur Loadfromfile in the try block and a default jpg image in the exception block

Vijay
0
 
EmmDiehCommented:
A JPEG file contains block markers followed by a block of a certain type. For example a jpeg file will start with the two byte FF D8 and end with the two byte FF D9. Sometimes one ore more extra byte are added to the end of the file due to bugs in transfer software. These files still display but you can delete the extra byte form the end. One time I had a file with more than 1000 extra byte.
I use the following function:
function IsJPEGFileOK(aFileName: string): Boolean;
var
  fs: tFileStream;
  JPEGSign: array[1..2] of Byte;
begin
  Result := false;
  fs := tFileStream.Create(aFileName, fmOpenRead);
  try
    try
        { Check for JPEG-File Beginnmarker }
      fs.Seek(0, soFromBeginning);
      fs.ReadBuffer(JPEGSign, 2);
      Result := (JPEGSign[1] = $FF) and (JPEGSign[2] = $D8);  { $FFD8 }
      if not Result then Exit;
        { Check for JPEG-File Endmarker }
      fs.Seek(-2, soFromEnd);
      fs.ReadBuffer(JPEGSign, 2);
      Result := (JPEGSign[1] = $FF) and (JPEGSign[2] = $D9);  { $FFD9 }
    except
      on E: Exception do
        ErrorMessage('IsJPEGFileOK', E.Message);
    end;
  finally
    fs.Free;
  end;
end;   { IsJPEGFileOK }

0
 
andrewyuAuthor Commented:
I understand, but, actually I want to know whether Delphi will raise a exception for a "BUG" JPEG file ?

Andrew
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
bryan7Commented:
use      with jpgimage     try jpgxx.loadfromfil...

bryan
0
 
bryan7Commented:
what kind of bugs ?

bryan
0
 
andrewyuAuthor Commented:
I meant a worong content of JPEG file !

Andrew
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now