问一个关于TImage和Stream的简单问题!(200分)

  • 主题发起人 主题发起人 www
  • 开始时间 开始时间
W

www

Unregistered / Unconfirmed
GUEST, unregistred user!
现在通过stream传送图像,格式可能是bmp,ico,gif或其他,用流发送给接收端,
接收端收到这个stream之后,不保存临时文件,如何让image把stream中的图像显示出来.
(好像image没有loadfromstream方法呀).
 
image.picture.bitmap.loadfromstream呢?
 
gif的怎么显示?
 
BMP,ICO可以直接Load,但是其它格式就行了,要转换一下
以GIF为例
gif:TGifImage;
bmp:TBitmap;
begin
try
gif:=TgifImage.Create;
bmp:=TBitmap.create;
gif.loadFromStream(stream);
bmp.assign(gif);
Image.Picture.assign(bmp);
finally
bmp.free;
gif.free;
end
end;
 
关键问题是我得到stream后怎么知道是bmp还是gif,还是ico呢??
 
看文件头
 
得到stream后分别按bmp,gif,ico处理一下,哪个成功了就做个标记;显示出来
 
根据文件头可以知道是什么图片。建立对应的图类。
 
看文件头愿书的前几个字节,好像是4-8个字节中可以大体上确认是什么文件(仅这几种图像文件)
 
class function TGGraphicUtils.GetImageStreamType(AImageStream: TStream;
ASpeedGet: Boolean): TGImageStreamType;
const
cSBMPCodeStr = '424D';
cSICOCodeStr = '000001';
cSJPGCodeStr = 'FFD8FFE000104A464946';
cSGIFCodeStr = '474946';
cSWMFCodeStr = 'D7CDC69A';
cSTIFCodeStr = '49492A0008';
var
vCode: array [0..9] of Char;
vCodeStr: string;
I, vOldPos: Integer;
begin
if ASpeedGet then
begin
vOldPos := AImageStream.Position;
AImageStream.Position := 0;
AImageStream.ReadBuffer(vCode, 1);
vCodeStr := IntToHex(Byte(vCode[0]), 2);
if vCodeStr = '42' then Result := istBMP
else if vCodeStr = '00' then Result := istICO
else if vCodeStr = 'FF' then Result := istJPG
else if vCodeStr = '47' then Result := istGIF
else if vCodeStr = 'D7' then Result := istWMF
else if vCodeStr = '49' then Result := istTIF
else Result := istCustom;
AImageStream.Position := vOldPos;
end
else begin
vOldPos := AImageStream.Position;
AImageStream.Position := 0;
AImageStream.ReadBuffer(vCode, 10);
for I := 0 to 9 do
vCodeStr := vCodeStr + IntToHex(Byte(vCode), 2);
if Pos(cSBMPCodeStr, vCodeStr) = 1 then Result := istBMP
else if Pos(cSICOCodeStr, vCodeStr) = 1 then Result := istICO
else if Pos(cSJPGCodeStr, vCodeStr) = 1 then Result := istJPG
else if Pos(cSGIFCodeStr, vCodeStr) = 1 then Result := istGIF
else if Pos(cSWMFCodeStr, vCodeStr) = 1 then Result := istWMF
else if Pos(cSTIFCodeStr, vCodeStr) = 1 then Result := istTIF
else Result := istCustom;
AImageStream.Position := vOldPos;
end;
end;
 
http://www.moon-soft.com/program/FORMAT/
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部