当我尝试使用以下方法将此位图绘制为 2 像素宽时,我创建了一个 1 像素宽和 256 像素高的位图:
public void DrawImage(Image image,RectangleF rect)
位图绘制不正确,因为每个位图列之间有白色细条纹。 请参阅下面的简单代码
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics gr = e.Graphics;
Bitmap bitmap = new Bitmap(1, 256);
for (int y = 0; y < 256; y++)
{
bitmap.SetPixel(0, y, Color.Red);
}
RectangleF rectf = new RectangleF();
for (int x = 0; x < 500; x++)
{
float factor = 2;
rectf.X = x*factor;
rectf.Y = 0;
rectf.Width = fact;
rectf.Height = 500;
// should draw bitmap as 2 pixels wide but draws it with white slim stripes in between each bitmap colomn
gr.DrawImage(bitmap, rectf);
}
}
最佳答案
这是 Graphics.InterpolationMode 的副作用,当位图边缘的像素耗尽时,位图缩放会产生伪影。对于只有一个像素宽的位图,会出现大量像素耗尽的情况。通过将其设置为 NearestNeighbor 并将 PixelOffsetMode 设置为 None 可以获得更好的结果。不过,这仍然会产生伪影,从外观上看,存在一些内部舍入错误。不确定,我必须猜测“事实”的值(value)。
避免缩放小位图。
关于c# - WinForms 上的 DrawImage() 函数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6744067/