Graphics.DrawPolygon没有在正确的位置绘制 [英] Graphics.DrawPolygon not drawing at correct location
问题描述
在左边是我使用类似绘画的程序绘制的多边形.右边是System.Drawing.Bitmap/Graphics绘制的多边形:
On the left is the polygon I drew using my paint-like program. On the right is the polygon drawn by System.Drawing.Bitmap/Graphics:
绘制它的代码如下:
protected static Bitmap CropImage(Bitmap src, Vector2[] rect)
{
var result = new Bitmap(src.Width, src.Height);
using (Graphics g = Graphics.FromImage(result))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
var pen = new Pen(Color.White);
g.DrawImage(src, new Point(0,0));
var poly = rect.Select(p => p.ToPointF()).ToArray();
g.DrawPolygon(pen, poly);
}
return result;
}
poly
是:
{System.Drawing.PointF[4]}
[0]: {X = 57.4230042 Y = 57.4229736}
[1]: {X = 147.058868 Y = 56.0224}
[2]: {X = 148.43277 Y = 143.951767}
[3]: {X = 58.7969131 Y = 145.352341}
图像中的每个黑色正方形均为50x50.如果您查看 poly
,则所有坐标都完全符合您的期望: poly [0]
略高于50,50,与 inside 标有"6"的黑色正方形(如左图所示).
Each of the black squares in the image is 50x50. If you look at poly
, all the coordinates are exactly as you'd expect: poly[0]
is a little above 50,50 which corresponds to inside the black square labelled "6" (as shown in left image).
那么 Graphics
怎么会变得困惑并将其放置在错误的位置?看起来它正在缩小整个矩形.
So how is Graphics
getting confused and putting it in the wrong spot? It looks like it's scaling the whole rect down.
推荐答案
您很困惑.矩形是正确的,但是背景图像放大了.请注意,6和黑色正方形要大得多,但矩形的大小完全相同.
You are confused. The rectangle is correct, but the background image is scaled up. Note that the 6 and the black square are a lot bigger, but the rectangle is the exact same size.
请注意, Graphics.DrawImage
方法缩放源图像以匹配目标分辨率.
Note that the Graphics.DrawImage
method scales the source image to match the destination resolution.
此方法使用其物理尺寸绘制图像,因此无论显示设备的分辨率(每英寸点数)如何,图像都将具有以英寸为单位的正确尺寸.例如,假设图像的像素宽度为216,水平分辨率为每英寸72点.如果调用此方法在分辨率为每英寸96点的设备上绘制该图像,则渲染图像的像素宽度将为(216/72)* 96 = 288.
This method draws an image using its physical size, so the image will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you call this method to draw that image on a device that has a resolution of 96 dots per inch, the pixel width of the rendered image will be (216/72)*96 = 288.
您应该使用过载接受 Rectangle
代替:
g.DrawImage(src, new Rectangle(0, 0, src.Width, src.Height));
这篇关于Graphics.DrawPolygon没有在正确的位置绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!