Copy Partial Contents of Canvas into new picture

I have a canvas with several pre-defined areas that are defined by RectShapes. I want to take and create a new picture of an area in the canvas that is bounded by one of the rect shapes.

I’m drawing a blank on how to do this.

The picture you see on a canvas is transient, if you draw it in the Paint event.

you should create a picture the size of the canvas,
draw stuff on that,
and when the Canvas paint event fires, draw bigpic to the canvas.

Now, if you want to recreate a portion of ‘the canvas’, you actually only need to grab part of the picture.

So, if your ‘backing picture’ is bigpic, your small portion code is

dim smallpart as picture smallpart = new picture (thewidth,theheight) smallpart.graphics.drawpicture bigpic,0,0,thewidth,theheight,startx,starty,thewidth,theheight

[quote=236874:@Jeff Tullin]The picture you see on a canvas is transient, if you draw it in the Paint event.
[/quote]

Correct. And I want to grab a portion of what ever is the transient picture at a specific point in time.

Let me think about the rest of what you said and see if I can make it work. However, what I really want is basically a screen grab of a portion of the canvas…

Yeah.
The trick is not to have a transient picture at all.

Always draw to the offscreen picture, whenever you need to draw. (May only be once in a while)
If you need the canvas to repaint, invalidate it or part of it.
The canvas paint event just copies the ready drawn image to the screen, and you can take any part of it elsewhere at any time, or save the whole to disc.

OK. I think I figured this out…

Say my RectShape coordinates are:

MyRectX - Left edge coordinates of the rect shape
MyRectY - Top Edge coordinates of the rect shape
MyRectWidth - Width of the rect shape
MyRectHeight - Height of the rect shape


Dim p1 as New Picture(me.Width,me.Height)
me.DrawInto(p1.graphics, 0, 0)

Dim p2 as Picture(MyRectWidth,MyRectHeight)

p2.graphcs.DrawPicture(p1,0,0,p2.width,p2.height,MyRectX,MyRectY,MyRectWidth,MyRectHeight)

I haven’t tried it yet, but I think this will work…

[quote=236877:@Jeff Tullin]Yeah.
The trick is not to have a transient picture at all.

Always draw to the offscreen picture, whenever you need to draw. (May only be once in a while)
If you need the canvas to repaint, invalidate it or part of it.
The canvas paint event just copies the ready drawn image to the screen, and you can take any part of it elsewhere at any time, or save the whole to disc.[/quote]

Yeah, that’s a good idea. I might see about implementing that.

The other options are DrawInto or using Declares.

Yeah. DrawInto works pretty good…

DrawInto may not work cross platform. It’s been a while since I tried, but it didn’t work on Windows before. I think Linux has had issues as well. If you want guaranteed xplat results, use a buffer image as Jeff suggested.

Thanks, Tim. I’m definitely considering the buffer image due to some other things I’d like to do.