How do I make a PictureBox use Nearest Neighbor resampling?
Asked Answered
G

3

32

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 moire patterns.

Gladygladys answered 26/8, 2008 at 23:7 Comment(3)
so there is no actual way to do this? in some easy fashion?Bohon
@Luiscencio: that's what it looks like. You'll have to do it yourself with a new Bitmap of the appropriate size and then Graphics.DrawImageGladygladys
You should mark JYelton answer. :)Corn
M
38

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:

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);
    }
}
Mccafferty answered 20/11, 2012 at 23:40 Comment(9)
Very nice. I think PanAndZoomPictureBox of EmguCV do the same. Are you aware of any performance issue doing it?Corn
I haven't had any measurable performance differences changing the interpolation mode in this way.Mccafferty
Lovely answer. I suggest posters be a bit more complete with their code, i.e., add a using System.Drawing.Drawing2D or put the full namespace in front of the InterpolationMode declaration.Cavill
Note that with crisp zoom you'll see that the image, when zoomed, is offset by half a pixel. To fix it, set paintEventArgs.Graphics.PixelOffsetMode = PixelOffsetMode.Half; This property is named wrongly; putting it to Half makes it not shift the whole thing up and to the left by half a (zoomed) pixel.Brom
@pelesl I added the namespace declarations in using statements for clarity.Mccafferty
And how do we use this class? How can I place a PictureBoxWithInterpolationMode on my form?Clarify
@Clarify You can either manually change the Designer.cs file (replace PictureBox with the new control) or add the new control to your toolbox. It's been years since I worked in VS but you should be able to find it using the phrase "add custom usercontrol to toolbox."Mccafferty
@JYelton, Thanks. I figured out to edit the designer.cs to include the component, but it would be cool to add this new control as a custom component to the toolbox - it's easier to drag and drop.Clarify
It would be useful to adjust the InterpolationMode` setter to call Invalidate. That way, if you change the InterpolationMode, the PictureBox will update.Vanquish
S
5

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.

Shyamal answered 26/8, 2008 at 23:41 Comment(0)
T
-4

When resizing an image in .net, the System.Drawing.Drawing2D.InterpolationMode offers the following resize methods:

  • Bicubic
  • Bilinear
  • High
  • HighQualityBicubic
  • HighQualityBilinear
  • Low
  • NearestNeighbor
  • Default
Tucky answered 26/8, 2008 at 23:47 Comment(1)
I don't see how this addresses the OP's question.Mccafferty

© 2022 - 2024 — McMap. All rights reserved.