简体   繁体   English

System.Drawing.Graphics.DrawRectangle绘制比指定大1px的矩形

[英]System.Drawing.Graphics.DrawRectangle draws rectangle 1px bigger than specified

Very simple code here...just drawing what should be a 64x64 rectangle in the OnPaint routine for a Form : 这里的代码非常简单...只需在FormOnPaint例程中绘制64x64矩形:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.DrawRectangle(Pens.Black, new Rectangle(32, 32, 64, 64));
}

I took the following screen capture, and measured the square, which was 65x65. 我采取了以下屏幕截图,并测量了方形,即65x65。

在此输入图像描述

Just wondering what causes the extra 1px width/height, and how to fix? 只是想知道是什么原因导致额外的1px宽度/高度,以及如何修复?

EDIT... 编辑...

I know I can fix using Rectangle.Inflate(-1, -1) ...more to the point, I want to know why GDI/GDI+ renders the rectangle 1px wider/higher than specified, and in this context, give reason for using the Inflate fix. 我知道我可以修复使用Rectangle.Inflate(-1, -1) ...更重要的是,我想知道为什么GDI / GDI +渲染矩形1px比指定更宽/更高,并在此上下文中,给出原因使用Inflate修复程序。

The centre of each border edge is drawn to the left of or below the actual edge of the rectangle. 每个边框边缘的中心都绘制在矩形实际边缘的左侧或下方。 For a single-pixel border, the top edge and bottom edge are drawn below the actual edge of the rectangle. 对于单像素边框,顶边和底边绘制在矩形的实际边缘下方。 If you draw such a rectangle at the top of the form then the Top of that rectangle is 0 and the top edge is drawn in the space between 0 pixels and 1 pixel. 如果在窗体顶部绘制这样的矩形,则该矩形的顶部为0,顶部边缘绘制在0像素和1像素之间的空间中。 If that rectangle is 64 pixels high then the Bottom is 64 and the bottom edge is drawn between 64 pixels and 65 pixels. 如果该矩形高64像素,则底部为64,底部边缘绘制在64像素和65像素之间。 If you set the thickness of the pen to 2 then you'll find the the other half of the bottom edge is drawn between 63 and 64 pixels. 如果将笔的粗细设置为2,则会发现底边的另一半绘制在63到64像素之间。 The vertical edges are likewise, ie drawn to the right for the first odd pixels and the left for even. 垂直边缘同样,即向右绘制第一个奇数像素,向左绘制为偶数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM