Einzelnen Beitrag anzeigen

Duck-of-Devastation

Registriert seit: 31. Mär 2010
11 Beiträge
 
Delphi 10.1 Berlin Enterprise
 
#1

GDI+ Bilddrehung mit Transparenz

  Alt 25. Feb 2019, 08:44
Moin!


ich versuche mittels GDI+ ein TBitmap (pf32Bit, bmDIP, afDefined) zu rotieren. Dieses klappt auch mit u.a. Procedure, leider geht dabei die Transparenz flöten und ich kenne mich mit GDI+ leider nicht ausreichend aus (Delphi 10.1 Berlin):


Code:
uses gdipapi, gdipobj; ...

procedure RotateBitmapGDI(Bmp: TBitmap; Degs: Integer; AdjustSize: Boolean;
  BkColor: TColor = clNone);
var
  Tmp: TGPBitmap;
  Matrix: TGPMatrix;
  C: Single;
  S: Single;
  NewSize: TSize;
  Graphs, G2: TGPGraphics;
  P: TGPPointF;
begin


  Tmp := TGPBitmap.Create(Bmp.Handle, Bmp.Palette);
 
  if tmp.getpixelformat=PixelFormatAlpha then showmessage('alpha');
  if tmp.getpixelformat=PixelFormatPAlpha then showmessage('Prealpha');
  if isalphapixelformat(tmp.GetPixelFormat) then ShowMessage('ALPHA');
  if iscanonicalpixelformat(tmp.GetPixelFormat) then ShowMessage('Canonical');
  if tmp.GetPixelFormat=PixelFormat32bppRGB then ShowMessage('32RGB');
  if isindexedpixelformat(tmp.GetPixelFormat) then ShowMessage('Indexed');
  showmessage(inttostr(getpixelformatsize(tmp.GetPixelFormat)));


  Matrix := TGPMatrix.Create;
  try
    Matrix.RotateAt(Degs, MakePoint(0.5 * Bmp.Width, 0.5 * Bmp.Height));
    if AdjustSize then
    begin
      C := Cos(DegToRad(Degs));
      S := Sin(DegToRad(Degs));
      NewSize.cx := Round(Bmp.Width * Abs(C) + Bmp.Height * Abs(S));
      NewSize.cy := Round(Bmp.Width * Abs(S) + Bmp.Height * Abs(C));
      Bmp.Width := NewSize.cx;
      Bmp.Height := NewSize.cy;
    end;
    Graphs := TGPGraphics.Create(Bmp.Canvas.Handle);
    try
      Graphs.Clear(ColorRefToARGB(ColorToRGB(BkColor)));
      Graphs.SetTransform(Matrix);
      Graphs.DrawImage(tmp, (Cardinal(Bmp.Width) - Tmp.GetWidth) div 2, (Cardinal(Bmp.Height) - Tmp.GetHeight) div 2);
    finally
      Graphs.Free;
    end;
  finally
    Matrix.Free;
    Tmp.Free;
  end;
end;

Das Problem ist offenbar, dass bei
Code:
Tmp := TGPBitmap.Create(Bmp.Handle, Bmp.Palette);
das Pixelformat nicht übernommen wird. Ich habe alternativ ausprobiert, die TGPBitmap aus einem Stream zu laden, auch das entfernt aber leider die Transparenz. Wenn ich die tmp-Bitmap selbst mit pixelFormat32bppARGB erzeuge, bin ich leider zu doof, meine TBitmap dort reinzukopieren (ich habe versucht, zu tmp ein TGPGraphics zu erzeugen und mit DrawImage reinzumalen, aber da bin ich offenbar zu blöd zu).


Kann mir da jemand auf die Sprünge helfen?


VG

Sven
Sven
  Mit Zitat antworten Zitat