Recent

Author Topic: [SOLVED] TImage remains black after FillRect in FormShow  (Read 3875 times)

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
[SOLVED] TImage remains black after FillRect in FormShow
« on: April 05, 2015, 12:40:52 pm »
I have a procedure to fill the initial bitmap's canvas of TImage (ItemImage):

Code: [Select]
procedure TfrmMain.ClearImage();
begin
  with ItemBmp.Canvas do
    begin
      // form's background color
      Brush.Color := GetColorResolvingParent;
      Brush.Style := bsSolid;
      FillRect(Rect(0, 0, Width, Height));
    end;
  // update image
  ItemImage.Picture.Bitmap.Canvas.Draw(0, 0, ItemBmp);
end;

First of  all: if I call this procedure in FormShow, then ItemImage remains black. When I call it two times in FormShow, then I get the desired result.

Also, it appears that I need a second bitmap (ItemBmp) to clear the image; if I try to do the fillrect directly on ItemImage, it remains black.

Before calling this procedure, Width and Height are properly set in FormShow:

Code: [Select]
procedure TfrmMain.FormShow(Sender: TObject);
begin
  // setup bitmaps
  ItemBmp := TBitMap.Create;
  with ItemImage.Picture.Bitmap do
    begin
      // fixed size
      SetSize(610, 546);
      ItemBmp.SetSize(Width, Height);
    end;
  ClearImage;
  ClearImage;
end;

This is on Debian Linux 7.8. I am probably doing something wrong, but I cannot figure out what it is.
« Last Edit: April 05, 2015, 02:37:18 pm by Artie »
It's only logical.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: TImage remains black after FillRect in FormShow
« Reply #1 on: April 05, 2015, 01:12:50 pm »
Hi,

GetColorResolvingParent can give you values like clForm or clBtnFace which are not RGB values but system constants (see unit Graphics where it is defined).

Try to pass it thru ColorToRGB, it should work:
Code: [Select]
Brush.Color:=ColorToRGB(GetColorResolvingParent);
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: TImage remains black after FillRect in FormShow
« Reply #2 on: April 05, 2015, 01:17:19 pm »
Thanks Blaazen, but no effect.

I narrowed it down to the first problem. Here is my complete example code:

Code: [Select]
unit uImage;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    procedure FormShow(Sender: TObject);
  private
    procedure ClearImage;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.ClearImage;
begin
  with Image1.Picture.Bitmap.Canvas do
    begin
      // form's background color
      Brush.Color := ColorToRGB(GetColorResolvingParent);
      Brush.Style := bsSolid;
      FillRect(Rect(0, 0, Width, Height));
    end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Image1.Picture.Bitmap.SetSize(200, 200);
  ClearImage;  // image remains black
  //ClearImage;  // uncomment to give image the window bgcolor
end;

end.

It's only logical.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: TImage remains black after FillRect in FormShow
« Reply #3 on: April 05, 2015, 01:38:03 pm »
Try replace Width&Height with Image1.Width/Height:
Code: [Select]
procedure TForm1.ClearImage;
begin
  with Image1.Picture.Bitmap.Canvas do
    begin
      // form's background color
      Brush.Color := ColorToRGB(GetColorResolvingParent);
      Brush.Style := bsSolid;
      FillRect(Rect(0, 0, Image1.Width, Image1.Height));
    end;
end;

It has some relation to initialization of the canvas, I don't exactly why it is but modified code works.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: TImage remains black after FillRect in FormShow
« Reply #4 on: April 05, 2015, 01:42:56 pm »
Thanks Blaazen, indeed this works.
It's only logical.

 

TinyPortal © 2005-2018