Jump to content
Xtreme .Net Talk
  • Ever wish the picture box could scale images without using bilinear filtering (for example, when viewing/editing video game graphics)? In the past I had written controls from scratch that emulate the PictureBox but used my desired interpolation. Then I was hit with a stroke of common sense. I've extended the PictureBox class to enhance it with the ability to use various interpolation modes, including the video game creator's best friend, nearest neighbor.

    using System.Windows.Forms;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    using System;
    
    namespace iLab
    {
       /// 
       /// A PictureBox control extended to allow a variety of interpolations.
       /// 
       class InterpolatedBox:pictureBox
       {
           #region Interpolation Property
           /// Backing Field
           private InterpolationMode interpolation = InterpolationMode.Default;
    
           /// 
           /// The interpolation used to render the image.
           /// 
           [DefaultValue(typeof(InterpolationMode), "Default"),
           Description("The interpolation used to render the image.")]
           public InterpolationMode Interpolation {
               get { return interpolation; }
               set {
                   if(value == InterpolationMode.Invalid)
                       throw new ArgumentException("\"Invalid\" is not a valid value."); // (Duh!)
    
                   interpolation = value;
                   Invalidate(); // Image should be redrawn when a different interpolation is selected
               }
           }
           #endregion
    
           /// 
           /// Overridden to modify rendering behavior.
           /// 
           /// Painting event args.
           protected override void OnPaint(PaintEventArgs pe) {
               // Before the PictureBox renders the image, we modify the
               // graphics object to change the interpolation.
    
               // Set the selected interpolation.
               pe.Graphics.InterpolationMode = interpolation;
               // Certain interpolation modes (such as nearest neighbor) need
               // to be offset by half a pixel to render correctly.
               pe.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
    
               // Allow the PictureBox to draw.
               base.OnPaint(pe);
           }
       }
    }
    

    Since I'm such a swell guy, I translated it to VB too.

    Imports System.Drawing.Drawing2D
    Imports System.ComponentModel
    
    ''' A PictureBox control extended to allow a variety of interpolations.
    Public Class InterpolatedBox
       Inherits PictureBox
    
       ''' The backing field for the Interpolation property
       Dim _interpolation As InterpolationMode = InterpolationMode.Default
    
       ''' 
       ''' The interpolation used to render the image.
       ''' 
           Description("The interpolation used to render the image.")> _
       Public Property Interpolation() As InterpolationMode
           Get
               Return _interpolation
           End Get
           Set(ByVal value As InterpolationMode)
               If value = InterpolationMode.Invalid Then _
                   Throw New ArgumentException("""Invalid"" is not a valid value.") '(duh.)
    
               _interpolation = value
               Invalidate() 'Image should be redrawn when a different interpolation is selected
           End Set
       End Property
    
       ''' 
       ''' Overridden to modify rendering behavior.
       ''' 
       ''' Painting event args.
       Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
           ' Before the PictureBox renders the image, we modify the
           ' graphics object to change the interpolation.
    
           ' Set the selected interpolation.
           pe.Graphics.InterpolationMode = _interpolation
           ' Certain interpolation modes (such as nearest neighbor) need
           ' to be offset by half a pixel to render correctly.
           pe.Graphics.PixelOffsetMode = PixelOffsetMode.Half
    
           ' Allow the PictureBox to draw.
           MyBase.OnPaint(pe)
       End Sub
    End Class
    


    User Feedback

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest

×
×
  • Create New...