c# - Paint.exe 样式缩放,最近邻插值减半边界像素

标签 c# winforms zooming

如果标题有点含糊,请见谅。基本上我在 c# 表单应用程序中创建一个缩放控件,我的想法是我可以按因素缩放图像,即。 1 倍、2 倍、4 倍、8 倍。我需要图像保持像素化,即。最近的邻居。缩放效果非常好,除了当我在使用边界像素时选择 Interp 模式作为最近的邻居时,它默认为内部颜色。这就是说外部像素的宽度似乎是内部像素的一半,真正出现的问题是当我添加一个工具提示来显示它抛出的当前鼠标悬停像素的 x、y 坐标时离开。要清楚,它被丢弃的原因是因为我将当前像素计算为:

void m_pictureBox_MouseMove(object sender, MouseEventArgs e)
{
    int x = e.Location.X / m_zoomFactor;
    int y = e.Location.Y / m_zoomFactor;
}

并且由于外部像素是宽度的一半......好吧,你明白了。

绘制代码很简单:

void m_pictureBox_Paint(object sender, PaintEventArgs e)
{
    if (m_pictureBox.Image!=null)
    {
        e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
        e.Graphics.ScaleTransform((float)m_zoomFactor, (float)m_zoomFactor);
        e.Graphics.DrawImage(m_pictureBox.Image, 0, 0);
    }            
}

图片控件托管在我的自定义“ZoomControl”中,它本身是从“Panel”控件继承的。

我的问题基本上是,任何人都可以帮助我解决边界像素问题吗?有没有更好的方法来获得缩放功能?

最佳答案

您还需要更改 Graphics.PixelOffsetMode。它默认为无,这对于插值是可以的,但当您将像素放大为 block 时则不行。将其更改为一半。例如:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    private float mZoom = 10;

    protected override void OnPaint(PaintEventArgs e) {
      e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
      e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
      Image img = Properties.Resources.SampleImage;
      RectangleF rc = new RectangleF(0, 0, mZoom * img.Width, mZoom * img.Height);
      e.Graphics.DrawImage(img, rc);
    }
  }

关于c# - Paint.exe 样式缩放,最近邻插值减半边界像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1281336/

相关文章:

c# - 使用方法 'localhost' 对用户 'root' 的主机 'mysql_native_password' 的身份验证失败并显示消息(使用密码 : NO)

c# - 将字符串转换为 char* 到字符串(C# -> C -> C++)

c# - 如何让 XAML 中的图像显示为它们的实际大小?

c# - 多选 WinForms 列表框的双向绑定(bind)?

c# - 在 Windows 窗体中启用滚动条

c# - 如何使用 C# 滚动到 WinForms TextBox 中的指定行?

r - 创建/查看后放大 ggplot

r - R中的交互式图形

python - 使用 wx.image 缩放图片

c# - .net mvc View 不在发布时返回模型