views:

20

answers:

1

How can I retrieve the client rectangle (of a control) out of a Graphics object? I think the Graphics object should know its coordinates of the area to draw in, but I don't see how to actually get it.

Edit: As stated in the answers, there should be access to the Control that the Graphics object was created for. This would obviously solve the problem (actually that's the solution I am currently using as a workaround), but I'm intentionally asking for a way without the control, just because I figured it should be possible.

A: 

If you haven't set the Clip property to further limit drawing area, its default setting will be the entire Graphics area, so you should be able to use Clip.GetBounds() to return a RectangleF describing the drawing area. ClipBounds is a property of the Graphics object itself that will return the same data.

If you know what Control the Graphics area was generated from, you can also use the control's height and width; the Graphics area should be the same dimensions.

EDIT: Grr. SOMEONE knows what Control this Graphics area was created from. It is most likely the immediate caller of one of your rendering library's methods. So, if you need the useable limits of the drawing area in your methods, I'd simply require it of your callers: accept a System.Drawing.Size or System.Drawing.Rectangle parameter that you can apply to the Graphics.ClipBounds, or require the Graphics area to be pre-clipped by testing Clip.IsInfinite() and throwing an exception if true.

KeithS
The was my first idea, too, but the MSDN states that the clip bounds are "infinitely big" if not set.
mafutrct
I do not have access to the Control the Graphics is from, would be too easy :)
mafutrct