0

我可以在不复制像素的情况下将 TBitmap32 对象转换为 TBitmap (pf32bit) 对象吗?

我找到了 2 种将 TBitmap32 复制到 TBitmap 的方法,而不会明显地复制像素,但可能有像素在引擎盖下被复制。或者也许一种方法比另一种更好?知道WinAPI的人可以建议吗?

var bmp2: TBitmap32;
    bmp: TBitmap;

    h: HBitmap;
    tag: tagBITMAP;
begin
  bmp2 := TBitmap32.Create;
  bmp2.LoadFromFile('in.bmp');


  tag.bmType := 0;
  tag.bmWidth := bmp2.Width;
  tag.bmheight := bmp2.height;
  tag.bmWidthBytes := bmp2.Width*4;
  tag.bmPlanes := 1;
  tag.bmBitsPixel := 32;
  tag.bmBits := @bmp2.Bits[0];

  h := CreateBitmapIndirect(tag);

  bmp := TBitmap.Create;
  bmp.PixelFormat := pf32bit;
  bmp.Handle := h;


  bmp.SaveToFile('out.bmp');

方法二

var bmp2: TBitmap32;
    bmp: TBitmap;
    x,y: Integer;

    h: HBitmap;
    bi  : TBitmapInfo;
    res : integer;
    abitmap: HBitmap;
begin
  bmp2 := TBitmap32.Create;
  bmp2.LoadFromFile('in.bmp');


  FillChar (bi,SizeOf(bi),0);
  with bi.bmiHeader do
  begin
     biSize := SizeOf(bi.bmiHeader);
     biWidth := bmp2.Width;
     biHeight := bmp2.height;
     biPlanes := 1;
     biBitCount := 32;
     biCompression := BI_RGB;
  end;

  bmp := TBitmap.Create;

  aBitmap := 0;
  try
    aBitmap := CreateDIBitmap(GetDC(0), bi.bmiHeader, CBM_INIT,  @bmp2.Bits[0], bi, DIB_RGB_COLORS);
    bmp.handle := aBitmap;
    bmp.SaveToFile('out.bmp');
  finally
    DeleteObject(aBitmap);
    bmp.Free;
  end;
4

0 回答 0