Recent

Author Topic: problem with transparency  (Read 6240 times)

Blue1987

  • Full Member
  • ***
  • Posts: 165
problem with transparency
« on: April 14, 2011, 02:13:52 am »
can anyone explain me what's wrong with this code??

Code: [Select]
  image1.Canvas.Brush.Color:=clblue;
  image1.canvas.FillRect(0,0,image1.Width,image1.height);
  image2.Transparent:=true;
  {image2.Picture.Bitmap.TransparentColor:=clblack;
  image2.Canvas.Brush.Color:=clblack;
  image2.canvas.FillRect(0,0,image2.Width,image2.height);}
  image2.canvas.Pen.Color:=clred;
  image2.Canvas.Pen.Style:=pssolid;
  image2.Canvas.Line(0,image2.height,image2.width,0);

i simply would like image1 to stay in the background, and image2 to show the red line only (and the rest transparent)...


thank you, Paolo

Blue1987

  • Full Member
  • ***
  • Posts: 165
Re: problem with transparency
« Reply #1 on: April 14, 2011, 02:17:59 am »
this is the output when I compile the code

(if I also use the code in the {}brackets it simply shows the blue square)

lainz

  • Guest
Re: problem with transparency
« Reply #2 on: April 14, 2011, 03:10:50 am »
can anyone explain me what's wrong with this code??

...

i simply would like image1 to stay in the background, and image2 to show the red line only (and the rest transparent)...


thank you, Paolo


Because you are drawing in Canvas, not assigning a Picture to TImage. So you can't use Transparent property in this way.

This assign transparency in a temp bitmap, and then draw the bitmap in the image2:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   bmp: TBitmap;
  4. begin
  5.   image1.Canvas.Brush.Color:=clblue;
  6.   image1.canvas.FillRect(0,0,image1.Width,image1.height);
  7.  
  8.   bmp:= TBitmap.Create;
  9.   bmp.Width:= image2.Width;
  10.   bmp.Height:= image2.Height;
  11.  
  12.   bmp.Transparent:=True;
  13.   bmp.TransparentColor:=clBlack;
  14.  
  15.   with bmp.canvas do begin
  16.   Pen.Color:=clred;
  17.   Pen.Style:=pssolid;
  18.   Line(0,image2.height,image2.width,0);
  19.   end;
  20.  
  21.   image2.Picture.Assign(bmp);
  22.   bmp.Free;
  23. end;

Blue1987

  • Full Member
  • ***
  • Posts: 165
Re: problem with transparency
« Reply #3 on: April 14, 2011, 03:37:06 am »
can anyone explain me what's wrong with this code??

...

i simply would like image1 to stay in the background, and image2 to show the red line only (and the rest transparent)...


thank you, Paolo


Because you are drawing in Canvas, not assigning a Picture to TImage. So you can't use Transparent property in this way.

This assign transparency in a temp bitmap, and then draw the bitmap in the image2:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   bmp: TBitmap;
  4. begin
  5.   image1.Canvas.Brush.Color:=clblue;
  6.   image1.canvas.FillRect(0,0,image1.Width,image1.height);
  7.  
  8.   bmp:= TBitmap.Create;
  9.   bmp.Width:= image2.Width;
  10.   bmp.Height:= image2.Height;
  11.  
  12.   bmp.Transparent:=True;
  13.   bmp.TransparentColor:=clBlack;
  14.  
  15.   with bmp.canvas do begin
  16.   Pen.Color:=clred;
  17.   Pen.Style:=pssolid;
  18.   Line(0,image2.height,image2.width,0);
  19.   end;
  20.  
  21.   image2.Picture.Assign(bmp);
  22.   bmp.Free;
  23. end;


thanks a lot!

GBarton

  • Newbie
  • Posts: 3
Re: problem with transparency
« Reply #4 on: March 26, 2013, 09:57:53 pm »
I have tried the same code by Lainz but Image2 is black (not transparent). Is there any way to make Image2 transparent? I am using Lazarus 1.0.8.

GBarton

  • Newbie
  • Posts: 3
Re: problem with transparency
« Reply #5 on: April 04, 2013, 12:50:43 am »
Image2.Transparent:= true
and it works :)

 

TinyPortal © 2005-2018