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

来自 , 2019-06-26, 写在 Delphi (Object Pascal), 查看 108 次.
URL http://www.code666.cn/view/1e7875cf
  1. function GetBMPSize(const FileName: string): Types.TSize;
  2. type
  3.   // Union of two different versions of windows bitmap info records
  4.   TBitmapInfo = packed record
  5.     case Byte of
  6.       0: (Core: Windows.TBitmapCoreHeader); // early version
  7.       1: (Ext: Windows.TBitmapInfoHeader);  // later version
  8.   end;
  9. const
  10.   cSignature = $4D42; // bitmap file signature
  11. var
  12.   FileHeader: Windows.TBitmapFileHeader;  // bmp file header record
  13.   BmpInfo: TBitmapInfo;                   // bmp info record union
  14.   FS: Classes.TFileStream;                // stream onto bmp file
  15.   BytesRead: Integer;                     // bytes read from stream
  16. begin
  17.   Result.cx := 0;
  18.   Result.cy := 0;
  19.   if (FileName = '') or not SysUtils.FileExists(FileName) then
  20.     Exit;
  21.   FS := Classes.TFileStream.Create(
  22.     FileName, SysUtils.fmOpenRead or SysUtils.fmShareDenyNone
  23.   );
  24.   try
  25.     // Read file header and check signature
  26.     BytesRead := FS.Read(FileHeader, SizeOf(FileHeader));
  27.     if BytesRead <> SizeOf(FileHeader) then
  28.       Exit;
  29.     if FileHeader.bfType <> cSignature then
  30.       Exit;
  31.     // Read bitmap info record (varies according to bmp type)
  32.     BytesRead := FS.Read(BmpInfo, SizeOf(BmpInfo));
  33.     if BytesRead < SizeOf(DWORD) then
  34.       Exit; // couldn't read length field
  35.     if BytesRead < Integer(BmpInfo.Ext.biSize) then
  36.       Exit; // couldn't read whole info header record
  37.     if BmpInfo.Ext.biSize = SizeOf(BmpInfo.Ext) then
  38.     begin
  39.       // later version
  40.       Result.cx := BmpInfo.Ext.biWidth;
  41.       Result.cy := BmpInfo.Ext.biHeight;
  42.     end
  43.     else
  44.     begin
  45.       // early version
  46.       Result.cx := BmpInfo.Core.bcWidth;
  47.       Result.cy := BmpInfo.Core.bcHeight;
  48.     end;
  49.   finally
  50.     FS.Free;
  51.   end;
  52. end;
  53. //delphi/2245

回复 "Delphi返回指定bitmap图片的尺寸"

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

captcha