[Delphi (Object Pascal)] Delphi获取JPG、GIF、PNG等格式图片的大小(高度和宽度) →→→→→进入此内容的聊天室

来自 , 2020-09-01, 写在 Delphi (Object Pascal), 查看 130 次.
URL http://www.code666.cn/view/b9acb4ae
  1. unit ImgSize;
  2.  
  3. interface
  4.  
  5. uses Classes;
  6.  
  7. procedure GetJPGSize(const sFile: string; var wWidth, wHeight: Word);
  8. procedure GetPNGSize(const sFile: string; var wWidth, wHeight: Word);
  9. procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: Word);
  10.  
  11. implementation
  12.  
  13. uses SysUtils;
  14.  
  15. function ReadMWord(f: TFileStream): Word;
  16. type
  17.   TMotorolaWord = record
  18.     case Byte of
  19.       0: (Value: Word);
  20.       1: (Byte1, Byte2: Byte);
  21.   end;
  22. var
  23.   MW: TMotorolaWord;
  24. begin
  25.   { It would probably be better to just read these two bytes in normally }
  26.   { and then do a small ASM routine to swap them.  But we aren't talking }
  27.   { about reading entire files, so I doubt the performance gain would be }
  28.   { worth the trouble. }
  29.   f.read(MW.Byte2, SizeOf(Byte));
  30.   f.read(MW.Byte1, SizeOf(Byte));
  31.   Result := MW.Value;
  32. end;
  33.  
  34. procedure GetJPGSize(const sFile: string; var wWidth, wHeight: Word);
  35. const
  36.   ValidSig: array[0..1] of Byte = ($FF, $D8);
  37.   Parameterless = [$01, $D0, $D1, $D2, $D3, $D4, $D5, $D6, $D7];
  38. var
  39.   Sig: array[0..1] of byte;
  40.   f: TFileStream;
  41.   x: integer;
  42.   Seg: byte;
  43.   Dummy: array[0..15] of byte;
  44.   Len: word;
  45.   ReadLen: LongInt;
  46. begin
  47.   FillChar(Sig, SizeOf(Sig), #0);
  48.   f := TFileStream.Create(sFile, fmOpenRead);
  49.   try
  50.     ReadLen := f.read(Sig[0], SizeOf(Sig));
  51.  
  52.     for x := Low(Sig) to High(Sig) do
  53.       if Sig[x] <> ValidSig[x] then ReadLen := 0;
  54.  
  55.     if ReadLen > 0 then
  56.     begin
  57.       ReadLen := f.read(Seg, 1);
  58.       while (Seg = $FF) and (ReadLen > 0) do
  59.       begin
  60.         ReadLen := f.read(Seg, 1);
  61.         if Seg <> $FF then
  62.         begin
  63.           if (Seg = $C0) or (Seg = $C1) then
  64.           begin
  65.             ReadLen := f.read(Dummy[0], 3); { don't need these bytes }
  66.             wHeight := ReadMWord(f);
  67.             wWidth  := ReadMWord(f);
  68.           end  
  69.           else  
  70.           begin
  71.             if not (Seg in Parameterless) then
  72.             begin
  73.               Len := ReadMWord(f);
  74.               f.Seek(Len - 2, 1);
  75.               f.read(Seg, 1);
  76.             end  
  77.             else
  78.               Seg := $FF; { Fake it to keep looping. }
  79.           end;
  80.         end;
  81.       end;
  82.     end;
  83.   finally
  84.     f.Free;
  85.   end;
  86. end;
  87.  
  88. procedure GetPNGSize(const sFile: string; var wWidth, wHeight: Word);
  89. type
  90.   TPNGSig = array[0..7] of Byte;
  91. const
  92.   ValidSig: TPNGSig = (137,80,78,71,13,10,26,10);
  93. var
  94.   Sig: TPNGSig;
  95.   f: tFileStream;
  96.   x: integer;
  97. begin
  98.   FillChar(Sig, SizeOf(Sig), #0);
  99.   f := TFileStream.Create(sFile, fmOpenRead);
  100.   try
  101.     f.read(Sig[0], SizeOf(Sig));
  102.     for x := Low(Sig) to High(Sig) do
  103.       if Sig[x] <> ValidSig[x] then Exit;
  104.     f.Seek(18, 0);
  105.     wWidth := ReadMWord(f);
  106.     f.Seek(22, 0);
  107.     wHeight := ReadMWord(f);
  108.   finally
  109.     f.Free;
  110.   end;
  111. end;
  112.  
  113. procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: Word);
  114. type
  115.   TGIFHeader = record
  116.     Sig: array[0..5] of char;
  117.     ScreenWidth, ScreenHeight: Word;
  118.     Flags, Background, Aspect: Byte;
  119.   end;
  120.  
  121.   TGIFImageBlock = record
  122.     Left, Top, Width, Height: Word;
  123.     Flags: Byte;
  124.   end;
  125. var
  126.   f: file;
  127.   Header: TGifHeader;
  128.   ImageBlock: TGifImageBlock;
  129.   nResult: integer;
  130.   x: integer;
  131.   c: char;
  132.   DimensionsFound: boolean;
  133. begin
  134.   wWidth  := 0;
  135.   wHeight := 0;
  136.  
  137.   if sGifFile = '' then
  138.     Exit;
  139.  
  140.   {$I-}
  141.   FileMode := 0;   { read-only }
  142.   AssignFile(f, sGifFile);
  143.   reset(f, 1);
  144.   if IOResult <> 0 then
  145.     { Could not open file }
  146.     Exit;
  147.  
  148.   { Read header and ensure valid file. }
  149.   BlockRead(f, Header, SizeOf(TGifHeader), nResult);
  150.   if (nResult <> SizeOf(TGifHeader)) or (IOResult <> 0) or
  151.     (StrLComp('GIF', Header.Sig, 3) <> 0) then
  152.   begin
  153.     { Image file invalid }
  154.     Close(f);
  155.     Exit;
  156.   end;
  157.  
  158.   { Skip color map, if there is one }
  159.   if (Header.Flags and $80) > 0 then
  160.   begin
  161.     x := 3 * (1 shl ((Header.Flags and 7) + 1));
  162.     Seek(f, x);
  163.     if IOResult <> 0 then
  164.     begin
  165.       { Color map thrashed }
  166.       Close(f);
  167.       Exit;
  168.     end;
  169.   end;
  170.  
  171.   DimensionsFound := False;
  172.   FillChar(ImageBlock, SizeOf(TGIFImageBlock), #0);
  173.   { Step through blocks. }
  174.   BlockRead(f, c, 1, nResult);
  175.   while (not EOF(f)) and (not DimensionsFound) do
  176.   begin
  177.     case c of
  178.       ',': { Found image }
  179.         begin
  180.           BlockRead(f, ImageBlock, SizeOf(TGIFImageBlock), nResult);
  181.           if nResult <> SizeOf(TGIFImageBlock) then  
  182.           begin
  183.             { Invalid image block encountered }
  184.             Close(f);
  185.             Exit;
  186.           end;
  187.           wWidth := ImageBlock.Width;
  188.           wHeight := ImageBlock.Height;
  189.           DimensionsFound := True;
  190.         end;
  191.       'Y': { Skip }
  192.         begin
  193.           { NOP }
  194.         end;
  195.       { nothing else.  just ignore }
  196.     end;
  197.     BlockRead(f, c, 1, nResult);
  198.   end;
  199.   Close(f);
  200.   {$I+}
  201. end;
  202.  
  203. end.
  204. //调用方法
  205.  
  206. procedure TForm1.Button1Click(Sender: TObject);
  207. var
  208. W, H: Word;
  209. sFileName:string;
  210. begin
  211. sFileName:='C:\test\test.jpg';
  212. GetJPEGSize(sFileName, W, H;
  213. showmessage(Format('Yes,W:%d,H:%d', [W, H]));
  214. end;
  215. end;
  216. //delphi/8990

回复 "Delphi获取JPG、GIF、PNG等格式图片的大小(高度和宽度)"

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

captcha