【发布时间】:2012-04-29 18:34:49
【问题描述】:
在下面的代码中:
void f13(Graphics g)
{
g.FillRectangle(new SolidBrush(Color.Black), pictureBox1.ClientRectangle);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
var zf = .0143;
const int w = 6000, h = 10, margin = 40;
var bmp = new Bitmap(w + 2 * margin, h + 2 * margin);
var bmpG = Graphics.FromImage(bmp);
bmpG.FillRectangle(new SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height);
var srcRect = new RectangleF(margin - .5f, margin - .5f, w, h);
zf = (float)Convert.ToInt32(w * zf) / w;
var destRect = new Rectangle(0, 0, Convert.ToInt32(w * zf), Convert.ToInt32(w * zf));
g.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
destRect.X += destRect.Width;
g.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
f13(e.Graphics);
}
我得到两个矩形之间的间隙:
micro http://www.uploadup.com/di-0HXM.png macro http://www.uploadup.com/di-G1O5.png
这是为什么呢?
如果差距线不是很清楚,您可以减少边距。如果你将它设置为 10,你会得到:
【问题讨论】:
-
添加
g.PixelOffsetMode = PixelOffsetMode.Half;以便浮点舍入错误不会字节。 -
感谢 Hans,这将 margin 的值降低到 9。现在即使是 8 也会出现差距。我仍然可以解释这一点,但无法弄清楚 PixelOffsetMode 是什么。你能解释更多吗?
-
我还评论了设置插值模式的行,这导致必须增加边距。使用默认插值模式,边距必须至少为 18。GDI+ 中发生了什么,我如何在其中找到自己的方式?