* * *

Author Topic: Merge to Bitmaps with transparent Color  (Read 451 times)

gbl

  • Newbie
  • Posts: 2
Merge to Bitmaps with transparent Color
« on: July 16, 2017, 10:45:19 am »
Hello,

I'm new to BGRABitmap.
Currently I'll try to merge two Bitmaps to another one. One of them must have a transparent Color.

I home bellow code is quite clear enough.

The Goal is a Bitmap containing a black Cross on a blue Background.

kind regards
Guenter


Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5.  
  6. {$define test2}
  7.  
  8.  
  9. interface
  10.  
  11. uses
  12.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  13.   BGRABitmap, BGRABitmapTypes{, BGRALayers};
  14.  
  15. type
  16.  
  17.   { TForm1 }
  18.  
  19.   TForm1 = class(TForm)
  20.     Image1: TImage;
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     { public declarations }
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. var bmp: TBGRABitmap;
  39.  
  40.     bmp1: TBGRABitmap;
  41.     bmp2: TBGRABitmap;
  42.     bmp3: TBGRABitmap;
  43.  
  44.     w,h : integer;
  45. begin
  46.   w := Image1.Width;
  47.   h := Image1.Height;
  48.  
  49.  
  50.   {$ifdef test1}
  51.   bmp := TBGRABitmap.Create(w, h, BGRABlack);
  52.   bmp.FillRect(20, 20, w div 2 + 20,h div 2 + 20, BGRAWhite, dmSet); //zeichnet ein weißes Quadrat ohne Transparenz
  53.   bmp.FillRect(w div 2 - 20, h div 2 - 20, w-20, h-20 ,BGRA(0,0,255,128), dmDrawWithTransparency); //zeichnet ein transparentes blaues Quadrat
  54.   bmp.FillRect(20, 20, w div 2 + 20,h div 2 + 20, BGRAWhite, dmSet); //zeichnet ein weißes Quadrat ohne Transparenz
  55.   {$endif}
  56.  
  57.  
  58.   {$ifdef test2}
  59.   bmp := TBGRABitmap.Create();
  60.  
  61.   // Blaues Viereck
  62.   bmp1 := TBGRABitmap.Create(w, h, ColorToBGRA(clBlue));
  63.  
  64.   // Trasparentes Schwarzes Kreuz
  65.   bmp2 := TBGRABitmap.Create(w, h, ColorToBGRA(clFuchsia));
  66.   bmp2.Canvas.FillRect(0, 0, w, h);
  67.   bmp2.Canvas.Pen.Width:=7;
  68.   bmp2.Canvas.Pen.Style:=psSolid;
  69.   bmp2.Canvas.Line(10, 10, w-10, h-10);
  70.   bmp2.Canvas.Line(10, h-10, w-10, 10);
  71.   bmp2.ReplaceColor(ColorToBGRA(clFuchsia),BGRAPixelTransparent);
  72.  
  73.  
  74.   // Bilder zu einem Bitmap zusammenfügen
  75.   // bmp2 soll mit transparenter Farbe gezeichnet werden.
  76.   bmp3 := TBGRABitmap.Create();
  77.   bmp3.SetSize(bmp1.Width, bmp1.Height);
  78.   bmp3.PutImage(0, 0, bmp1, dmSet );
  79.   {
  80.   bmp3. ....
  81.  
  82.   HOW TO INSERT BMP2 WITH TRANSPARENT COLOR clFuchsia HERE ?
  83.   }
  84.  
  85.  
  86.   // Bild dem endgültigem Bitmap übergeben
  87.   bmp.SetSize(bmp3.Width, bmp3.Height);
  88.   bmp.Assign(bmp3);
  89.   {$endif}
  90.  
  91.  
  92.   // ---- Darstellen von bmp
  93.  
  94.   Image1.Picture.Bitmap.Assign(bmp);
  95. end;
  96.  
  97. end.
  98.  

circular

  • Hero Member
  • *****
  • Posts: 2602
    • Personal webpage
Re: Merge to Bitmaps with transparent Color
« Reply #1 on: July 17, 2017, 08:15:33 pm »
Hello!

First, you need to remove this line:
Code: Pascal  [Select]
  1. bmp2.Canvas.FillRect(0, 0, w, h);
because your image is already filled with fuschia and this would fill it with a default color (white or black).

Note that using Canvas is not recommended, it is there for compatibility only. It can induce slowness and aliasing. Instead it is recommended to use CanvasBGRA property.

As you replaced the fuschia color, you can simply do a PutImage in the end:
Code: Pascal  [Select]
  1. bmp3.PutImage(0, 0, bmp2, dmDrawWithTransparency );

Conscience is the debugger of the mind

gbl

  • Newbie
  • Posts: 2
Re: Merge to Bitmaps with transparent Color
« Reply #2 on: July 21, 2017, 09:46:34 am »
Thank you.

It works now.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus