Is it possible to convert TBitmap32 to TBitmap without copying pixels?

1.4k Views Asked by At

Can I convert TBitmap32 object to TBitmap (pf32bit) object without copying pixels?

I found 2 methods to copy TBitmap32 to TBitmap without visibly copying pixels but maybe there are pixels being copied under the hood. Or maybe one method is better somehow than another? Could someone knowing WinAPI advise?

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');

Method 2

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;
0

There are 0 best solutions below