c# - WinForms 上的 DrawImage() 函数无法正常工作

标签 c# .net winforms graphics drawimage

当我尝试使用以下方法将此位图绘制为 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/

相关文章:

c# - PictureBox 在 RotateFlipType 之后不重绘,但它使用断点

c# - 为什么扩展在@helper 中不可用?

.net - ASP.NET MVC - 跨 View 传递数据

winforms - 如何在Delphi中的任务栏上显示透明且无边框的窗体?

c# - 什么图标应该设置为表单的图标?

c# - Windows 窗体 : Progress Bar Unresponsive

c# - 系统重启后Ignite缓存变空

c# - 如何将字典参数传递给 web-api 方法?

.net - 在 Xamarin.Forms 中指定默认按钮

c# - 在 .NET 4.6 项目中引用 .NET Core 库