Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between PaintEventArgs.ClipRectangle and PaintEventArgs.Graphics.ClipBounds

Tags:

.net

winforms

When overriding an OnPaint or OnPaintBackground method in an inheritor of System.Windows.Forms.Control, one argument is always a System.Windows.Forms.PaintEventArgs object. The partial object structure:

  • PaintEventArgs
    • ClipRectangle (System.Drawing.Rectangle)
    • Graphics (System.Drawing.Graphics)
      • Clip (System.Drawing.Region)
      • ClipBounds (System.Drawing.RectangleF)
      • VisibleClipBounds (System.Drawing.RectangleF)

Graphics.Clip appears to be a 1-bit map of applicable pixels to influence in subsequent paint operations.

MSDN: "Gets or sets a Region that limits the drawing region of this Graphics."

Graphics.ClipBounds is a read only rectangle that appears to be the minimum rectangle to fully contain the Clip region.

MSDN: "Gets a RectangleF structure that bounds the clipping region of this Graphics."

Graphics.VisibleClipBounds seems to be a more 'intelligent' clip, though I cannot understand how it operates.

MSDN: "Gets the bounding rectangle of the visible clipping region of this Graphics."

ClipRectangle appears to in all cases duplicate the Graphics.ClipBounds property.

MSDN: "Gets the rectangle in which to paint."

Can someone please answer the main question and potentially shed light on what all the various boundary objects are for?

Edit: added MSDN's descriptions as per Dan-o's answer.

like image 806
J Collins Avatar asked Aug 21 '12 19:08

J Collins


1 Answers

PaintEventArgs.ClipRectangle is the rectangle that actually needs to be drawn by your Paint event. Commonly equal to the size of the control's client area. It will be smaller when only a portion of it was overlapped by another window. Or when you call its Invalidate(Rectangle) method. You can use it to skip drawing expensive objects that fall outside of that rectangle. Which is pretty rare, Windows itself already does a very good job of clipping what you draw.

Graphics.Clip is a region that you can assign in your drawing code to clip what you draw yourself. It allows for various kinds of effects, like drawing an image clipped by a circle. Or really intricate clipping effects that use a GraphicsPath converted to a region.

Graphics.ClipBounds is the rectangle around Graphics.Clip, it makes the math faster to get a first order estimate whether or not a point is outside of Graphics.Clip. You'll get a meaningless "infinite" clip bounds if you never assigned the Clip property (X=-4194304,Y=-4194304,Width=8388608,Height=8388608)

Graphics.VisibleClipBounds is the same as Graphics.ClipBounds but clipped also by the control window. So useful to test if a point might be inside the Clip region and visible to the user. If you never assign the Clip property then you'll just get the size of the window.

So it is Windows that sets PaintEventArgs.ClipRectangle. The rest is set by you by assigning the Graphics.Clip property.

like image 179
Hans Passant Avatar answered May 23 '23 06:05

Hans Passant