{******************************************************************************* 功 能:用图像流填充图像组件; 参 数:[stream]图像数据流;[picture]图像组件; 返回值:[true]成功;[false]失败; 备 注:这里主要是判断图像的类型;[bmp,jpg,wmf,ico]; 判断的方法是检测数据流的前两个字节 *******************************************************************************} Function FillPicture(const Stream: TStream; Picture: TPicture): Boolean; var buf: array[0..1] of byte; jpg: TJPEGImage; begin // if Stream.Size<2 then begin Result:=false; Exit; end; // stream.Position:=0; stream.Read(buf,2); stream.Position:=0; if (buf[0]=$0) and (buf[1]=$0) then // .ico 00 00 Picture.Icon.LoadFromStream(Stream) else if (buf[0]=$42) and (buf[1]=$4d) then // .bmp 42 4D Picture.Bitmap.LoadFromStream(Stream) else if (buf[0]=$d7) and (buf[1]=$cd) then // .wmf D7 CD Picture.Metafile.LoadFromStream(Stream) else if (buf[0]=$ff) and (buf[1]=$d8) then // .jpg FF D8 begin jpg:=TJPEGImage.Create; try jpg.LoadFromStream(Stream); Picture.Assign(jpg); finally jpg.Free; end; end else begin Result:=false; Exit; End; // Stream.Free; //释放流对象; Result:=true; end;先用tfilestream加在文件,然后用这个函数填充,这个函数也可用于显示数据库中的图像流。
可以专门做了一个函数DGRAPHIC(distinguish graphic file's format)来识别这些文件: function dgraphic(filename:string):string; {This function can distinguish graphic file} type TIconHeader=record wReserved:word; wType:word; wCount:word; end; var ispicture:boolean; f:file of char; ch7,ch8,ch9,cha:char; i,j:integer; myicontemp,myicon:TIconHeader; keystring:string; begin ispicture:=false; assignfile(f,filename); reset(f); //start to distinguish if a .bmp file read(f,ch7); read(f,ch8); keystring:=ch7+ch8; if keystring='BM' then ispicture:=true; //start to distinguish if a .jpeg file if not ispicture then begin reset(f); for i:=1 to 6 do read(f,ch7); read(f,ch7); read(f,ch8); read(f,ch9); read(f,cha); keystring:=ch7+ch8+ch9+cha; if keystring='JFIF' then ispicture:=true; end; {Distinguish if a .ico file} if not ispicture then begin reset(f); j:=0; repeat j:=j+1; BlockRead(f,myicontemp,sizeof(myicontemp),i); if j=1 then myicon:=myicontemp; j:=j+1; until i=0; i:=myicon.wType; if i=1 then begin ispicture:=true; keystring:='ICON' end; end; if not ispicture then begin reset(f); read(f,ch7); read(f,ch8); read(f,ch9); keystring:=ch7+ch8+ch9; if keystring='GIF' then ispicture:=true; end; closefile(f); if not ispicture then keystring:='NO'; dgraphic:=keystring; end;
但对你不太好用,扩展名可能太多!所以我觉的如果你不愿意研究下文件格式(其实就前面几个字节)!直接利用异常机制,先用TBitmap.LoadFromFile,出错再试TGifImage.LoadFromFile!
功 能:用图像流填充图像组件;
参 数:[stream]图像数据流;[picture]图像组件;
返回值:[true]成功;[false]失败;
备 注:这里主要是判断图像的类型;[bmp,jpg,wmf,ico];
判断的方法是检测数据流的前两个字节
*******************************************************************************}
Function FillPicture(const Stream: TStream; Picture: TPicture): Boolean;
var buf: array[0..1] of byte;
jpg: TJPEGImage;
begin
//
if Stream.Size<2 then
begin
Result:=false;
Exit;
end;
//
stream.Position:=0;
stream.Read(buf,2);
stream.Position:=0;
if (buf[0]=$0) and (buf[1]=$0) then // .ico 00 00
Picture.Icon.LoadFromStream(Stream)
else if (buf[0]=$42) and (buf[1]=$4d) then // .bmp 42 4D
Picture.Bitmap.LoadFromStream(Stream)
else if (buf[0]=$d7) and (buf[1]=$cd) then // .wmf D7 CD
Picture.Metafile.LoadFromStream(Stream)
else if (buf[0]=$ff) and (buf[1]=$d8) then // .jpg FF D8
begin
jpg:=TJPEGImage.Create;
try
jpg.LoadFromStream(Stream);
Picture.Assign(jpg);
finally
jpg.Free;
end;
end else begin
Result:=false;
Exit;
End;
//
Stream.Free; //释放流对象;
Result:=true;
end;先用tfilestream加在文件,然后用这个函数填充,这个函数也可用于显示数据库中的图像流。
也可,因为单元中已经包含将后缀注册到TPicture类的代码!
{This function can distinguish graphic file}
type TIconHeader=record
wReserved:word;
wType:word;
wCount:word;
end;
var ispicture:boolean; f:file of char; ch7,ch8,ch9,cha:char;
i,j:integer; myicontemp,myicon:TIconHeader; keystring:string;
begin
ispicture:=false;
assignfile(f,filename);
reset(f); //start to distinguish if a .bmp file read(f,ch7);
read(f,ch8);
keystring:=ch7+ch8;
if keystring='BM'
then ispicture:=true; //start to distinguish if a .jpeg file
if not ispicture then
begin
reset(f);
for i:=1 to 6 do read(f,ch7);
read(f,ch7);
read(f,ch8);
read(f,ch9);
read(f,cha);
keystring:=ch7+ch8+ch9+cha;
if keystring='JFIF' then ispicture:=true;
end;
{Distinguish if a .ico file}
if not ispicture then
begin
reset(f);
j:=0;
repeat
j:=j+1;
BlockRead(f,myicontemp,sizeof(myicontemp),i);
if j=1 then myicon:=myicontemp;
j:=j+1;
until i=0;
i:=myicon.wType;
if i=1 then
begin
ispicture:=true;
keystring:='ICON'
end;
end;
if not ispicture then
begin
reset(f);
read(f,ch7);
read(f,ch8);
read(f,ch9);
keystring:=ch7+ch8+ch9;
if keystring='GIF' then ispicture:=true;
end;
closefile(f);
if not ispicture then keystring:='NO';
dgraphic:=keystring;
end;