如何使PictureBox使用最近邻居重采样?

问题描述:

我使用StretchImage,因为该框可以使用分隔线调整大小。它看起来像默认是某种平滑的双线性过滤,导致我的图像模糊,并有波纹图案。如何使PictureBox使用最近邻居重采样?

+1

所以没有实际的方法来做到这一点?在一些简单的时尚? – Luiscencio 2010-10-25 20:18:01

+0

@Luiscencio:这就是它的样子。你必须自己做一个适当大小的新位图,然后Graphics.DrawImage – 2010-10-25 21:33:55

+0

你应该标记JYelton的答案。 :) – Pedro77 2013-05-30 22:28:20

我需要这个功能也。我做了一个继承PictureBox的类,覆盖OnPaint,并添加了一个属性以允许插值模式被设置:

/// <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); 
    } 
} 

我怀疑你将不得不手动调整大小,通过Image类和DrawImage函数并响应PictureBox上的调整大小事件。

调整时在.NET中的形象,System.Drawing.Drawing2D.InterpolationMode提供了以下调整方法:

  • 双三次
  • 双线性
  • HighQualityBicubic
  • HighQualityBilinear
  • NearestN eighbor
  • 默认
+0

我不明白这是如何解决OP的问题。 – JYelton 2012-11-20 23:29:04

我做了MSDN搜索和原来有这个的文章,这是不是很详细,但列出了你应该使用Paint事件。

http://msdn.microsoft.com/en-us/library/k0fsyd4e.aspx

我编辑了常用的图像缩放例如要使用此功能,请参阅下面

从编辑:http://www.dotnetcurry.com/ShowArticle.aspx?ID=196&AspxAutoDetectCookieSupport=1

希望这有助于

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); 
    }