Why is ClipRectangle always 1px too wide and high?
As already in the subject. If you have a Graphics objects of a control and check the ClipRectangle property it matches the size of the control, however if you try to draw a border:
| | Brush b =new SolidBrush(SystemColors.ControlDark); Pen p =new Pen(b, 1); e.Graphics.DrawRectangle(p, e.ClipRectangle);
|
the bottom and right lines can't be seen because they're 1px too wide. What am I missing here?
[681 byte] By [
TomFrey] at [2007-12-16]
The ClipRectangle will not always match the size of the control that you are drawing, but rather the area that needs to be drawn. Therefore if only half the control needs redrawing,then the ClipRectangle will be half the size of the control.
It is better to use Control.ClientRectangle.
You've hit the famous GDI+ Rectangle off-by-1 error. Any GDI+ rectangle can reproduce this issue.
Put this code in your Button's clieck event handler:
Rectangle rect = new Rectangle(0, 0, 99, 99);
MessageBox.Show(rect.Width.ToString() + ", " + rect.Height.ToString());This should show 100, 100, but it shows 99, 99. Unfortunately, this is one of those bug fixes that we cannot make as it would break every user who's worked around it and thus, it's something you'll also have to code around.
- mike