Click here to Skip to main content
15,884,425 members
Articles / Desktop Programming / Windows Forms

PixelBox: A PictureBox with configurable InterpolationMode

Rate me:
Please Sign up or sign in to vote.
4.83/5 (16 votes)
28 Jan 2014CPOL2 min read 32.4K   997   14   6
The standard Windows Forms PictureBox is missing one very useful line of code.

Introduction 

It's not often that we work with extremely low-resolution, or pixelated images, but when we do we may have differing expectations of what that image should look like.

In a recent project I wanted to show the pixelation of the images, but when shown in the default PictureBox, the pixelation could not be seen, because the control was smoothing the pixelation.

This is what I wanted (InterpolationType.NearestNeighbor): 

 Image 1

This is what I got (InterpolationType.Default): 

Image 2

Background 

Windows.Forms.Controls.PictureBox is the standard control that is used to display images within a Windows Forms application. It does a decent job of this and offers some customization options, such as SizeMode, to set how an image should fit in the box.

When displaying an image at any size other than the images original size i.e. pixel-to-pixel, the extra pixels used to turn a low-resolution image into a higher one, and vise-versa have to come from somewhere. This is called interpolation, and it turns out there are many algorithms to accomplish this.

PixelBox is a custom control that is derived from PictureBox. It adds the InterpolationMode property, which has the effect of setting the GDI+ interpolation mode before the image is drawn onto the control's surface. The property is a value from the InterpolationMode enumeration.

The code 

The changes to PictureBox are quite simple. The entire class is shown below: 

C#
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
namespace RedCell.UI.Controls
{
    /// <summary>
    /// A PictureBox with configurable interpolation mode.
    /// </summary>
    public class PixelBox : PictureBox
    {
        #region Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="PixelBox"> class.
        /// </see></summary>
        public PixelBox ()
        {
            // Set default.
            InterpolationMode = InterpolationMode.Default;
        }
        #endregion
 
        #region Properties
        /// <summary>
        /// Gets or sets the interpolation mode.
        /// </summary>
        /// <value>The interpolation mode.</value>
        [Category("Behavior")]
        [DefaultValue(InterpolationMode.Default)]
        public InterpolationMode InterpolationMode { get; set; }
        #endregion
 
        #region Overrides of PictureBox
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"> event.
        /// </see></summary>
        /// <param name="pe" />A <see cref="T:System.Windows.Forms.PaintEventArgs"> that contains the event data. 
        protected override void OnPaint (PaintEventArgs pe)
        {
            pe.Graphics.InterpolationMode = InterpolationMode;
            base.OnPaint(pe);
        }
        #endregion
    }
}
</see>

Demonstration

The demonstration application demonstrates any of the interpolation modes on a pixelated image. In addition to those shown above, here are the other modes for comparison:

InterpolationMode.Low 

Image 3

InterpolationMode.High 

Image 4

InterpolationMode.Bilinear 

Image 5

InterpolationMode.Bicubic 

Image 6

InterpolationMode.HighQualityBilinear 

Image 7

InterpolationMode.HighQualityBicubic 

Image 8

Conclusion

By subclassing PictureBox and overriding its OnPaint method we can set the GDI+ InterpolationMode property, giving us greater control over the algorithm, quality, and speed of rendering.   

History

January 28, 2014

Initial publication.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Robotic Assistance Devices / AITX
Canada Canada
Yvan Rodrigues has 30 years of experience in information systems and software development for the industry. He is Senior Concept Designer at Robotic Assistance Devices

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., Mabel's Labels Inc. as Manager of Systems and Development, the University of Waterloo as Information Systems Manager, and OTTO Motors as Senior Systems Engineer and Senior Concept Designer.

Yvan is currently focused on design of embedded systems.

Comments and Discussions

 
QuestionYou forgot a detail... Pin
Nyerguds19-Feb-19 1:17
Nyerguds19-Feb-19 1:17 
Praisepraise... Pin
Member 134015989-Sep-17 9:09
Member 134015989-Sep-17 9:09 
QuestionVery interesting article about the pixelation of the images Pin
Volynsky Alex29-Jan-14 10:10
professionalVolynsky Alex29-Jan-14 10:10 
AnswerRe: Very interesting article about the pixelation of the images Pin
Yvan Rodrigues29-Jan-14 10:14
professionalYvan Rodrigues29-Jan-14 10:14 
GeneralRe: Very interesting article about the pixelation of the images Pin
Volynsky Alex29-Jan-14 10:17
professionalVolynsky Alex29-Jan-14 10:17 
GeneralRe: Very interesting article about the pixelation of the images Pin
.:floyd:.4-Jul-14 2:12
.:floyd:.4-Jul-14 2:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.