views:

81

answers:

4

Can anyone share a sample code to draw a non-rectangular part of a picture in delphi canvas?

+1  A: 
Canvas.Ellipse(0, 0, 10, 20); // not a rectangle
Rob Kennedy
how can we cut the picture with any shape, you are not answering any part of this question
Maysam
+2  A: 

Your question is pretty vague. But I suspect what you are looking for is clipping regions. Read up on them. Set the clipping region on the target device to the shape you want, and then draw the image onto the device. Only the part of the image that would be within the clipping region will be drawn.

GrandmasterB
how can I do that in delphi?
Maysam
Read up on the Region and clipping functions of the Windows GDI, such as CreatePolygonRGN() and SelectClipRgn(). There should be ample examples online of using regions to clip output. Its really pretty simple - you define a region on the target DC that you want to limit drawing to, perform your drawing (images, lines, whatever), and then reset the clipping region back to normal. Here's a simple example at the MSDN library: http://msdn.microsoft.com/en-us/library/dd183437(v=VS.85).aspx
GrandmasterB
A: 

I use so called runlists for this feature (generalized shapes and blitting them). I've seen them called warplists too. A shape is encoded as a runlist by defining it as a set of horizontal lines, and each line is two integer values (skip n pixels,copy n pixels).

This means you can draw entire lines, leaving you with only "height" draw operations.

So a rectangle is defined (the first "skip" pixels from top level corner to the left corner (xorg,yorg). The rectangle is width_rect wide, and width_pixels goes a line further. width_pixels can be wider than the width of the picture (alignment bytes)

(yorg*width_pixels+xorg  , width_rect),
(width_pixels-width_rect , width_rect),
(width_pixels-width_rect , width_rect),
(width_pixels-width_rect , width_rect),
..
..

This way you can make your drawing routines pretty generic, and for simple, regular shapes (rects, circles) it takes only minor math to precalculate these lists. It simplified my shape handling enormously.

However I draw directly to bitmaps, not to canvasses, so I can't help with that part. A primitive that efficiently draws a row, and a way to extract a row from a graphic should be enough.

Marco van de Voort
A: 

You're looking for GDI paths. Start here, which explains what paths are in this context, and provides links on the left to explain the functionality available with them.

Google can turn up lots of examples of using paths in Delphi. If you can't find them, post a comment back here and I'll see what I can turn up for you.

Ken White