Click here to Skip to main content
15,885,886 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.3K   1.2K  
A tool and library for image processing
// Image Processing Lab
//
// Copyright � Andrew Kirillov, 2005
// andrew.kirillov@gmail.com
//

namespace IPLab
{
    using System;
    using System.IO;

    /// <summary>
    /// Serialize2DimArray class - 2 dimensional arrays serialization
    /// </summary>
    internal class Serialize2DimArray
    {
        // Save array to file
        public static void Save( string file, Array array )
        {
            StreamWriter writer = null;

            try
            {
                int height = array.GetLength( 0 );
                int width = array.GetLength( 1 );

                // create file
                writer = File.CreateText( file );

                // write array dimensions
                writer.WriteLine( "{0} {1}", height, width );

                for ( int i = 0; i < height; i++ )
                {
                    string[] strs = new string[width];

                    for ( int j = 0; j < width; j++ )
                    {
                        strs[j] = array.GetValue( i, j ).ToString( );
                    }

                    writer.WriteLine( string.Join( " ", strs ) );
                }
            }
            catch ( Exception )
            {
                throw new ApplicationException( );
            }
            finally
            {
                // close file
                if ( writer != null )
                    writer.Close( );
            }
        }

        // Load array from file
        public static Array Load( string file, Type type )
        {
            StreamReader reader = null;
            Array array;

            try
            {
                string[] strs;
                int height;
                int width;

                // open file
                reader = File.OpenText( file );

                // read dimensions
                strs = reader.ReadLine( ).Split( ' ' );
                height = int.Parse( strs[0] );
                width = int.Parse( strs[1] );

                // create array of specified type
                array = Array.CreateInstance( type, height, width );

                for ( int i = 0; i < height; i++ )
                {
                    // read next line
                    strs = reader.ReadLine( ).Split( ' ' );
                    for ( int j = 0; j < width; j++ )
                    {
                        array.SetValue( Convert.ChangeType( strs[j], type ), i, j );
                    }
                }
            }
            catch ( Exception )
            {
                throw new ApplicationException( );
            }
            finally
            {
                // close file
                if ( reader != null )
                    reader.Close( );
            }
            return array;
        }
    }
}

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