C# 如何使 PictureBox 使用最近邻重采样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a PictureBox use Nearest Neighbor resampling?
提问by Jared Updike
I am using StretchImage because the box is resizable with splitters. It looks like the default is some kind of smooth bilinear filtering, causing my image to be blurry and have mtheitroade patterns.
我使用 StretchImage 是因为该框可通过拆分器调整大小。看起来默认是某种平滑的双线性过滤,导致我的图像模糊并有莫尔图案。
采纳答案by JYelton
I needed this functionality also. I made a class that inherits PictureBox, overrides OnPaint
and adds a property to allow the interpolation mode to be set:
我也需要这个功能。我创建了一个继承 PictureBox 的类,覆盖OnPaint
并添加一个属性以允许设置插值模式:
using System.Drawing.Drawing2D;
using System.Windows.Forms;
/// <summary>
/// Inherits from PictureBox; adds Interpolation Mode Setting
/// </summary>
public class PictureBoxWithInterpolationMode : PictureBox
{
public InterpolationMode InterpolationMode { get; set; }
protected override void OnPaint(PaintEventArgs paintEventArgs)
{
paintEventArgs.Graphics.InterpolationMode = InterpolationMode;
base.OnPaint(paintEventArgs);
}
}
回答by Joel Lucsy
I suspect you're going to have to do the resizing manually thru the Image class and DrawImage function and respond to the resize events on the PictureBox.
我怀疑您将不得不通过 Image 类和 DrawImage 函数手动调整大小并响应 PictureBox 上的调整大小事件。
回答by GateKiller
When resizing an image in .net, the System.Drawing.Drawing2D.InterpolationMode offers the following resize methods:
在 .net 中调整图像大小时,System.Drawing.Drawing2D.InterpolationMode 提供以下调整大小方法:
- Bicubic
- Bilinear
- High
- HighQualityBicubic
- HighQualityBilinear
- Low
- NearestNeighbor
- Default
- 双三次
- 双线性
- 高的
- 高品质双立方
- 高品质双线性
- 低的
- 最近的邻居
- 默认
回答by ardunn
I did a MSDN search and turns out there's an article on this, which is not very detailed but outlines that you should use the paint event.
我进行了 MSDN 搜索,结果发现有一篇关于此的文章,它不是很详细,但概述了您应该使用 Paint 事件。
http://msdn.microsoft.com/en-us/library/k0fsyd4e.aspx
http://msdn.microsoft.com/en-us/library/k0fsyd4e.aspx
I edited a commonly available image zooming example to use this feature, see below
我编辑了一个常用的图像缩放示例以使用此功能,请参见下文
Edited from: http://www.dotnetcurry.com/ShowArticle.aspx?ID=196&AspxAutoDetectCookieSupport=1
编辑自:http: //www.dotnetcurry.com/ShowArticle.aspx?ID=196&AspxAutoDetectCookieSupport =1
Hope this helps
希望这可以帮助
private void Form1_Load(object sender, EventArgs e)
{
// set image location
imgOriginal = new Bitmap(Image.FromFile(@"C:\images\TestImage.bmp"));
picBox.Image = imgOriginal;
// set Picture Box Attributes
picBox.SizeMode = PictureBoxSizeMode.StretchImage;
// set Slider Attributes
zoomSlider.Minimum = 1;
zoomSlider.Maximum = 5;
zoomSlider.SmallChange = 1;
zoomSlider.LargeChange = 1;
zoomSlider.UseWaitCursor = false;
SetPictureBoxSize();
// reduce flickering
this.DoubleBuffered = true;
}
// picturebox size changed triggers paint event
private void SetPictureBoxSize()
{
Size s = new Size(Convert.ToInt32(imgOriginal.Width * zoomSlider.Value), Convert.ToInt32(imgOriginal.Height * zoomSlider.Value));
picBox.Size = s;
}
// looks for user trackbar changes
private void trackBar1_Scroll(object sender, EventArgs e)
{
if (zoomSlider.Value > 0)
{
SetPictureBoxSize();
}
}
// redraws image using nearest neighbour resampling
private void picBox_Paint_1(object sender, PaintEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
e.Graphics.DrawImage(
imgOriginal,
new Rectangle(0, 0, picBox.Width, picBox.Height),
// destination rectangle
0,
0, // upper-left corner of source rectangle
imgOriginal.Width, // width of source rectangle
imgOriginal.Height, // height of source rectangle
GraphicsUnit.Pixel);
}