Recent

Author Topic: Abnormal canvas brush colour after added the BGRABitmap  (Read 1251 times)

williamy

  • Newbie
  • Posts: 2
Abnormal canvas brush colour after added the BGRABitmap
« on: August 03, 2020, 01:43:48 pm »
Hello,
I was using the BGRABitmap in a project and noticed the canvas behavior is abnormal because of the BGRABitmap.
Below is a simple unit to reproduce the problem. The button1 will load the image, and button 2 to draw a retangle.
The problem is, the color of the retangle is not white. it is following the color of its parent.
So, if the color of Form1 is red, then, the retangle will be in red.
I am testing it on win7.  Lazarus 2.0.8.


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.     Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  6.     ExtDlgs, BGRABitmap, BGRABitmapTypes;
  7. type
  8.     { TForm1 }
  9.     TForm1 = class(TForm)
  10.         Button1: TButton;
  11.         Button2: TButton;
  12.         Image1: TImage;
  13.         OpenPictureDialog1: TOpenPictureDialog;
  14.         procedure Button1Click(Sender: TObject);
  15.         procedure Button2Click(Sender: TObject);
  16.     private
  17.     public
  18.     end;
  19. var
  20.     Form1: TForm1;
  21.     bgraImg: TBGRABitmap;
  22. implementation
  23. {$R *.lfm}
  24. { TForm1 }
  25. procedure TForm1.Button1Click(Sender: TObject);
  26. begin
  27.     bgraImg := TBGRABitmap.Create;
  28.     if OpenPictureDialog1.Execute then
  29.     begin
  30.         bgraImg.LoadFromFile(OpenPictureDialog1.FileName);
  31.         Image1.Picture.Assign(bgraImg.Bitmap);
  32.     end;
  33. end;
  34. procedure TForm1.Button2Click(Sender: TObject);
  35. begin
  36.     Image1.Canvas.Brush.Color := clWhite;
  37.     Image1.Canvas.Rectangle(10, 10, 200, 200);
  38. end;
  39. end.
  40.  


winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Abnormal canvas brush colour after added the BGRABitmap
« Reply #1 on: August 03, 2020, 02:34:36 pm »
Hi!

Don't assign the BGRAbitmap to the Image.Picture but draw on the Image:

Code: Pascal  [Select][+][-]
  1. BGRAImg.Draw(Image1.Canvas,0,0,true);  


Winni

williamy

  • Newbie
  • Posts: 2
Re: Abnormal canvas brush colour after added the BGRABitmap
« Reply #2 on: August 03, 2020, 02:52:59 pm »
Thanks for the quick response. it works.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Abnormal canvas brush colour after added the BGRABitmap
« Reply #3 on: August 03, 2020, 08:42:31 pm »
Hi!

One tip to reduce code:

You can code create and loadfromfile in one line:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.      if OpenPictureDialog1.Execute then
  4.     begin
  5.        bgraImg := TBGRABitmap.Create(OpenPictureDialog1.FileName);
  6.         ....
  7.      end;
  8. end;
  9.  

Winni

circular

  • Hero Member
  • *****
  • Posts: 4221
    • Personal webpage
Re: Abnormal canvas brush colour after added the BGRABitmap
« Reply #4 on: August 08, 2020, 05:07:18 pm »
I suppose it is related to the alpha channel added from the BGRA bitmap.

It makes sense to assign the bitmap so that if the window is redrawn, the image will be kept.

Though to be sure the image you assign is not transparent, you can use the function MakeBitmapCopy:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var bmpCopy: TBitmap;
begin
    bgraImg := TBGRABitmap.Create;
    if OpenPictureDialog1.Execute then
    begin
        bgraImg.LoadFromFile(OpenPictureDialog1.FileName);
        bmpCopy := bgraImg.MakeBitmapCopy(clForm);
        Image1.Picture.Assign(bmpCopy);
        bmpCopy.Free;
    end;
end;

Note that bgraImg variable would normally either be:
- a local variable in the Button1Click function
- an object variable in TForm1

And would need to be freed, either at the end of Button1Click function or in FormDestroy.

If you want also the rectangle drawn on the image to be persistent, you can draw it within the image:
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
begin
    Image1.Picture.Bitmap.Canvas.Brush.Color := clWhite;
    Image1.Picture.Bitmap.Canvas.Rectangle(10, 10, 200, 200);
end;

Explanation: it is to draw on the canvas of the picture contained in the image. The picture in fact can contain any kind of graphic things, not just a bitmap, so here we specify we access it as a bitmap.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018