DIB到Tbitmap代码操作以适应PNG图像?

时间:2011-11-28 12:29:51

标签: delphi delphi-7 dib tpngimagelist tbitmap

我使用此代码将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

1 个答案:

答案 0 :(得分:1)

您可以查看PngComponents。单位PngFunctions.pas包含将任何TGraphic转换为png图像的方法。正如您使用TPngImagelist标记了您的问题,无论如何您可能已经在使用此库。

相关问题