Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Delphi TBitmap.draw und transparenz (https://www.delphipraxis.net/169002-tbitmap-draw-und-transparenz.html)

Memnarch 22. Jun 2012 16:10

TBitmap.draw und transparenz
 
Tag allerseits,
Irgendwo habe ich gerade nen hänger.
TBitmap implementiert Draw und DrawTransparent.
Beide funktionen sind recht spaghetti artig. Was mir jedoch aufgefallen ist, ist das beide methoden die AlphaBlendMethode in bestimmten fällen nutzen um eine grafik alphatransparent zu zeichnen.

zwei probleme:

1) so ganz werde ich aus dem gewurschtel nicht schlau
2) Bisher konnte ich es über TBitmap einstellungen nicht erreichen, das ich einfach

Delphi-Quellcode:
MyBMP.canvas.draw(0, 0, MyOtherBMP)
nutzen konnte um ein Alphatransparentes bild zu zeichnen. Ich musste mir da bisher immer selbst mit alphaBlend helfen. Jemand erfahren was ich bei der TBitmap einstellen muss?
nur 32bit reicht da wohl nicht -.-

Die bitmaps wurden zur laufzeit erstellt.

MFG
Memnarch

Bummi 22. Jun 2012 19:49

AW: TBitmap.draw und transparenz
 
Delphi-Quellcode:

type
  pRGBQuadArray = ^TRGBQuadArray;
  TRGBQuadArray = ARRAY[0..$effffff] OF TRGBQuad;


Procedure SetAlpha(bmp:TBitMap;Alpha:Byte);
// 20120622 by Thomas Wassermann
var
 pscanLine32 : pRGBQuadArray;
 i,j:Integer;
 begin
   Bmp.PixelFormat := pf32Bit;
   bmp.HandleType := bmDIB;
   bmp.ignorepalette := true;
   bmp.alphaformat := afDefined;
   for i := 0 to bmp.Height -1 do
     begin
     pscanLine32 := bmp.Scanline[i];
     for j := 0 to bmp.Width -1 do
        begin
          pscanLine32[j].rgbReserved := Alpha;
          pscanLine32[j].rgbBlue := 0;
          pscanLine32[j].rgbRed := 0;
          pscanLine32[j].rgbGreen := 0;
        end;
     end;
 end;

procedure TForm5.Button1Click(Sender: TObject);
var
  Bmp,bmp2: TBitmap;
  p : TPNGImage;
begin
  Bmp:=TBitmap.Create;
  Bmp2:=TBitmap.Create;
  bmp2.Width := 300;
  bmp2.height := 300;
  SetAlpha(bmp2,0);
  p:=TPNGImage.Create;
  try
    p.LoadFromFile('C:\Icons\IconCollection\ix_ap_all\48x48\shadow\about.png');
    Bmp.Assign(p);
    bmp2.Canvas.Draw(0,0,bmp);
    bmp2.Canvas.Draw(50,50,bmp);
    Canvas.Draw(0,0,bmp);
    Canvas.Draw(100,100,bmp2);
  finally
    Bmp.Free;
    bmp2.Free;
    p.Free;
  end;
end;

ich habe gerade mal experimentiert ... man kann auch mit GDI+ wie erwartet malen (Alpha verhält sich wie gewünscht im Gegensatz zu den Canvasroutinen), auch ein Image behält die Transparenz.

Delphi-Quellcode:
procedure TForm5.Button1Click(Sender: TObject);
var
  Bmp,bmp2: TBitmap;
  p : TPNGImage;
  g : TGPGraphics;
  b:TGPSolidBrush;
begin
  Bmp:=TBitmap.Create;
  Bmp2:=TBitmap.Create;
  bmp2.Width := 300;
  bmp2.height := 300;
  SetAlpha(bmp2,50);
  p:=TPNGImage.Create;
  try
    p.LoadFromFile('C:\Icons\IconCollection\ix_ap_all\48x48\shadow\about.png');
    Bmp.Assign(p);
    bmp2.Canvas.Draw(20,20,bmp);
    bmp2.Canvas.Draw(50,50,bmp);
    g := GetGraphics(bmp2.Canvas);
    b := GetSolidBrush(clRed,128);
    try
      g.FillRectangle(b,100.0,100.0,100.0,100.0);
      g.FillRectangle(b,150.0,150.0,100.0,100.0);
      Canvas.Draw(100,100,bmp2);
      image1.Picture.Assign(bmp2);

    finally
      g.Free;
      b.Free;
    end;
  finally
    Bmp.Free;
    bmp2.Free;
    p.Free;
  end;
end;

Memnarch 25. Jun 2012 11:27

AW: TBitmap.draw und transparenz
 
du hast im zweiten beispiel ne png date vwerwendet. alpha hat funktioniert? Versuchs mal mit ner 32bit bmp :|

Bummi 25. Jun 2012 12:09

AW: TBitmap.draw und transparenz
 
Liste der Anhänge anzeigen (Anzahl: 1)
Du traust mit nicht :oops:

EXE und Code im Anhang (nur BMP, Pfad sollte existieren)


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:06 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz