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
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using AForge.Imaging;

namespace IPLab
{
    // HuePickerType enumeration
    public enum HuePickerType
    {
        Value,
        Region
    }

    /// <summary>
    /// Summary description for HuePicker.
    /// </summary>
    public class HuePicker : System.Windows.Forms.Control
    {
        private HuePickerType type = HuePickerType.Value;

        private Pen blackPen;
        private Brush blackBrush;
        private Pen whitePen;
        private Brush whiteBrush;
        private Point ptCenter = new Point( 0, 0 );
        private Point ptMin = new Point( 0, 0 );
        private Point ptMax = new Point( 0, 0 );
        private int trackMode = 0;

        private int min = 0;
        private int max = 359;

        // values changed event
        public event EventHandler ValuesChanged;

        // Min property
        [DefaultValue( 0 )]
        public int Min
        {
            get { return min; }
            set
            {
                min = Math.Max( 0, Math.Min( 359, value ) );
                Invalidate( );
            }
        }
        // Max property
        [DefaultValue( 359 )]
        public int Max
        {
            get { return max; }
            set
            {
                max = Math.Max( 0, Math.Min( 359, value ) );
                Invalidate( );
            }
        }
        // Type property
        [DefaultValue( HuePickerType.Value )]
        public HuePickerType Type
        {
            get { return type; }
            set
            {
                type = value;
                Invalidate( );
            }
        }

        // Constructor
        public HuePicker( )
        {
            InitializeComponent( );

            SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw |
                ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true );

            blackPen = new Pen( Color.Black, 1 );
            blackBrush = new SolidBrush( Color.Black );
            whitePen = new Pen( Color.White, 1 );
            whiteBrush = new SolidBrush( Color.White );
        }

        // Dispose
        protected override void Dispose( bool disposing )
        {
            if ( disposing )
            {
                blackPen.Dispose( );
                blackBrush.Dispose( );
                whitePen.Dispose( );
                whiteBrush.Dispose( );
            }
            base.Dispose( disposing );
        }

        // Init component
        private void InitializeComponent( )
        {
            // 
            // HSLPicker
            // 
            this.MouseUp += new System.Windows.Forms.MouseEventHandler( this.HSLPicker_MouseUp );
            this.MouseMove += new System.Windows.Forms.MouseEventHandler( this.HSLPicker_MouseMove );
            this.MouseDown += new System.Windows.Forms.MouseEventHandler( this.HSLPicker_MouseDown );

        }

        // Paint control
        protected override void OnPaint( PaintEventArgs pe )
        {
            Graphics g = pe.Graphics;
            Rectangle rc = this.ClientRectangle;
            Rectangle rcPie;
            Brush brush;
            RGB rgb = new RGB( );
            HSL hsl = new HSL( );

            // get pie rectangle
            rcPie = new Rectangle( 4, 4, Math.Min( rc.Right, rc.Bottom ) - 8, Math.Min( rc.Right, rc.Bottom ) - 8 );

            // init HSL value
            hsl.Luminance = 0.5;
            hsl.Saturation = 1.0;

            if ( type == HuePickerType.Value )
            {
                // draw HSL pie
                for ( int i = 0; i < 360; i++ )
                {
                    hsl.Hue = i;
                    // convert from HSL to RGB
                    AForge.Imaging.ColorConverter.HSL2RGB( hsl, rgb );
                    // create brush
                    brush = new SolidBrush( rgb.Color );
                    // draw one hue value
                    g.FillPie( brush, rcPie, -i, -1 );

                    brush.Dispose( );
                }
            }
            else
            {
                // draw HSL pie
                for ( int i = 0; i < 360; i++ )
                {
                    if ( ( ( min < max ) && ( i >= min ) && ( i <= max ) ) ||
                        ( ( min > max ) && ( ( i >= min ) || ( i <= max ) ) ) )
                    {
                        hsl.Hue = i;
                        // convert from HSL to RGB
                        AForge.Imaging.ColorConverter.HSL2RGB( hsl, rgb );
                        // create brush
                        brush = new SolidBrush( rgb.Color );
                    }
                    else
                    {
                        brush = new SolidBrush( Color.FromArgb( 128, 128, 128 ) );
                    }

                    // draw one hue value
                    g.FillPie( brush, rcPie, -i, -1 );

                    brush.Dispose( );
                }
            }

            //
            double halfWidth = (double) rcPie.Width / 2;
            double angleRad = -min * Math.PI / 180;
            double angleCos = Math.Cos( angleRad );
            double angleSin = Math.Sin( angleRad );

            double x = halfWidth * angleCos;
            double y = halfWidth * angleSin;

            ptCenter.X = rcPie.Left + (int) ( halfWidth );
            ptCenter.Y = rcPie.Top + (int) ( halfWidth );
            ptMin.X = rcPie.Left + (int) ( halfWidth + x );
            ptMin.Y = rcPie.Top + (int) ( halfWidth + y );

            // draw MIN pointer
            g.FillEllipse( blackBrush,
                rcPie.Left + (int) ( halfWidth + x ) - 4,
                rcPie.Top + (int) ( halfWidth + y ) - 4,
                8, 8 );
            g.DrawLine( blackPen, ptCenter, ptMin );

            // check picker type
            if ( type == HuePickerType.Region )
            {
                angleRad = -max * Math.PI / 180;
                angleCos = Math.Cos( angleRad );
                angleSin = Math.Sin( angleRad );

                x = halfWidth * angleCos;
                y = halfWidth * angleSin;

                ptMax.X = rcPie.Left + (int) ( halfWidth + x );
                ptMax.Y = rcPie.Top + (int) ( halfWidth + y );

                // draw MAX pointer
                g.FillEllipse( whiteBrush,
                    rcPie.Left + (int) ( halfWidth + x ) - 4,
                    rcPie.Top + (int) ( halfWidth + y ) - 4,
                    8, 8 );
                g.DrawLine( whitePen, ptCenter, ptMax );
            }

            base.OnPaint( pe );
        }

        // On mouse down
        private void HSLPicker_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
        {
            // check coordinates of MIN pointer
            if ( ( e.X >= ptMin.X - 4 ) && ( e.Y >= ptMin.Y - 4 ) &&
                ( e.X < ptMin.X + 4 ) && ( e.Y < ptMin.Y + 4 ) )
            {
                trackMode = 1;
            }
            if ( type == HuePickerType.Region )
            {
                // check coordinates of MAX pointer
                if ( ( e.X >= ptMax.X - 4 ) && ( e.Y >= ptMax.Y - 4 ) &&
                    ( e.X < ptMax.X + 4 ) && ( e.Y < ptMax.Y + 4 ) )
                {
                    trackMode = 2;
                }
            }

            if ( trackMode != 0 )
                this.Capture = true;
        }

        // On mouse up
        private void HSLPicker_MouseUp( object sender, System.Windows.Forms.MouseEventArgs e )
        {
            if ( trackMode != 0 )
            {
                // release capture
                this.Capture = false;
                trackMode = 0;

                // notify client
                if ( ValuesChanged != null )
                    ValuesChanged( this, new EventArgs( ) );
            }
        }

        // On mouse move
        private void HSLPicker_MouseMove( object sender, System.Windows.Forms.MouseEventArgs e )
        {
            if ( trackMode != 0 )
            {
                int dy = e.Y - ptCenter.Y;
                int dx = e.X - ptCenter.X;

                if ( trackMode == 1 )
                {
                    // MIN pointer tracking
                    if ( dx == 0 )
                    {
                        min = ( dy < 0 ) ? 90 : 270;
                    }
                    else
                    {
                        min = ( (int) ( -Math.Atan( (double) dy / dx ) * 180 / Math.PI ) );

                        if ( dx < 0 )
                            min = 180 + min;
                        if ( min < 0 )
                            min = 360 + min;
                    }
                }
                else
                {
                    // MAX pointer tracking
                    if ( dx == 0 )
                    {
                        max = ( dy < 0 ) ? 90 : 270;
                    }
                    else
                    {
                        max = ( (int) ( -Math.Atan( (double) dy / dx ) * 180 / Math.PI ) );

                        if ( dx < 0 )
                            max = 180 + max;
                        if ( max < 0 )
                            max = 360 + max;
                    }
                }

                // repaint control
                Invalidate( );
            }
        }
    }
}

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 | Mobile
Web02 | 2.8.141022.2 | Last Updated 13 Mar 2007
Article Copyright 2005 by Andrew Kirillov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid