我使用此代码将 DIB 转换为 TBitmap,那么如何操作此代码以适合 PNG 图像(保持其透明度)?我厌倦了将透明属性设置为 true,但似乎代码是为 256 色位图制作的。
代码来源:这里
VAR
BitCount : INTEGER;
BitmapFileHeader: TBitmapFileHeader;
BitmapInfo : pBitmapInfo;
DIBinMemory : Pointer;
MemoryStream : TMemoryStream;
NumberOfColors : INTEGER;
BEGIN
RESULT := TBitmap.Create;
DIBinMemory := GlobalLock(hDIB);
TRY
BitmapInfo := DIBInMemory;
NumberOfColors := BitmapInfo.bmiHeader.biClrUsed;
BitCount := BitmapInfo.bmiHeader.biBitCount;
IF (NumberOfColors = 0) AND (BitCount <= 8)
THEN NumberOfColors := 1 SHL BitCount;
WITH BitmapFileHeader DO
BEGIN
bfType := $4D42; // 'BM'
bfReserved1 := 0;
bfReserved2 := 0;
bfOffBits := SizeOf(TBitmapFileHeader) +
SizeOf(TBitmapInfoHeader) +
NumberOfColors*SizeOf(TRGBQuad);
bfSize := bfOffBits + BitmapInfo.bmiHeader.biSizeImage;
END;
MemoryStream := TMemoryStream.Create;
TRY
MemoryStream.Write(BitmapFileHeader, SizeOf(TBitmapFileHeader));
MemoryStream.Write(DIBInMemory^,
BitmapFileHeader.bfSize - SizeOf(TBitmapFileHeader));
MemoryStream.Position := 0;
RESULT.LoadFromStream(MemoryStream)
FINALLY
MemoryStream.Free
END
FINALLY
GlobalUnlock(hDIB);
GlobalFree(hDIB)
END