Code Library A repository of sample code

Go Back  Xtreme .NET Talk > Knowledge Base > Code Library > Interpolated Picture Box


Reply
 
Thread Tools Display Modes
  #1  
Old 12-17-2006, 07:53 PM
snarfblam's Avatar
snarfblam snarfblam is offline
Ultimate Contributor

Preferred language:
C#, VB
 
Join Date: Jun 2003
Location: USA
Posts: 2,097
snarfblam will become famous soon enough
Default Interpolated Picture Box



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.
Code:
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.
Code:
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
__________________
e
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Picture Box lothos12345 Graphics and Multimedia 1 10-25-2005 03:27 PM
Put a picture from a file, in a picture box... decrypt Graphics and Multimedia 5 05-28-2004 05:27 PM
MDI picture box fadi Windows Forms 5 03-24-2004 09:51 AM
picture in message box whosyodaddy Windows Forms 4 06-24-2003 04:20 PM
Delete Picture Box Picture hercemer Windows Forms 5 06-12-2003 08:04 AM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe