我使用此代码将DIB转换为TBitmap,那么我如何操作此代码以适合PNG图像(保持其透明度)?我试图将Transparent属性设置为true,但似乎代码是为256色位图制作的。
代码来源:Here
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
你可以看看PngComponents。单元PngFunctions.pas
包含一个将任意TGraphic
转换为png图像的方法。当你用TPngImagelist
标记你的问题时,你可能已经在使用这个库了。