Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads
 
Add your own
alternative version

Image Processing Lab in C#

, 13 Mar 2007 GPL3
A tool and library for image processing
iplab_demo.zip
iplab_demo
AForge.dll
AForge.Imaging.chm
AForge.Imaging.dll
AForge.Math.dll
DockingToolbar.dll
iplab.exe
SourceGrid2.dll
SourceLibrary.dll
WeifenLuo.WinFormsUI.dll
iplab_src.zip
iplab_src
icons
about.gif
clone.gif
convolution.gif
copy.gif
cut.gif
disk.gif
fit_to_size.gif
folder_out.gif
fourier.gif
grayscale.gif
histogram_window.gif
levels.gif
morphology.gif
paste.gif
pie-chart.gif
resize.gif
rotate.gif
threshold.gif
view_1_1.gif
zoom_in.gif
zoom_out.gif
iplab
App.ico
banner.gif
column-chart.ico
Controls
Filters Forms
line-chart.ico
References
AForge.dll
AForge.Imaging.dll
AForge.Math.dll
DockingToolbar.dll
SourceGrid2.dll
SourceLibrary.dll
WeifenLuo.WinFormsUI.dll
Resources
arrow.bmp
harrow.cur
hmove.cur
Tools
// 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)

Share

About the Author

Andrew Kirillov
Software Developer (Senior) Cisco Systems
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.
 
Going out of computers I am just a man loving his family, enjoying traveling, a bit of books, a bit of movies and a mixture of everything else. Always wanted to learn playing guitar, but it seems like 6 strings are much harder than few dozens of keyboard’s keys. Will keep progressing ...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411028.1 | Last Updated 13 Mar 2007
Article Copyright 2005 by Andrew Kirillov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid