Click here to Skip to main content
13,409,520 members (40,046 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 28 Jan 2014

PixelBox: A PictureBox with configurable InterpolationMode

, 28 Jan 2014
Rate this:
Please Sign up or sign in to vote.
The standard Windows Forms PictureBox is missing one very useful line of code.


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): 


This is what I got (InterpolationType.Default): 


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: 

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;
        #region Properties
        /// <summary>
        /// Gets or sets the interpolation mode.
        /// </summary>
        /// <value>The interpolation mode.</value>
        public InterpolationMode InterpolationMode { get; set; }
        #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;


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:








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.   


January 28, 2014

Initial publication.


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


About the Author

Yvan Rodrigues
Systems Engineer Clearpath Robotics
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the industry. He is Senior Systems Developer at Clearpath Robotics

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, and the University of Waterloo as Information Systems Manager.

Yvan supports open-source software. He is a committer for SharpKit (C# to Javascript cross-compiler) and WebIssues (Issue/Ticket Management System), TinyMCE (JavaScript editor), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, and Xamarin.

You may also be interested in...

Comments and Discussions

Praisepraise... Pin
Member 134015989-Sep-17 10:09
memberMember 134015989-Sep-17 10:09 
QuestionVery interesting article about the pixelation of the images Pin
Volynsky Alex29-Jan-14 11:10
professionalVolynsky Alex29-Jan-14 11:10 
AnswerRe: Very interesting article about the pixelation of the images Pin
Yvan Rodrigues29-Jan-14 11:14
professionalYvan Rodrigues29-Jan-14 11:14 
GeneralRe: Very interesting article about the pixelation of the images Pin
Volynsky Alex29-Jan-14 11:17
professionalVolynsky Alex29-Jan-14 11:17 
GeneralRe: Very interesting article about the pixelation of the images Pin
.:floyd:.4-Jul-14 3:12
member.:floyd:.4-Jul-14 3: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180221.1 | Last Updated 28 Jan 2014
Article Copyright 2014 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid