Можно ли конвертировать TBitmap32 в TBitmap без копирования пикселей? - PullRequest
0 голосов
/ 21 июня 2019

Могу ли я преобразовать объект 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');

Метод 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;
...