Home > Back-end >  Seek a read TMemoryStream binary stream header files, to determine the code file type
Seek a read TMemoryStream binary stream header files, to determine the code file type

Time:09-26

Seek a read TMemoryStream binary stream header files, to determine the sample code file type, thank you

CodePudding user response:

The following content is back, the image file header format:
1. The JPEG
- file header identifiers (2 bytes) : $ff, $d8 (SOI) (JPEG file id)
- end of file identifier (2 bytes) : $ff, $d9 (EOI)

2. The TGA
- 5 bytes uncompressed before 00 00 00 00 02
- before 5 bytes of RLE compression 10 00 00 00 00

3. The PNG
- file header identifiers (8 bytes) 50 89 e 47 0 d 0 a 1 a 0 a

4. GIF
47 - file head logo (6 bytes) 49 46 38 and 39 (37) 61
G I F 8 9 (7) a

5. BMP
- file head logo (2 bytes) 42 4 d
B M

6. The PCX
- file head logo (1 bytes) 0 a

7. TIFF
Mark - file header (2 bytes) 4 d, 4 d or 49 49

8. ICO
Mark - file header (8 bytes) 00 00 01 00 01 00 20 20

9. CUR
Mark - file header (8 bytes) 00 00 00 02 01 00 20 20

10. IFF
Mark - file header (4 bytes) 46 4 f and 4 d
F O R M

11. ANI
Mark - file header (4 bytes) 52 49 46 46
R I F F


 myimage.png:=TMemoryStream. Create; 
Myimage.png LoadFromFile (FileName);//put just the user to select the file is loaded into memory stream
Myimage.png Position:=0;//move the pointer to the beginning place
Myimage.png ReadBuffer (Buffer, 2);//read the file before 2 bytes, into the Buffer inside
If Buffer=$4 d42 then//if the first two bytes are 4 d42/low to high (because memory is from high to low, from left to right in front of the so said BMP file header is $424 d, will in turn used to when compared with the data in the memory, compared with $4 d42
The begin
ShowMessage (' BMP);//this is BMP format files
End

CodePudding user response:

Want to see more of the contents of the file header, look at this website: http://www.garykessler.net/library/file_sigs.html

CodePudding user response:

This paper explained how to determine a real type of image files, not is judged by the suffix types of methods:

The following is a reference:

 unit Unit55; 

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

Type
TImageType=(IT_None, IT_Error IT_Bmp, IT_JPEG, IT_GIF, IT_PCX, IT_PNG,
IT_PSD IT_RAS, IT_SGI IT_TIFF);

TForm55=class (TForm)
OpenDialog1: TOpenDialog;
For: TButton;
Procedure Button1Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form55: TForm55;

Implementation

{$R *. DFM}

The function CheckImageType (FileName: string) : TImageType;
Var
Myimage.png: TMemoryStream;
Buffer: Word;
The begin
Myimage.png:=TMemoryStream. Create;
Try
Myimage.png LoadFromFile (FileName);
Myimage.png Position:=0;
If myimage.png Size=0 then//if the file Size is equal to zero, then the error (
The begin
Result:=IT_Error;
The Exit;
end;
Myimage.png ReadBuffer (Buffer, 2);//read the file before 2 bytes, into the Buffer inside

Case Buffer of
$4 d42:
Result:=IT_Bmp;
$D8FF:
Result:=IT_JPEG;
$4947:
Result:=IT_GIF;
A: $050
Result:=IT_PCX;
$5089:
Result:=IT_PNG;
$4238:
Result:=IT_PSD;
$A659:
Result:=IT_RAS;
$DA01:
Result:=IT_SGI;
$4949:
Result:=IT_TIFF;
The else
Result:=IT_None;
end;
The finally
Myimage.png Free;
end;
end;

Procedure TForm55. Button1Click (Sender: TObject);
The begin
If not OpenDialog1. Execute then
The Exit;
If CheckImageType (OpenDialog1 FileName) & lt;> IT_Bmp then
Showmessage (' is not a BMP format);

end;

End.
  • Related