### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Bitmap behaviour  (Read 4587 times)

#### nibor

• New member
• Posts: 6
##### Bitmap behaviour
« on: March 08, 2013, 10:51:23 am »
Hi,

Sorry if this is a basic question but I am new to Lazarus.

I have been trying to write an application to manipulate some graphics and I found the following strange behaviour when I run the code below.

Image1(TImage) contains a picture loaded from .jpg First time through this I get the first two ellipses as expected then on the ...picture.bitmap.canvas.. instructions I get a transparent ellipse drawn in the displayed image. (even with transparent set false irrespective of the colours I choose). The second time through the code I get the first two ellipses also drawn as transparent holes in the image (but to the bitmap's scale). All further drawing on the Image1.canvas just makes transparent holes in the bitmap image.

Can anyone help me understand what is happening here? Any help appreciated.

begin
x:=Image1.Width;
y:=Image1.Height;
Image1.Canvas.Brush.Color:=clWhite;
Image1.Canvas.Ellipse(0,0,trunc(x/2),y);
u:=Image1.ClientWidth;
v:=Image1.ClientHeight;
Image1.Canvas.Brush.Color:=clRed;
Image1.Canvas.Ellipse(trunc(u/2)+1,0,u,v);
i:=Image1.picture.Width;
j:=Image1.picture.Height;
Image1.Picture.Bitmap.Canvas.Brush.Color:=clGreen;  {behaviour??}
Image1.Picture.Bitmap.Canvas.Ellipse(0,0,i,j);
end;

#### taazz

• Hero Member
• Posts: 5365
##### Re: Bitmap behaviour
« Reply #1 on: March 08, 2013, 11:49:17 am »
Image1.Picture.Bitmap.Canvas.Brush.Style := bsSolid;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

#### nibor

• New member
• Posts: 6
##### Re: Bitmap behaviour
« Reply #2 on: March 09, 2013, 12:41:24 am »
Thanks for the suggestion but unfortunately this does not change the behaviour.

#### User137

• Hero Member
• Posts: 1791
##### Re: Bitmap behaviour
« Reply #3 on: March 09, 2013, 08:51:01 am »
Image1(TImage) contains a picture loaded from .jpg First time through this I get the first two ellipses as expected then on the ...picture.bitmap.canvas.. instructions I get a transparent ellipse drawn in the displayed image. (even with transparent set false irrespective of the colours I choose).
Please be more specific, what is "as expected", how did you try to set the transparency, and what should and should not be transparent? Are these drawings supposed to be drawn in the mix with the original JPG, or just for the "above layer"?

As taazz said, this makes any drawing that comes after this line become non-transparent:
Image1.Picture.Bitmap.Canvas.Brush.Style := bsSolid;

As for transparency, it is the opposite:
Image1.Picture.Bitmap.Canvas.Brush.Style := bsClear;
In here you have ...Canvas.Pen.Color, and Pen.Style := bsSolid, which should be like that by default.
(Property .Transparent has nothing to do with drawing graphics transparent on top of solid JPG)
« Last Edit: March 09, 2013, 08:54:59 am by User137 »

#### nibor

• New member
• Posts: 6
##### Re: Bitmap behaviour
« Reply #4 on: March 09, 2013, 08:38:52 pm »
Thanks for taking the time to reply.

Image1 has a .jpg loaded appx 1500 pixels wide which is being displayed proportionately into the image which itself is about 500 pixels wide.

The first two ellipse drawing instructions perform as I expected in that they draw on the canvas of Image1 over the displayed bitmap and the drawing is not persistant (i.e. any change to the form looses them).

Any instruction to the underlying bitmap e.g. x:=Image1.picture.bitmap.height;  seems to switch the Image1 reference permanently to the bitmap itself.  So any further drawing to Image1.canvas... actually appears to draw to Image1.picture.bitmap.canvas....  So to me as a novice, it seems strange that I can't draw on the Image1 canvas itself after any such instruction.  Probably missing something here?  (The bitmap changes are persistant with form changes by the way).

Secondly,  when drawing onto the bitmap canvas (the bitmap itself I suppose?) the brush settings as per the previous posts don't seem to make any difference. Neither did altering the transparency with ...Transparent:=false; etc.

I attached the modified code below.  The different behaviours I mention can be seen by commenting out lines when running.

Thanks for any help with understanding this.

procedure TForm1.MakeHoleClick(Sender: TObject);
var
x,y,i,j,u,v : integer;
begin
x:=Image1.Width;
y:=Image1.Height;
Image1.Canvas.Brush.Color:=clWhite;
Image1.Canvas.Ellipse(0,0,trunc(x/2),y);
u:=Image1.ClientWidth;
v:=Image1.ClientHeight;
Image1.Canvas.Brush.Color:=clRed;
Image1.Canvas.Ellipse(trunc(u/2)+1,0,u,v);
i:=Image1.picture.Width;
j:=Image1.picture.Height;
Image1.Picture.Bitmap.Canvas.Brush.Style := bsSolid;
Image1.Picture.Bitmap.Transparent:=false;
Image1.Picture.Bitmap.Canvas.Brush.Color:=clGreen;
Image1.Picture.Bitmap.Canvas.Ellipse(0,0,i,j);    {still get a transparent ellipse in the displayed bitmap}
Image1.Canvas.Brush.Color:=clWhite;
Image1.Canvas.Ellipse(0,0,trunc(x/2),y);  {now draws on the bitmap}
end;