Leaders snarfblam Posted December 18, 2006 Leaders Posted December 18, 2006 (edited) 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 { /// <summary> /// A PictureBox control extended to allow a variety of interpolations. /// </summary> class InterpolatedBox:pictureBox { #region Interpolation Property /// <summary>Backing Field</summary> private InterpolationMode interpolation = InterpolationMode.Default; /// <summary> /// The interpolation used to render the image. /// </summary> [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 /// <summary> /// Overridden to modify rendering behavior. /// </summary> /// <param name="pe">Painting event args.</param> 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 ''' <summary>A PictureBox control extended to allow a variety of interpolations.</summary> Public Class InterpolatedBox Inherits PictureBox ''' <summary>The backing field for the Interpolation property</summary> Dim _interpolation As InterpolationMode = InterpolationMode.Default ''' <summary> ''' The interpolation used to render the image. ''' </summary> <DefaultValue(GetType(InterpolationMode), "Default"), _ 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 ''' <summary> ''' Overridden to modify rendering behavior. ''' </summary> ''' <param name="pe">Painting event args.</param> 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 Edited October 15, 2021 by AWS Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.