Forum > LCL

Doubts about copying an image onto a canvas

(1/1)

simsee:
I want to copy the contents of a TImage onto a scrollbox canvas. I tried with Canvas.Draw and with Canvas.CopyRect, but I don't see anything in both cases. Here is the code:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    ScrollBox1: TScrollBox;    procedure FormShow(Sender: TObject);  private    Image : TImage;    DestRect, SourceRect : TRect;  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormShow(Sender: TObject);begin  Image:=TImage.Create(Self);  Image.Picture.LoadFromFile('project1.ico');  with SourceRect do    begin      Left:=0;      Top:=0;      Width:=Image.Picture.Width;      Height:=Image.Picture.Height;    end;  with DestRect do    begin      Left:=100;      Top:=100;      Width:=Image.Picture.Width;      Height:=Image.Picture.Height;    end;  ScrollBox1.Canvas.Draw(0,0,Image.Picture.Bitmap);             //It does not work  ScrollBox1.Canvas.CopyRect(DestRect,Image.Canvas,SourceRect); //It does not workend; end.
Where did I go wrong? Thank you.

wp:

--- Quote from: simsee on June 03, 2024, 11:52:44 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormShow(Sender: TObject);begin  ...  ScrollBox1.Canvas.Draw(0,0,Image.Picture.Bitmap);             //It does not work  ScrollBox1.Canvas.CopyRect(DestRect,Image.Canvas,SourceRect); //It does not workend;
--- End quote ---
Accessing a canvas method in the OnShow event handler is not a good idea. In some operating systems this is strictly forbidden and will crash the application, and with even more tolerant ones the drawing will not be persistent, i.e. when after this event handler the OS requests the Scrollbox to repaint itself again it will not know that it should draw the image...

Golden rule for user graphics: Do drawing operations only in the OnPaint handlers of controls because this is exactly what the control must execute when it should redraw itself:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormShow(Sender: TObject);begin  Image:=TImage.Create(Self);  Image.Picture.LoadFromFile('project1.ico');end; procedure TForm1.Scrollbox1Paint(Sender: TObject);var  SourceRect, DestRect: TRect;     // No need to make them global variables!begin  with SourceRect do    begin      Left:=0;      Top:=0;      Width:=Image.Picture.Width;      Height:=Image.Picture.Height;    end;  with DestRect do    begin      Left:=100;      Top:=100;      Width:=Image.Picture.Width;      Height:=Image.Picture.Height;    end;  ScrollBox1.Canvas.Draw(0,0,Image.Picture.Bitmap);            // only one of them needed.//  ScrollBox1.Canvas.CopyRect(DestRect,Image.Canvas,SourceRect); end;

TRon:

--- Quote from: simsee on June 03, 2024, 11:52:44 pm ---Where did I go wrong? Thank you.

--- End quote ---
you forgot to mention FPC & Lazarus version and platform. Also take note of the remark at the documentation of the scrollbox OnPaint handler.

nvm as I crossed wp's post.

simsee:
Thanks for the reply and sorry for the delay in mine.

Yes, you're right, I knew the rule that you have to draw inside the OnPaint event handler.

In reality my problem (I'm on Windows) was mainly with CopyRect method (I didn't realize, due to my mistake, that Draw method works within OnPaint).

In fact, if I use CopyRect inside OnPaint, as in your code, I get a run time error (see attached screenshot). What's the reason?

Navigation

[0] Message Index

Go to full version