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之类的,谢谢!
Image1.Picture.LoadFromFile(filename);
Image1.Height := Image1.Picture.Height;
Image1.Width := Image1.Picture.Width;
当文件实际和jpg文件不相同(后缀确实是jpg的),就会出错,如何当Image1无法显示时进行错误的处理,防止弹出错误信息,如error 52之类的,谢谢!
try
filename := ExtractFilePath(application.ExeName) + a.jpg';
Image1.Picture.LoadFromFile(filename);
Image1.Height := Image1.Picture.Height;
Image1.Width := Image1.Picture.Width;
except
。。该写什么?
end;
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;
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;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls,jpeg;//可有引用JPEG?
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
如果要读取*.Jpg等其它类型的图片文件,可以使用 TWICImage类WicImg := TWICImage.Create;
WicImg.LoadFromFile('c:\temp\test.jpg');