Click here to Skip to main content
14,690,608 members
Articles » Desktop Development » Miscellaneous » Custom Controls
Posted 28 Jan 2014

Tagged as


13 bookmarked

PixelBox: A PictureBox with configurable InterpolationMode

Rate me:
Please Sign up or sign in to vote.
4.83/5 (16 votes)
28 Jan 2014CPOL
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): 

 Image 1

This is what I got (InterpolationType.Default): 

Image 2


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:


Image 3


Image 4


Image 5


Image 6


Image 7


Image 8


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 30 years of experience in information systems and software development for the industry. He is Senior Controls Engineer at OTTO Motors

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.

Comments and Discussions

QuestionYou forgot a detail... Pin
Nyerguds19-Feb-19 2:17
MemberNyerguds19-Feb-19 2:17 
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.