filename := ExtractFilePath(application.ExeName) + a.jpg';
Image1.Picture.LoadFromFile(filename);
Image1.Height := Image1.Picture.Height;
Image1.Width := Image1.Picture.Width;
当文件实际和jpg文件不相同(后缀确实是jpg的),就会出错,如何当Image1无法显示时进行错误的处理,防止弹出错误信息,如error 52之类的,谢谢!

解决方案 »

  1.   

    就是
    try
     filename := ExtractFilePath(application.ExeName) + a.jpg'; 
     Image1.Picture.LoadFromFile(filename); 
     Image1.Height := Image1.Picture.Height; 
     Image1.Width := Image1.Picture.Width;
    except
     。。该写什么?
    end; 
      

  2.   

    那就要先读取图片的文件头,进行判断, 
    JPEG   文件开头必须为   FF   D8,文件尾必须为   FF   D9 
     
    function IsJpegFile(FileName: string): Boolean;
    const
    RightBuf : array[0..3] of Byte = ($FF,$D8,$FF,$D9);
    var
    Buf: array[0..3] of Byte;
    begin
    FillChar(Buf, 4, 0);
    with TFileStream.Create(FileName, 0) do begin
        Position := 0;
        ReadBuffer(Buf[0], 2);
        Position := Size-2;
        ReadBuffer(Buf[2], 2);
        Free;
    end;
    Result := CompareMem(@RightBuf[0], @Buf[0], 4);
    end;
      

  3.   

    try 
    filename := ExtractFilePath(application.ExeName) + a.jpg'; 
    Image1.Picture.LoadFromFile(filename); 
    Image1.Height := Image1.Picture.Height; 
    Image1.Width := Image1.Picture.Width; 
    except 
     Application.MessageBox('This file is not load!','Error!',MB_OK); //该写什么? 
    end; 
      

  4.   

    First chance exception at $7C812AFB. Exception class EJPEG with message 'JPEG error #53'. Process myreader.exe (2104)还是会出现告警,然后才弹出消息
      

  5.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Buttons, ExtCtrls,jpeg;//可有引用JPEG?
      

  6.   

    to de410:引用JPEG了,实际程序运行时还会出错的,不知道什么原因
      

  7.   

    是delphi2010版本的,d7的不知道有没有这种情况
      

  8.   

    Delphi中判断一个文件是不是JPG图片文件
    2009-7-3 15:00:36 来源: 转载 作者:LihuaSoft 访问:165 次 被顶:3 次 字号:【大 中 小】
    核心提示:根据扩展名判断,只是一种简单方法.下面是根据文件头和文件尾判断....function IsJpegFile(FileName: string): Boolean;
    const
    RightBuf : array[0..3] of Byte = ($FF,$D8,$FF,$D9);
    var
    Buf: array[0..3] of Byte;
    begin
    FillChar(Buf, 4, 0);
    with TFileStream.Create(FileName, 0) do begin
        Position := 0;
        ReadBuffer(Buf[0], 2);
        Position := Size-2;
        ReadBuffer(Buf[2], 2);
        Free;
    end;
    Result := CompareMem(@RightBuf[0], @Buf[0], 4);
    end;
    procedure TForm1.Button1Click(Sender: TObject);//测试
    begin
    if Self.OpenDialog1.Execute then
        if IsJpegFile(Self.OpenDialog1.FileName) then
          Showmessage('Is Jpg File');
    end;
    本文来自Delphi之窗,原文地址:http://www.52delphi.com
      

  9.   

    经测试Delphi2010 Image控件不能读取*.jpg文件,但读取 *.BMP是可以的,不会出错.
    如果要读取*.Jpg等其它类型的图片文件,可以使用 TWICImage类WicImg := TWICImage.Create;
    WicImg.LoadFromFile('c:\temp\test.jpg');