gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:12:05 25 4
gpt4 key购买 nike

如果标题有点含糊,请见谅。基本上我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com