C# : Image operations performer

This article performs operations related to Images. Currently I have written it so that it can encrypt and decrypt image, can generate thumbnail image and can generate grayscal image. I have also given the main form and outputs. I will include much more operations as I got the time.

Develop the form looks like below: You can put any image in picture box. I have taken this example so that I can show you the outputs. The image I have used can be found at
http://www.eggheadcafe.com/fileupload/-698781408_img.jpg




Outputs:

GrayScal Image OutPut:



Thumnail Image Output:



The Complete C# code for all listed operations:



using System;
using System.Drawing;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace AllAboutImages
{
    public partial class Form1 : Form
    {
         string inputPath = string.Empty;
        string outputPath = string.Empty;
        private string mySecretKey = string.Empty;

        [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
        public static extern bool ZeroMemory(IntPtr Destination, int Length);
        static string getSecretKey()
        {
            DESCryptoServiceProvider desKey = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            return Encoding.ASCII.GetString(desKey.Key);
         }

        public Form1()
        {
             InitializeComponent();
        }

         private bool loadValues()
        {
            inputPath = InputPathTextBox.Text;
            outputPath = OutputPathTextBox.Text;
             if (inputPath == string.Empty || outputPath == string.Empty)
             {
                 MessageBox.Show("Enter valid input and output path values", "Image Converter", MessageBoxButtons.OK,
                                  MessageBoxIcon.Error);
                 return false;
            }
            return true;
        }

         private void EncryptButton_Click(object sender, EventArgs e)
        {
            //Encrypt the image using DES algorithm
            if (!loadValues()) return;
            mySecretKey = getSecretKey();
            GCHandle garbageCollectionHandle = GCHandle.Alloc(mySecretKey, GCHandleType.Pinned);
            FileStream fin = new FileStream(inputPath, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(outputPath, FileMode.Create, FileAccess.Write);
            DESCryptoServiceProvider desKey = new DESCryptoServiceProvider();
            desKey.Key = Encoding.ASCII.GetBytes(mySecretKey);
            desKey.IV = Encoding.ASCII.GetBytes(mySecretKey);
            ICryptoTransform desencrypt = desKey.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fout, desencrypt, CryptoStreamMode.Write);
             byte[] bytearrayinput = new byte[fin.Length];
            fin.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Close();
            fin.Close();
            fout.Close();
            ZeroMemory(garbageCollectionHandle.AddrOfPinnedObject(), mySecretKey.Length * 2);
             garbageCollectionHandle.Free();
             MessageBox.Show("Encrypted image succesully at " + outputPath, "Image Converter", MessageBoxButtons.OK,
                                  MessageBoxIcon.Information);
        }

         private void DecryptButton_Click(object sender, EventArgs e)
        {
            //decrypt the image using DES algorithm
            if (!loadValues()) return;
            mySecretKey = getSecretKey();
            GCHandle garbageCollectionHandle = GCHandle.Alloc(mySecretKey, GCHandleType.Pinned);
            DESCryptoServiceProvider desKey = new DESCryptoServiceProvider();
            desKey.Key = Encoding.ASCII.GetBytes(mySecretKey);
            desKey.IV = Encoding.ASCII.GetBytes(mySecretKey);
            FileStream fin = new FileStream(inputPath, FileMode.Open, FileAccess.Read);
            ICryptoTransform desTrans = desKey.CreateDecryptor();
            CryptoStream cs = new CryptoStream(fin, desTrans, CryptoStreamMode.Read);
            StreamWriter fout = new StreamWriter(outputPath);
             fout.Write(new StreamReader(cs).ReadToEnd());
            fout.Flush();
            fout.Close();
            ZeroMemory(garbageCollectionHandle.AddrOfPinnedObject(), mySecretKey.Length * 2);
             garbageCollectionHandle.Free();
             MessageBox.Show("Decrypted image succesully at " + outputPath, "Image Converter", MessageBoxButtons.OK,
                                  MessageBoxIcon.Information);

        }

         private void GrayScalButton_Click(object sender, EventArgs e)
        {
            //Convert the picturbox image to grayscal
            Bitmap myImage = (Bitmap)pictureBox1.Image.Clone();
             int h = myImage.Size.Height;
             int w = myImage.Size.Width;

             for (int y = 0; y < h; y++)      
                 for (int x = 0; x < w; x++)
                {                  
                    Color c = myImage.GetPixel(x, y);            
                     int gray = (c.R + c.G + c.B) / 3;        
                    myImage.SetPixel(x, y, Color.FromArgb(gray,gray,gray));
                }                
            MessageBox.Show("Converted image to grayscal..Click ok to see.", "Image Converter", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            pictureBox1.Image = myImage;
        }

        private void ThumbNailButton_Click(object sender, EventArgs e)
        {
            //This function will generate thumbnail image of size 100 x 100
            if (!loadValues()) return;
            Image myImage = Image.FromFile(inputPath);        
            Bitmap thumbNailImg = new Bitmap(myImage, 100, 100);
             thumbNailImg.Save(outputPath);
             MessageBox.Show("Created thumbnail successfully at " + outputPath, "Image Converter", MessageBoxButtons.OK,
                                  MessageBoxIcon.Information);
        }

         private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
By Perry    Popularity  (3367 Views)