Delphi DIB到Tbitmap代码操作以适应PNG图像?
我使用此代码将DIB转换为TBitmap,那么如何操作此代码以适合PNG图像(保留其透明度)? 我试图将Transparent属性设置为true,但代码似乎是为256色位图编写的 代码来源:Delphi DIB到Tbitmap代码操作以适应PNG图像?,delphi,delphi-7,dib,tpngimagelist,tbitmap,Delphi,Delphi 7,Dib,Tpngimagelist,Tbitmap,我使用此代码将DIB转换为TBitmap,那么如何操作此代码以适合PNG图像(保留其透明度)? 我试图将Transparent属性设置为true,但代码似乎是为256色位图编写的 代码来源: VAR 比特数:整数; BitmapFileHeader:TBitmapFileHeader; BitmapInfo:pBitmapInfo; DIBinMemory:指针; MemoryStream:TMemoryStream; NumberOfColors:整数; 开始 结果:=TBitmap.Crea
VAR
比特数:整数;
BitmapFileHeader:TBitmapFileHeader;
BitmapInfo:pBitmapInfo;
DIBinMemory:指针;
MemoryStream:TMemoryStream;
NumberOfColors:整数;
开始
结果:=TBitmap.Create;
DIBinMemory:=GlobalLock(hDIB);
尝试
BitmapInfo:=DIBInMemory;
NumberOfColor:=BitmapInfo.bmiHeader.biClrUsed;
BitCount:=BitmapInfo.bmiHeader.biBitCount;
如果(NumberOfColor=0)和(BitCount你可以看一看。单位PngFunctions.pas
包含一种方法,可以将任何TGraphic
转换为png图像。因为你已经用TPngImagelist
标记了你的问题,你可能已经在使用这个库了。谢谢你,但问题是当我从上面的代码得到位图时失真(无透明度)问题是我使用SDK旋转png图像,该SDK将图像输出为DIB句柄,但当我保存图像,然后再次对其进行复制时,效果良好。从DIB到位图的转换会扭曲透明度。
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