找出文件类型(GIF,JPG,PNG)-DELPHI [英] Find out the type of file ( GIF, JPG, PNG) - DELPHI
问题描述
我每个URL都带有图像,并显示TImage.我将使用JPG,GIF和PNG.但是我不知道如何找出每个文件具有什么样的扩展名,然后加以区分.如何获取标头或其他让我知道文件类型的方法:GIF或PNG或JPG?
I'm carrying images per URL and showing TImage . I will work with JPG , GIF and PNG . But I do not know how to find out what kind of extension possess each file, and then differentiate. How do I get header , or any other method for me to know what type of file: GIF or PNG or JPG?
var
MS : TMemoryStream;
GIf: TGIFImage;
jpegimg: TJPEGImage;
begin
MS := TMemoryStream.Create;
GIf := TGIFImage.Create;
jpegimg := TJPEGImage.Create; ///////
try
try
IdHTTP1.get('http://forum.wmonline.com.br/uploads/av-8929.jpg',MS);
Ms.Seek(0,soFromBeginning);
//Gif.LoadFromStream(MS);
//Logo.Picture.Assign(GIF);
jpegimg.LoadFromStream(MS);
Logo.Picture.Assign(jpegimg);
except
ShowMessage('ERRO');
Exit;
end;
finally
FreeAndNil(GIF);
FreeAndNil(MS);
jpegimg.Free; ////
end;
推荐答案
有些机制旨在允许描述请求(或响应)的内容,但是任何外部元数据可能是不可靠的,完全取决于所涉及的元数据的准确实现和设置.在某些情况下,元数据可能不正确或完全丢失.
There are mechanisms intended to allow the description of the content of a request (or response), but any external meta-data may be unreliable, being wholly dependent upon an accurate implementation and setting of the meta-data involved. In some cases that meta-data may be incorrect or entirely missing.
幸运的是,与许多文件格式一样,您提到的图像文件类型的规范都要求使用特定的标头,以将文件(或流)标识为符合(或希望符合)相关规范.
Fortunately in common with many file formats, the specifications for the image file types you mention all mandate a specific header to identify the file (or stream) as conforming (or aspiring to conform) to the relevant specification.
`G` `I` `F` (ASCII)
您可能还希望检查随后的3个字节,以获取有效的GIF版本号,该版本号也以ASCII编码:
You may also wish to check the subsequent 3 bytes for a valid GIF version number, also encoded in ASCII:
`8` `9` `a` or `8` `7` `a`
137 80 78 71 13 10 26 10 (decimal)
JPEG fil的前2个字节 e是:
FF D8 (hex)
因此,要检测响应流中的数据格式,您仅需要检查流的前8个字节中是否有这些预期的标头值之一.
So to detect the format of the data in a response stream you need only inspect at most the first 8 bytes of the stream for one of these expected header values.
这篇关于找出文件类型(GIF,JPG,PNG)-DELPHI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!