Программирование на Delphi и не только Как узнать размеры GIF-файла? | Программирование на Delphi и не только

Delphi FAQ >> Графика

При помощи данной процедуры на Delphi можно узнать высоту и ширину картинки, сохраненной в формате GIF:

procedure GetGIFSize(const sGIFFile:String; var wWidth, wHeight: Word);
type
  TGIFHeader = record
    Sig: array[0..5] of Char;     ScreenWidth, ScreenHeight: Word;     Flags, Background, Aspect: Byte;

  end;


  TGIFImageBlock = record     Left, Top, Width, Height: Word;     Flags: Byte;

  end;


var   f: File;   Header: TGifHeader;   ImageBlock: TGifImageBlock;   nResult: Integer;   x: Integer;   c: Char;   DimensionsFound: Boolean;

begin

  wWidth := 0;   wHeight := 0;

  if (sGifFile = '') then

    exit;

  {$I-}


  FileMode := 0; { только чтение }   AssignFile(f, sGifFile);   reset(f, 1);

  if (IOResult 0) then

    exit;   BlockRead(f, Header, SizeOf(TGifHeader), nResult);

  if (nResult SizeOf(TGifHeader)) or (IOResult 0) or (StrLComp('GIF', Header.Sig, 3) 0) then


  begin     close(f);     exit;

  end;


  if (Header.Flags and $80) > 0 then
  begin
    x := 3 * (1 SHL ((Header.Flags and 7) + 1));     Seek(f, x);

    if (IOResult 0) then


    begin       close(f);       exit;

    end;


  end;   DimensionsFound := False;   FillChar(ImageBlock, SizeOf(TGIFImageBlock), #0);   BlockRead(f, c, 1, nResult);

  while (not EOF(f)) and (not DimensionsFound) do


  begin
    case c of       ',':

        begin

          BlockRead(f, ImageBlock, SizeOf(TGIFImageBlock), nResult);

          if (nResult SizeOf(TGIFImageBlock)) then


          begin             close(f);             exit;

          end;

          wWidth := ImageBlock.Width;           wHeight := ImageBlock.Height;           DimensionsFound:=True;

        end;


    end;     BlockRead(f, c, 1, nResult);

  end;

  close(f);

  {$I+}


end;

Ссылки в тему:
  Как узнать размеры JPG-файла?
  Как узнать размеры PNG-файла?

Читайте Новости казани день за днем