Recent

Author Topic: Solved - Unable to set image background to white  (Read 516 times)

Wilko500

  • New Member
  • *
  • Posts: 19
Solved - Unable to set image background to white
« on: January 22, 2021, 01:38:07 am »
I had this working in previous test program but after moving into my full project it did not.  The difference is down to different order of operations. Under test I was
-creating a form dynamically with TImage (align set to acClient) with random size
-setting the background to white
-then drawing on the image.
The new process
-creates the form dynamically the same was with TImage (align set to acClient) with a fixed size
-calculates coords and sets form to new size/position
-then draws on the image.

What happens is that the area drawn to (cleared) is the same size as the form that the dynamically created form is based on.
Quote
frm.Image1.Canvas.Line(0, 0, frm.image1.Width-1, frm.image1.Height-1);
frm.Image1.Canvas.Line(0, frm.image1.Height-1,frm.image1.Width-1 ,0);
draws the red lines and seems to understand the actual image1 size.
Quote
frm.Image1.Canvas.FillRect(frm.image1.BoundsRect);
but this which I found somewhere seems not to. I am missing something but what? The attached project Test12.zip contains just enough of my program to simulate the problem
Thanks
« Last Edit: January 23, 2021, 02:38:27 am by Wilko500 »

Handoko

  • Hero Member
  • *****
  • Posts: 4048
  • My goal: build my own game engine using Lazarus
Re: Unable to set image background to white
« Reply #1 on: January 22, 2021, 05:09:32 am »
Problem fixed. Download and try the modified code below.

The issue happened because you resized the form. I believe for lightweight reason TImage does not 'renew' its canvas after being resized. You need create a new canvas to replace the old but because replacing the canvas is not allowed, so I replace the whole TImage directly.

Code: Pascal  [Select][+][-]
  1. procedure TfrmPage.FormResize(Sender: TObject);
  2. var
  3.   Temp: TImage;
  4. begin
  5.   Temp        := TImage.Create(Self);
  6.   Temp.Parent := Self;
  7.   Temp.Width  := Width;
  8.   Temp.Height := Height;
  9.   Temp.Canvas.Clear;
  10.   Temp.Canvas.CopyRect(Temp.BoundsRect, Image1.Canvas, Temp.BoundsRect);
  11.   Image1.Free;
  12.   Image1 := Temp;
  13. end;

The workaround clearly has limitations. For a good solution you will need to think many things. How should it handle the resizing? Does is pad and/or trim the edges? Or does it scale the image? If padding and trimming are used, does it do it at the center or topleft corner? If scaling is used, does it simply stretching it or do it proportionally? And which interpolation is being used? Nearest-neighbor, billinear, Lancoz, box, etc?

If you really try to provide the good solution, you will end up to write a graphics editing program like GIMP or LazPaint. So TImage does not 'try' to handle the canvas resizing, it lets the programmer to implement the solution.
« Last Edit: January 22, 2021, 05:20:41 am by Handoko »

Wilko500

  • New Member
  • *
  • Posts: 19
Re: Unable to set image background to white
« Reply #2 on: January 22, 2021, 11:33:41 am »
Thank you.  You have confirmed my own (inexperienced) thoughts.  I'll give your code a try as an interim solution.  I think I've arrived at a point where a simple port of existing logic/code from VB6 to FPC is not going to go as smoothly as I had hoped.  Time therefore to review the program logic, take stock and see if there is practical alternative that fits better into FPC framework.

lucamar

  • Hero Member
  • *****
  • Posts: 3591
Re: Unable to set image background to white
« Reply #3 on: January 22, 2021, 12:03:08 pm »
The issue happened because you resized the form. I believe for lightweight reason TImage does not 'renew' its canvas after being resized. You need create a new canvas to replace the old but because replacing the canvas is not allowed, so I replace the whole TImage directly.

You don't need to do anything that complicated. What one should do is instead of drawing on the canvas of TImage, do it on the underlying graphic (for example, in the Bitmap property). If one does it that way TImage will happily adjust itself and/or the graphic to whatever properties you set for the control: Center, Proportional, Stretch, whatever.

Seems I have to say this every time ;) : TImage is *not* a "painting" control, like TPaintBox, but a "showing" one, meant to show an underlying graphic. Any graphic operation should be done in that graphic, not on the canvas of TImage.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Wilko500

  • New Member
  • *
  • Posts: 19
Re: Unable to set image background to white
« Reply #4 on: January 23, 2021, 02:37:52 am »
Just out of interest I was having a look at bitmap and I discovered that I can achieve my needs by adding the following lines after dynamic creation of form and before drawing to it.  The two constants can be taken from the form width/height properties. The TImage.align is set t o alClient.
 
Code: Pascal  [Select][+][-]
  1. frm.Image1.Picture.Bitmap.Width:=600;
  2. frm.Image1.Picture.Bitmap.Height:=1581;
  3. frm.Image1.Canvas.FillRect(frm.image1.BoundsRect);

Handoko

  • Hero Member
  • *****
  • Posts: 4048
  • My goal: build my own game engine using Lazarus
Re: Solved - Unable to set image background to white
« Reply #5 on: January 23, 2021, 04:18:40 am »
Yes, that should work. I hadn't thought about it.

lucamar

  • Hero Member
  • *****
  • Posts: 3591
Re: Solved - Unable to set image background to white
« Reply #6 on: January 23, 2021, 04:40:28 am »
Rather than constants I would get the bitmap width and height from the TImage size and make sure the image has its Stretch property to True but otherwise, yes, that's what I meant ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Wilko500

  • New Member
  • *
  • Posts: 19
Re: Solved - Unable to set image background to white
« Reply #7 on: January 23, 2021, 11:07:12 am »
Quote
Rather than constants I would get the bitmap width and height from the TImage size and make sure the image has its Stretch property to True but otherwise, yes, that's what I meant
Good point, I was get them from the form, thank you.

 

TinyPortal © 2005-2018