Click here to Skip to main content
15,886,833 members
Articles / Multimedia / GDI+

Image Processing Lab in C#

Rate me:
Please Sign up or sign in to vote.
4.94/5 (533 votes)
13 Mar 2007GPL38 min read 5.7M   158.4K   1.2K  
A tool and library for image processing
// Image Processing Lab
//
// Copyright � Andrew Kirillov, 2005
// andrew.kirillov@gmail.com
//

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Reflection;
using AForge.Imaging.Filters;

namespace IPLab
{
    /// <summary>
    /// FilterPreview window
    /// </summary>
    public class FilterPreview : System.Windows.Forms.Control
    {
        private Bitmap previewImage;
        private Bitmap image;
        private IFilter filter;
        private Pen blackPen = new Pen( Color.Black, 1 );
        private Cursor cursorHand;
        private Cursor cursorHandMove;

        private int areaWidth, areaHeight;
        private int imageX, imageY;
        private bool tracking = false;

        private int startTrackingX, startTrackingY;
        private int oldImageX, oldImageY;

        // Image property
        [Browsable( false )]
        public Bitmap Image
        {
            get { return image; }
            set
            {
                image = value;

                if ( value != null )
                {
                    // calculate size of preview area
                    areaWidth = Math.Min( ClientRectangle.Width - 2, image.Width );
                    areaHeight = Math.Min( ClientRectangle.Height - 2, image.Height );

                    // calculate image position
                    imageX = ( image.Width - areaWidth ) >> 1;
                    imageY = ( image.Height - areaHeight ) >> 1;
                }

                RefreshFilter( );
            }
        }
        // Filter property
        [Browsable( false )]
        public AForge.Imaging.Filters.IFilter Filter
        {
            set
            {
                filter = value;
                RefreshFilter( );
            }
        }

        // Constructor
        public FilterPreview( )
        {
            InitializeComponent( );
            SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw |
                ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true );


            // load cursors
            Assembly assembly = this.GetType( ).Assembly;
            cursorHand = new Cursor( assembly.GetManifestResourceStream( "IPLab.Resources.harrow.cur" ) );
            cursorHandMove = new Cursor( assembly.GetManifestResourceStream( "IPLab.Resources.hmove.cur" ) );
        }

        // Dispose
        protected override void Dispose( bool disposing )
        {
            if ( disposing )
            {
                blackPen.Dispose( );

                if ( previewImage != null )
                {
                    previewImage.Dispose( );
                    cursorHand.Dispose( );
                    cursorHandMove.Dispose( );
                }
            }
            base.Dispose( disposing );
        }

        // Initialize control
        private void InitializeComponent( )
        {
            this.MouseUp += new System.Windows.Forms.MouseEventHandler( this.FilterPreview_MouseUp );
            this.MouseMove += new System.Windows.Forms.MouseEventHandler( this.FilterPreview_MouseMove );
            this.MouseDown += new System.Windows.Forms.MouseEventHandler( this.FilterPreview_MouseDown );
        }

        // Paint control
        protected override void OnPaint( PaintEventArgs pe )
        {
            Graphics g = pe.Graphics;
            Rectangle rc = ClientRectangle;
            int width = rc.Width;
            int height = rc.Height;
            int x, y;

            // calculate size of preview area
            if ( image != null )
            {
                width = areaWidth + 2;
                height = areaHeight + 2;
            }
            // calculate position of preview area
            x = ( rc.Width - width ) >> 1;
            y = ( rc.Height - height ) >> 1;

            // draw rectangle
            g.DrawRectangle( blackPen, x, y, width - 1, height - 1 );

            x++;
            y++;

            if ( image != null )
            {
                if ( previewImage == null )
                {
                    // draw original image
                    g.DrawImage( image, new Rectangle( x, y, areaWidth, areaHeight ), imageX, imageY, areaWidth, areaHeight, GraphicsUnit.Pixel );
                }
                else
                {
                    // draw preview image
                    g.DrawImage( previewImage, x, y, areaWidth, areaHeight );
                }
            }

            // Calling the base class OnPaint
            base.OnPaint( pe );
        }

        // Refresh preview
        public void RefreshFilter( )
        {
            // release old image
            if ( previewImage != null )
            {
                previewImage.Dispose( );
                previewImage = null;
            }

            if ( ( image != null ) && ( filter != null ) )
            {
                Bitmap tmp = image.Clone( new Rectangle( imageX, imageY, areaWidth, areaHeight ), image.PixelFormat );

                try
                {
                    previewImage = filter.Apply( tmp );
                }
                catch ( Exception )
                {
                }

                // release temp image
                tmp.Dispose( );
            }

            // repaint
            Invalidate( );
        }

        // On mouse move
        private void FilterPreview_MouseMove( object sender, System.Windows.Forms.MouseEventArgs e )
        {
            if ( image != null )
            {
                if ( !tracking )
                {
                    // calculate position of preview area
                    int x = ( ( ClientRectangle.Width - areaWidth - 2 ) >> 1 ) + 1;
                    int y = ( ( ClientRectangle.Height - areaHeight - 2 ) >> 1 ) + 1;

                    // check mouse coordinates
                    if ( ( e.X >= x ) && ( e.Y >= y ) &&
                        ( e.X < x + areaWidth ) && ( e.Y < y + areaHeight ) )
                    {
                        Cursor = cursorHand;
                    }
                    else
                        Cursor = Cursors.Default;
                }
                else
                {
                    int dx = e.X - startTrackingX;
                    int dy = e.Y - startTrackingY;

                    imageX = Math.Max( 0, Math.Min( image.Width - areaWidth, oldImageX - dx ) );
                    imageY = Math.Max( 0, Math.Min( image.Height - areaHeight, oldImageY - dy ) );

                    Invalidate( );

                    Cursor = cursorHandMove;
                }
            }
        }

        // On mouse button down
        private void FilterPreview_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
        {
            if ( ( image != null ) && ( e.Button == MouseButtons.Left ) )
            {
                // start tracking
                tracking = true;
                Capture = true;

                startTrackingX = e.X;
                startTrackingY = e.Y;

                oldImageX = imageX;
                oldImageY = imageY;

                // release preview image
                if ( previewImage != null )
                {
                    previewImage.Dispose( );
                    previewImage = null;
                }
                // repaint
                Invalidate( );

                // set cursor
                Cursor = cursorHandMove;
            }
        }

        // On mouse button up
        private void FilterPreview_MouseUp( object sender, System.Windows.Forms.MouseEventArgs e )
        {
            if ( tracking )
            {
                // stop tracking
                tracking = false;
                Capture = false;

                RefreshFilter( );

                // set cursor
                Cursor = cursorHand;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer IBM
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET, Computer Vision Sandbox, cam2web, ANNT, etc.

Comments and Discussions