取出资源文件中的bitmap,并将其保存到TMemoryStream中,从资源里载入图象而不丢失调色板

Posted 游子日月长

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了取出资源文件中的bitmap,并将其保存到TMemoryStream中,从资源里载入图象而不丢失调色板相关的知识,希望对你有一定的参考价值。

 

从资源里载入图象而不丢失调色板

procedure loadgraphic(naam:string);
var
  { I‘ve moved these in here, so they exist only during the lifetime of the
    procedure. }
  HResInfo: THandle;
  BMF: TBitmapFileHeader;
  MemHandle: THandle;
  Stream: TMemoryStream;
  ResPtr: PByte;
  ResSize: Longint;
  null:array [0..8] of char;
  
begin
  { In this first part, you are retrieving the bitmap from the resource.
    The bitmap that you retrieve is almost, but not quite, the same as a
    .BMP file (complete with palette information). }

  strpcopy (null, naam);
  HResInfo := FindResource(HInstance, null, RT_Bitmap);
  ResSize := SizeofResource(HInstance, HResInfo);
  MemHandle := LoadResource(HInstance, HResInfo);
  ResPtr := LockResource(MemHandle);

  { Think of a MemoryStream almost as a "File" that exists in memory.
    With a Stream, you can treat either the same way! }

  Stream := TMemoryStream.Create;

  try
    Stream.SetSize(ResSize + SizeOf(BMF));

    { Next, you effectively create a .BMP file in memory by first writing
      the header (missing from the resource, so you add it)... }
    BMF.bfType := $4D42;
    Stream.Write(BMF, SizeOf(BMF));

    { Then the data from the resource. Now the stream contains a .BMP file }
    Stream.Write(ResPtr^, ResSize);

    { So you point to the beginning of the stream... }
    Stream.Seek(0, 0);

    { ...and let Delphi‘s TBitmap load it in }
    Bitmap:=tbitmap.create;
    Bitmap.LoadFromStream(Stream);

    { At this point, you are done with the stream and the resource. }
  finally
    Stream.Free;
  end;
  FreeResource(MemHandle);
end;

以上是关于取出资源文件中的bitmap,并将其保存到TMemoryStream中,从资源里载入图象而不丢失调色板的主要内容,如果未能解决你的问题,请参考以下文章

Bitmap

IOS拦截webview加载并将网页资源保存到磁盘

如何从同一资源运行两个函数(绘图图形并将无限数据保存到txt文件)

如何序列化对象并将其保存到 Android 中的文件中?

创建一个表并将其保存到Julia中的csv文件中

我们如何逐行打印字符并将其保存到 PLSQL 中的 csv 或文本文件