View Issue Details

IDProjectCategoryView StatusLast Update
0031556LazarusLCLpublic2020-03-31 13:57
ReporterMike Wiering Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
PlatformPCOSWindows 
Product Version1.6.2 
Summary0031556: transparency not working when drawing a bitmap to a canvas
DescriptionIf you create a TBitmap and set its TransparentColor and then draw it to the canvas of a TImage, the image is drawn without transparency.
Steps To ReproduceNew application, add a TButton and a TImage:


procedure TForm1.Button1Click(Sender: TObject);
var
  bmp: TBitmap;
begin
  bmp := TBitmap.Create();
  bmp.Width := 100;
  bmp.Height := 100;
  with bmp.Canvas do
  begin
    Brush.Color := clRed;
    FillRect(Rect(0, 0, 100, 100));
    Brush.Color := clYellow;
    FillRect(Rect(20, 20, 80, 80));
  end;
  bmp.Transparent := TRUE;
  bmp.TransparentColor := clRed;

  with Image1.Picture.Bitmap do
  begin
    Width := 100;
    Height := 100;
    Canvas.Brush.Color := clBlue;
    Canvas.FillRect(Rect(0, 0, 100, 100));
    Canvas.Draw(0, 0, bmp);
  end;
end;


In Delphi, you get a blue square with a yellow square in the middle.
In Lazarus, you get a red square with a yellow square in the middle.
Tagstransparent
Fixed in Revision
LazTarget
Widgetset
Attached Files

Relationships

related to 0024241 new TBitBtn do not display bitmap correctly 

Activities

Mike Wiering

2017-03-18 04:38

reporter   ~0098997

Last edited: 2017-03-18 04:40

View 2 revisions

I've investigated this some more and the issue is more complex. If you change the code above and set the transparency at the top after creating the image it does work.

However, once you draw the image with Canvas.Draw, the transparency mask is locked and the TransparentColor is forgotten, so when you change the image, the transparency doesn't change anymore, if you draw with the transparent color onto it, it will just show that color.

Here is code that will show the difference in Delphi and Lazarus (see the result image above):


procedure TForm1.BitBtn1Click(Sender: TObject);
  var
    bmp1: TBitmap;
begin
  bmp1 := TBitmap.Create();
  bmp1.Transparent := TRUE;
  bmp1.TransparentColor := clRed;

  bmp1.Width := 100;
  bmp1.Height := 100;
  with bmp1.Canvas do
  begin
    Brush.Color := clRed;
    FillRect(Rect(0, 0, 100, 100));
    Brush.Color := clYellow;
    FillRect(Rect(20, 20, 80, 80));
  end;

  Image1.Picture.Bitmap := TBitmap.Create();
  Image1.Picture.Bitmap.Width := 100;
  Image1.Picture.Bitmap.Height := 100;
  with Image1.Picture.Bitmap.Canvas do
  begin
    Brush.Color := clGreen;
    FillRect (Rect(0, 0, 100, 100));
    Draw(0, 0, bmp1);
  end;

  with bmp1.Canvas do
  begin
    Brush.Color := clBlue;
    FillRect(Rect(0, 0, 50, 50));
    Brush.Color := clRed;
    FillRect (Rect(50, 50, 100, 100));
  end;

  with Image1.Picture.Bitmap.Canvas do
  begin
    Brush.Color := clGreen;
    FillRect (Rect(0, 0, 100, 100));
    Draw(0, 0, bmp1);
  end;
end;

Mike Wiering

2017-03-18 04:39

reporter  

delphi_lazarus.png (4,106 bytes)   
delphi_lazarus.png (4,106 bytes)   

Mike Wiering

2017-03-18 05:45

reporter   ~0098998

I found a temporary workaround: every time before drawing, mask the image again with the current transparent color.

So for example, in the last code, before

draw(0, 0, bmp1);

you can add

bmp1.Mask(clRed);

Issue History

Date Modified Username Field Change
2017-03-17 04:28 Mike Wiering New Issue
2017-03-18 04:38 Mike Wiering Note Added: 0098997
2017-03-18 04:39 Mike Wiering File Added: delphi_lazarus.png
2017-03-18 04:40 Mike Wiering Note Edited: 0098997 View Revisions
2017-03-18 05:45 Mike Wiering Note Added: 0098998
2020-03-31 10:37 Juha Manninen Relationship added related to 0024241
2020-03-31 13:57 Juha Manninen Tag Attached: transparent