### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Simple Canvas Copy  (Read 5973 times)

#### LA.Center

• Full Member
• Posts: 244
##### Simple Canvas Copy
« on: September 08, 2011, 08:28:59 am »
I am executing this code to copy a simple barchart canvas to a bitmap canvas.

Code: [Select]
`for i := 0 to TCustomBarChart(Instance).Width -1 do        for j := 0 to TCustomBarChart(Instance).Height -1 do        bmp.Canvas.Pixels[i, j] := TCustomBarChart(Instance).Canvas.Pixels[i, j];`
The problem is, it takes very long. A graph that is 200x150 pixels about 7 sec.
Does someone know why, and is there a way to makes this faster?

#### Leledumbo

• Hero Member
• Posts: 8317
• Programming + Glam Metal + Tae Kwon Do = Me
##### Re: Simple Canvas Copy
« Reply #1 on: September 08, 2011, 08:59:58 am »
What if you just CopyRect-ing the Canvas? AFAIK Pixels property is a function call when it's read, hence the overhead.
Code: [Select]
`var  r: TRect;  bc: TCustomBarChart;begin  bc := TCustomBarChart(Instance);  r := Rect(0,0,bc.Width - 1,bc.Height - 1);  bmp.Canvas.CopyRect(r,bc.Canvas,r);end;`

#### LA.Center

• Full Member
• Posts: 244
##### Re: Simple Canvas Copy
« Reply #2 on: September 08, 2011, 09:04:53 am »
Thx a lot man, its ultra fast now

#### Sheepdog

• New Member
• Posts: 21
##### Re: Simple Canvas Copy
« Reply #3 on: January 26, 2021, 10:38:10 am »
Just in case others have the trouble I had...

CopyRec takes a part of one canvas, copies it to somewhere on another canvas.

Crudely put in pseudocode...

DestCanvas.CopyRec(DestArea, SourceCanvas, SourceArea)

... will copy TO "DestCanvas".
The copy will be pasted into "DestArea"
What is copied...
comes from "SourceCanvas"
and will be just the "SourceArea" part of that.

The areas are specifiec with 4 numbers:
The X and Y of the areas left(X), upper (Y) corner, in pixels,
from the left upper corner of the canvas being copied from,
... and ...
The LENGTHS of the area's width (X) and height.

---
Expanding upon how the area is specified:
15,35,40,40 would NOT be a rectangle with corners at (15,35) and (40,40)

It's left upper corner WOULD be at (15,35), but the right lower corner would be
at (55,75)

---
If the dimensions of the destination are different from those of the source,
the material in the rectangle will be re-sized (stretched or shrunk). If you've
specified a different aspect ratio, that will be accommodated too... you will
still get ALL of what was in the area copied from.

Horse's mouth...
https://lazarus-ccr.sourceforge.io/docs/lcl/graphics/tcanvas.copyrect.html
« Last Edit: January 26, 2021, 10:42:12 am by Sheepdog »

#### Sheepdog

• New Member
• Posts: 21
##### Re: Simple Canvas Copy
« Reply #4 on: January 26, 2021, 10:47:48 am »
If you copy from, say, a panel that is on the computer's screen, but currently
behind something else IN THAT WINDOW, none of the stuff behind the other thing
will come through in the copy. But! If the panel is at the front, in IT'S
WINDOW, even if that window is behind other windows, you WILL get what you
might hope to.

Panel1.BringToFront; (and .SendToBack) may be useful to you.