[Delphi (Object Pascal)] Delphi 返回Gif图片的尺寸 →→→→→进入此内容的聊天室

来自 , 2021-03-17, 写在 Delphi (Object Pascal), 查看 130 次.
URL http://www.code666.cn/view/81507461
  1. function GetGIFSize(const FileName: string): Types.TSize;
  2. type
  3.   // GIF header record
  4.   TGIFHeader = packed record
  5.     Sig: array[0..5] of AnsiChar;     // signature bytes
  6.     ScreenWidth, ScreenHeight: Word;  // logical screen width and height
  7.     Flags: Byte;                      // various flags
  8.     Background: Byte;                 // background colour index
  9.     Aspect: Byte;                     // pixel aspect ratio
  10.   end;
  11.   // GIF image block header record
  12.   TGIFImageBlock = packed record
  13.     Left, Top: Word;      // image top left
  14.     Width, Height: Word;  // image dimensions
  15.     Flags: Byte;          // flags and local colour table size
  16.   end;
  17. const
  18.   cSignature: PAnsiChar = 'GIF';  // gif image signature
  19.   cImageSep = $2C;                // image separator byte
  20. var
  21.   FS: Classes.TFileStream;      // stream onto gif file
  22.   Header: TGIFHeader;           // gif header record
  23.   ImageBlock: TGIFImageBlock;   // gif image block record
  24.   BytesRead: Integer;           // bytes read in a block read
  25.   Offset: Integer;              // file offset to seek to
  26.   B: Byte;                      // a byte read from gif file
  27.   DimensionsFound: Boolean;     // flag true if gif dimensions have been read
  28. begin
  29.   Result.cx := 0;
  30.   Result.cy := 0;
  31.   if (FileName = '') or not SysUtils.FileExists(FileName) then
  32.     Exit;
  33.   FS := Classes.TFileStream.Create(
  34.     FileName, SysUtils.fmOpenRead or SysUtils.fmShareDenyNone
  35.   );
  36.   try
  37.     // Check signature
  38.     BytesRead := FS.Read(Header, SizeOf(Header));
  39.     if (BytesRead <> SizeOf(TGIFHeader)) or
  40.       (SysUtils.StrLComp(cSignature, Header.Sig, 3) <> 0) then
  41.       // Invalid file format
  42.       Exit;
  43.     // Skip colour map, if there is one
  44.     if (Header.Flags and $80) > 0 then
  45.     begin
  46.       Offset := 3 * (1 shl ((Header.Flags and 7) + 1));
  47.       if Offset >= FS.Size then
  48.         Exit;
  49.       FS.Seek(Offset, Classes.soFromBeginning);
  50.     end;
  51.     DimensionsFound := False;
  52.     FillChar(ImageBlock, SizeOf(TGIFImageBlock), #0);
  53.     // Step through blocks
  54.     FS.Read(B, SizeOf(B));
  55.     while (FS.Position < FS.Size) and (not DimensionsFound) do
  56.     begin
  57.       if B = cImageSep then
  58.       begin
  59.         // We have an image block: read dimensions from it
  60.         BytesRead := FS.Read(ImageBlock, SizeOf(ImageBlock));
  61.         if BytesRead <> SizeOf(TGIFImageBlock) then
  62.           // Invalid image block encountered
  63.           Exit;
  64.         Result.cx := ImageBlock.Width;
  65.         Result.cy := ImageBlock.Height;
  66.         DimensionsFound := True;
  67.       end;
  68.       FS.Read(B, SizeOf(B));
  69.     end;
  70.   finally
  71.     FS.Free;
  72.   end;
  73. end;
  74. //delphi/2246

回复 "Delphi 返回Gif图片的尺寸"

这儿你可以回复上面这条便签

captcha