3

Microsoft's online documentation says that this Graphics method returns "true if the rectangle specified by the rect parameter is contained within the visible clip region of this Graphics; otherwise, false".

Do they really mean that? It would be much more useful to have it return true if any part of rect lies within the clip region. That way, if rect was the bounding rectangle of some object to be drawn, you would know if none of the object was on view and you could safely skip it.

I've experimented with some very simple WinForms code and the method does appear to behave as I'd like rather than as the documentation says. But I'm reluctant to assume I'm right. Does anyone know for sure?

And while I'm talking about the documentation for this, does anyone have a definition of "visible clip region"? Microsoft says it's "the intersection of the clipping region of this Graphics and the clipping region of the window", but there must be more to it than that: some Graphics objects, for example, don't even have a window (if they're associated with a Bitmap in memory).

Experimentation suggests that if, say, you're painting onto a rectangular Panel of size X by Y, the Graphics clip region is intersected with an X-by-Y rectangle. And apparently this continues to apply if the Panel is bigger than its containing Form and partly scrolled out of view. But again, it would be good to know what the actual rules are.

All accumulated wisdom is gratefully received.

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
Spooner
  • 31
  • 2
  • 1
    You could try to use the Graphics.IsVisible(point) overload a few times.. – TaW Feb 02 '21 at 19:26
  • 1
    [What is the difference between PaintEventArgs.ClipRectangle and PaintEventArgs.Graphics.ClipBounds](https://stackoverflow.com/a/12062755/7444103). Note that a Rectangle has four vertices, so four known Points. `Graphics.IsVisible()` and `GraphicsPath.IsVisible()` also have an overload for Point. You can build extension methods, or use standard math, to perform specific calculations. Also, the underlying Graphics procedures (and the BufferedGraphics) are smart enough to know when drawing something is necessary or not. – Jimi Feb 02 '21 at 19:27
  • @Jimi Yes, I spotted that Hans Passant answer a couple of days ago, and found it very helpful - it cleared up a lot of questions. Unfortunately he stopped short of defining 'control window' so some puzzles remained. Regarding the Point overloads of IsVisible, all four vertices of a rectangle could be outside a clipping region yet some part could be inside. You can't beat a purpose-built rectangle test - if that's what we have! – Spooner Feb 03 '21 at 01:45
  • We have much more than that (also, two vertices define other Point positions, as the Point in the middle of a Line): a Clipping Region (a [Region](https://docs.microsoft.com/en-us/dotnet/api/system.drawing.region.complement)) object and Rectangle have Complement, Exclude, Intersect, Union, XOR methods that also work with other objects - as the GraphicsPath - so you can determine whether the Intersection of two Regions or Rectangles (see the `GetBounds()` methods) is empty. Also, yes, using standard math and basic geometry you can *beat* native GDI+ function that need Interop (PInvoke). – Jimi Feb 03 '21 at 05:42
  • BUT, in relation to the .Net documentation of GDI+ and its functionality, tools and properties, see what I wrote here: [Disable Image blending on a PictureBox](https://stackoverflow.com/a/54726707/7444103) -- I'm not sure what is obscure about the *Window* concept: all Controls have a Window that (in this context) defines its bounds and of course, in relation to interaction with each other and their Parent Container, Clipping Regions. Other objects, as a Bitmap, don't have a [Window](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.nativewindow), but have Bounds -- Dive deeper. – Jimi Feb 03 '21 at 05:53

1 Answers1

2

I don't know about the documentation, but my experience is pretty much aligned with your observation, that the method returns true, if any part of the rectangle is within the visible clip region of the Graphics.

Don't overthink this. Documentation is never as precise as code. Any sizable body of documentation will contain such imprecise definitions.

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
  • 1
    In fact reading over the many overloads for point and rectangle params, it looks more like a copy and paste error to me..: Correct for point, not so much for rectangle.. – TaW Feb 02 '21 at 19:41
  • 1
    most Microsoft's SDK and API documentation is now on github. I have improved several such sections of documentation myself, as a plain end user, just with my github account. If a contribution is coherent and improves the quality, a PR is likely to get approved by one of their content managers in a matter of days. – Cee McSharpface Feb 02 '21 at 19:58
  • @CeeMcSharpface Interesting. Mind to share the link to the repo? – EricSchaefer Feb 02 '21 at 20:00
  • https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.Drawing/Graphics.xml – Cee McSharpface Feb 02 '21 at 20:00
  • I'm glad to hear your experience is that the method acts as one would hope, but I'm still a bit hesitant to rely on it doing so. Imprecision in documentation is understandable but needs addressing just like bugs in code (good enough isn't good enough, you might say). Improvement efforts like those of @CeeMcSharpface are a welcome contribution. – Spooner Feb 03 '21 at 02:25