Einzelnen Beitrag anzeigen

Benutzerbild von Matze
Matze
(Co-Admin)

Registriert seit: 7. Jul 2003
Ort: Schwabenländle
14.929 Beiträge
 
Turbo Delphi für Win32
 
#12

Re: [GELÖST]: Bilder/Bitmaps transparent in eine PaintBox la

  Alt 8. Okt 2009, 18:10
Etwas langsam aufgrund des pixelweisen Zeichnens auf der PaintBox und Delphi-Referenz durchsuchenBitBlt geht da leider nicht, aber sonst geht der folgende Code bei mir.

Delphi-Quellcode:
procedure DrawTransparent(ImgList: TImageList; ImgLIndex: Integer; PaintBox: TPaintBox);
// some crazy declarations
type TRGB = packed record
  R, G, B: Byte;
end;

type TLine = array[0..0] of TRGB;

var
  MyBitmap: TBitmap;
  x, y: Integer;
  PLine: ^TLine;
  PixelColor, TranspColor: TColor;
begin
  MyBitmap := TBitmap.Create;
  try
    MyBitmap.PixelFormat := pf24bit;

    // get bitmap from the TImageList
    ImgList.GetBitmap(ImgLIndex, MyBitmap);

    // the transparant color is in the upper left corner
    TranspColor := MyBitmap.Canvas.Pixels[0, 0];

    // read pixels from the bitmap
    for y := 0 to MyBitmap.Height - 1 do
    begin
      for x := 0 to MyBitmap.Width - 1 - 1 do
      begin
        PLine := MyBitmap.ScanLine[y];

        // check if the color of the current pixel is TranspColor
        PixelColor := RGB(PLine[x].R, PLine[x].G, PLine[x].B);

        // draw pixels on the TPaintBox.Canvas
        if PixelColor <> TranspColor then
        begin
          PaintBox.Canvas.Pixels[x, y] := PixelColor;
        end;
      end;
    end;
  finally
    FreeAndNil(MyBitmap);
  end;
end;
Die Farbe des oberen linken Pixels wird als transparente Farbe genommen.

Aufruf:
DrawTransparent(ImageList1, 0, PaintBox1); Das ist mehr zum Spaß und bei großen Bilder vermutlich nicht sehr schnell.

Grüße, Matze
  Mit Zitat antworten Zitat