If you have a Linux OS: please help to test, if you can reproduce this bug:
- compile and run attached project on Linux
- if a graphic image is displayed, then the bug did
not occur
- if instead the form is completely gray, then the bug
did occur
- if the bug occurs, please uncomment lines 54..57. Does the bug now disapear?
Please report your results here and include the detailed version numbers of your Linux OS and your Lazarus installation. Thanks a lot. If the but is reproduceable, I will file a bug report.
The attached program is a demo which was created by wp in reply #11 of this Topic:
https://forum.lazarus.freepascal.org/index.php/topic,68059.0.htmlIt displays a picture file (demo.png) which is also included in the attached project. I added only 1 line (line 52):
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, ComCtrls,
math;
const
FileSpec = 'demo.png'; {picture file to show}
Margin = 10; {Margin for background-color in 'clYellow'}
type
{ TForm1 }
TForm1 = class(TForm)
PaintBox1: TPaintBox;
ScrollBox1: TScrollBox;
procedure FormDestroy(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure showPicture(fspec: string);
procedure FormActivate(Sender: TObject);
private
FPicture: TPicture;
FBuffer: TBitmap;
FScalingFactor: Double;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
const
MaxFormWidth = 1000;
MaxFormHeight = 800;
{ TForm1 }
procedure TForm1.ShowPicture(FSpec: String);
var
w, h, i: Integer;
R: TRect;
begin
FPicture.Free; // To prevent memory leak when ShowPicture might be called a second time
FPicture := TPicture.Create;
if FBuffer = nil then FBuffer := TBitmap.Create;
FBuffer.PixelFormat:=pf32bit; // added
(*
i:=33; {values < 33 do not work} // this seems to solve the bug:
FBuffer.SetSize(i,i);
FBuffer.Canvas.Brush.Color:=clBlack;
FBuffer.Canvas.FillRect(0,0,i,i);
*)
try
FPicture.LoadFromFile(FSpec);
w := round(FScalingFactor*FPicture.Width) + 2*Margin;
h := round(FScalingFactor*FPicture.Height) + 2*Margin;
Paintbox1.SetBounds(0, 0, w, h);
SetBounds(0, 0, Min(Paintbox1.Width, MaxFormWidth), Min(Paintbox1.Height, MaxFormHeight));
FBuffer.SetSize(w, h);
FBuffer.Canvas.Brush.Color := clYellow;
FBuffer.Canvas.FillRect(0, 0, FBuffer.Width, FBuffer.Height);
R := Rect(Margin, Margin, FBuffer.Width - Margin, FBuffer.Height - Margin);
FBuffer.Canvas.StretchDraw(R, FPicture.Bitmap);
except
on E:Exception do
begin
ShowMessage('Error on loading file!');
FreeAndNil(FPicture);
FreeAndNil(FBuffer);
exit;
end;
end;
end;
const
Counter: Integer = 0;
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
if FBuffer = nil then
exit;
WriteLn('OnPaint ' + IntToStr(Counter));
inc(Counter);
Paintbox1.Canvas.Draw(0, 0, FBuffer);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FPicture.Free;
FBuffer.Free;
end;
procedure TForm1.FormActivate(Sender: TObject);
const
first: boolean = true;
begin
if not first then exit;
first:=false;
ScrollBox1.Align:=alClient;
ScrollBox1.BorderStyle:=bsNone;
ScrollBox1.HorzScrollBar.Tracking:=true; {scroll in Realtime: }
ScrollBox1.VertScrollBar.Tracking:=true;
Paintbox1.Left := 0;
Paintbox1.Top := 0;
FScalingFactor := 0.75; {0.75 causes bug, 0.74 and 0.76 work}
ShowPicture(FileSpec); {shows the picture in file 'FileSpec'}
end;
end.
The reason for this line 52 is, that I found out, that the memory consumption with 'pf32bit' is significantly smaller (1 GB less for 'FScalingFactor=14'). After I did this, I got this bug for many FScalingFactor's:
- the bug occurs with values of 0.71, 0.73, 0.75 and so on
- the bug does not occur with values of 0.72, 0.74, 0.76 and so on.
This bug only occurs on Linux (Ubuntu 18.04 64-bit) with gtk2, not with qt5. And not on Windows. I tested with:
- Lazarus 3.99 (rev main_3_99-2316-g00f3d3b397) FPC 3.3.1 x86_64-linux-gtk2
- Lazarus 3.4.0 with FPC 3.2.2
- Lazarus 2.2.4 with FPC 3.2.2
- Lazarus 2.0.10 with FPC 3.2.0
I found out, that lines 54..57 seem to be a workaround for this bug. Can you confirm this?