SEARCH C# PROGRAMMING CODES HERE

ImageEditor in C# Visual Studio 2017



ImageEditor in C #














Start a new project in Visual Studio > choose Visual C# then click on
Windows Desktop then select WindowsForm App(.Net Framework) 
and name it as ImageEditor then press ok.
First, go to the Form1 properties Section and set the Size as following.
Form1-size=952, 607
Now Add 3 panels from the toolbox and set the properties as follows.
After setting the properties it will look as follows.
Now Add 2-picture box from the toolbox and go to the properties and set the properties as follows.
Now add 15-Button from the toolbox.
Now add 3-textbox from the toolbox.
Now add 3-trackbar and 5-label from the toolbox.
Now go to the properties section and check all the Components are named correctly or not.


Once all the Components are named correctly then move to the coding part.
(Now the Coding Part)

Now go to the Form1 code view and Add the Namespace as following.
using System;
using System. Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
Now follow these steps.
Step-1-btn_open
// To open Image file.
 public partial class Form1 : Form
    {
        Image img;
        OpenFileDialog ofd = new OpenFileDialog();
 public Form1()
        {
            InitializeComponent();
        }
// Now go to the btn_open _click event and copy the following code.
    
 private void btn_open_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            ofd.Filter = "images|*.png;*.jpg;*.jpeg;*.gif";

            if(ofd.ShowDialog()==DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(ofd.FileName);
                pictureBox2.Image = new Bitmap(ofd.FileName);
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                txt_imgpath.Text = ofd.FileName;
                img = Image.FromFile(ofd.FileName);
                lbl_size.Text = pictureBox1.Image.Size.ToString(); // To displaye the image size//
            }
        }
    }


Step-2-btn_save
// To Save Image file go to the btn_save _click event and copy the following code.

 private void btn_save_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "JPEG|*.JPG|PNG|*.PNG|GIF|*.GIF|BMP|*.BMP|All files (*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (Bitmap bitmap = new Bitmap(pictureBox2.Image))
                    {
                        bitmap.Save(sfd.FileName, ImageFormat.Jpeg);
                    }
                    MessageBox.Show("Image Saved Successfully.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(" Error );
                }
            }
        }

Step-3-btn_Resize
 // To Resize Image Width & Height copy the Resize function as following.

 new Image Resize(Image image,int width,int height)
        {
            Bitmap bmp = new Bitmap(width, height);
            Graphics graphic = Graphics.FromImage(bmp);
            graphic.DrawImage(image, 0, 0, width, height);
            graphic.Dispose();
            return bmp;
        }

// Now go to the btn_resize _click event and copy the following code.

private void btn_resize_Click(object sender, EventArgs e)
        {
            int width = Convert.ToInt32(txt_width.Text), height = Convert.ToInt32(txt_height.Text);
            img = Resize(img, width, height);
            pictureBox2.Image = img;
            txt_width.Text = "";
            txt_Height.Text = "";
        }

Step-4-btn_reload
// Now create a Reload function as following.
        void reload()
        {
            img = Image.FromFile(ofd.FileName);
            pictureBox1.Image = img;
        }

// To Reload Image to original format go to the btn_reload _click event and copy the following code.

 private void btn_reload_Click(object sender, EventArgs e)
        {
            txt_width.Text = "";
            txt_Height.Text = "";
            trackBar1.Value = 0;
            trk_Contrast.Value = 0;
            trackBar3.Value = 0;
            reload();
        }

Step-5-btn_rotate
Button -Rotate
// To Rotate Image go to the btn_rotate _click event and copy the following code.

 private void btn_rotate_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = pictureBox1.Image;
            pictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipX);
            pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipX);
            pictureBox1.Refresh();
        }

Step-6-btn_normal
Button-Normal
// To get Normal image size on the picturebox1 to go to the btn_normal_Click event and copy the following code.

 private void btn_normal_Click_(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
        }



Step-7-btn_stretch
Button-Stretch
// To get Stretch image size on the picturebox1 , now go to the btn_stretch_Click event and copy the following code.

 private void btn_stretch_Click_(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.Stretch;
        }


Step-8-btn_autosize
Button-Auto Size
// To get Auto image size on the picturebox1, now go to the btn_autosize_Click event and copy the following code.

 private void btn_autosize_Click_(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
        }

Step-9-btn_center
Button-Center
// To get Center image size on the picturebox1 , now go to the btn_center_Click event and copy the following code.

 private void btn_autosize_Click_(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        }

Step-10-btn_zoom
Button-Zoom
// To get Zoom image size on the picturebox1, now go to the btn_zoom_Click event and copy the following code.

 private void btn_autosize_Click_(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }

Step-11  -To crop an image by selecting an area on the picturebox1.
public partial class Form1: Form
    {
        Point a;
        Point b;
        Boolean mouseDown = false;
      
        public Form1()
        {
            InitializeComponent();
        }

// Now go to the pictureBox1_MouseDown Event and copy the following code.

 private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
            a = e.Location;
    }
// Now go to the  pictureBox1_MouseMove Event and copy the following code.
 private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if(mouseDown==true)
            {
                b = e.Location;
                Refresh();
            }
        }

// Now go to the  pictureBox1_MouseUp Event and copy the following code.
 private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if(mouseDown==true)
            {
                b= e.Location;
                mouseDown = false;
            }
        }
// Now create a function to draw Rectangle on the picturebox.

  public partial class Form1: Form
    {
        Rectangle Rect = new Rectangle();
        public Form1()
        {
            InitializeComponent();
        }

 private Rectangle GetRect()
        {
            Rect = new Rectangle();
            Rect.X = Math.Min(xy.X, x1y1.X);
            Rect.Y = Math.Min(xy.Y, x1y1.Y);
            Rect.Width = Math.Abs(xy.X - x1y1.X);
            Rect.Height = Math.Abs(xy.Y - x1y1.Y);
            return Rect;
        }

// Now go to the pictureBox1_Paint Event and copy the following code.
 private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if(Rect!=null)
            {
                e.Graphics.DrawRectangle(Pens.Aqua, GetRect());
            }
        }

// To display crop image on the picturebox2 go to the  pictureBox1_MouseUp Event and copy the following code.
 private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
           
            if(Rect!=null)
                {
                    Bitmap bitm = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
                    Bitmap crop = new Bitmap(Rect.Width, Rect.Height);
                    Graphics g=  Graphics.FromImage(crop);
                    g.DrawImage(bitm, 0, 0, Rect, GraphicsUnit.Pixel);
                    pictureBox2.Image = crop;
                   
                }
        }

Step-12- Create a function for image Filter-1
// Button name-btn_f1
// Button Text- Filter-1





 void f1()        
        {
            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
             new float[] {0,0,1,0,0},
             new float[] {0,1,0,0,0},
             new float[] {0,0,0,0,0},
             new float[] {0,0,0,1,0},
             new float[]{0,0,0,0,1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }

// Now go to the btn_f1_Click Event and copy the following code.
 private void btn_f1_Click_(object sender, EventArgs e)
        {
            reload();
            f1();
        }

Step-13-Create a function for image Filter-2
// Button name-btn_f2
// Button Text- Filter-2
void f2()    
        {
           img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
              new float[]{.393f, .349f, .272f, 0, 0},
              new float[]{.769f, .686f, .534f, 0, 0},
              new float[]{.189f, .168f, .131f, 0, 0},
              new float[]{0, 0, 0, 1, 0},
              new float[]{0, 0, 0, 0, 1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }
// Now go to the btn_f2_Click Event and copy the following code.

 private void btn_f2_Click_(object sender, EventArgs e)
        {
            reload();
            f2();
        }

Step-14-Create a function for image Filter-3
// Button name-btn_f3
// Button Text- Filter-3

 void f3()      
        {
            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
              new float[]{.3f, .3f, .3f, 0, 0},    //Grayscale Filter//
              new float[]{.59f, .59f, .59f, 0, 0},
              new float[]{.11f, .11f, .11f, 0, 0},
              new float[]{0, 0, 0, 1, 0},
              new float[]{0, 0, 0, 0, 1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
           pictureBox1.Image = bmpinverted;
        }

// To call the function go to the btn_f3_Click Event and copy the following code.

 private void btn_f3_Click_(object sender, EventArgs e)
        {
            reload();
            f3();
        }

Step-15-Create a function for image Filter-4
// Button Name-btn_f4
// Button Text- Filter-4


  void f4()
        {
            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
             new float[] {1,1,0,0,0,0},
             new float[] {0,0,1,0,0},
             new float[] {0,0,0,0,0},
             new float[] {0,0,0,1,0},
             new float[]{0,0,0,0,1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }

// To call the function go to the btn_f4_Click Event and copy the following code.

 private void btn_f4_Click_(object sender, EventArgs e)
        {
            reload();
            f4();
        }

Step-16-Create a function for image Filter-5
// Button Name-btn_f5
// Button Text- Filter-5
 void f5()
        {
            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
             new float[] {1,1,1,0,0},
             new float[] {0,0,1,1,0},
             new float[] {0,0,0,0,0},
             new float[] {0,0,0,1,0},
             new float[]{0,0,0,0,1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }

// To call the function go to the btn_f5_Click Event and copy the following code.  

 private void btn_f5_Click_(object sender, EventArgs e)
        {
            reload();
            f5();
        }

Step-17 - To get Hue Effect on the image create a function as follows.















void hue()
        {
            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
             new float[] {1,0,(trackBar1.Value),0,0,0,0},
             new float[] {0,1,0,0,0},
             new float[] {0,0,0,0,0},
             new float[] {0,0,0,1,0},
             new float[]{0,0,0,0,1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
            pictureBox2.Image = pictureBox1.Image;
            
        }

// Now go to the trk_hue_ValueChanged Event and copy the following code.
// Note - Propertie of trackbar1-Name =  trk_hue

 private void trk_hue_ValueChanged(object sender, EventArgs e)
        {
            reload();
            hue();
        }

Step-18 - To Increase or Decrease the Image Contrast first create a function as follows.


void contrast()
        {
            float cont = 0;
            contrast = 0.2f * trk_Contrast.Value;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
              new float[]{contrast ,0f,0f,0f,0f },
              new float[]{0f,contrast,0f,0f,0f },
              new float[]{0f,0f,contrast,0f,0f },
              new float[]{0f,0f,0f,1f,0f },
              new float[]{0.001f,0.001f,0.001f,0f,1f} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }
// Now go to the trk_contrast_ValueChanged Event and copy the following code.
// Note - Propertie of trackbar2-Name =  trk_contrast

 private void trk_contrast_ValueChanged_(object sender, EventArgs e)
        {
            contrast();
        }

Step-19 - To Increase or Decrease Image Brightness  create a function as following
 void bright()
        {
            img = pictureBox1.Image;
            float fvalue = trackBar3.Value / 50f;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
            new float[] {1,0,0,0,0},
             new float[] {0,1,0,0,0},
             new float[] {0,0,1,0,0 },
             new float[] {0,0,0,0,0},
             new float[]{fvalue,fvalue,fvalue,1,1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }

// Now go to the trk_bright_ValueChanged Event and copy the following code.
// Note - Propertie of trackbar3-Name =  trk_bright

 private void trk_bright_ValueChanged_1(object sender, EventArgs e)
        {
           bright();
        }

// Now go to the trk_bright_Scroll Event and copy the following code.

 private void trk_bright_Scroll(object sender, EventArgs e)
        {
            reload();
        }
         Full Source code
------------------------------------------------------------------------------------------------------------------------------
using System;
using System. Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace Image_tool_by_Rohit_Programming_zone
{
    public partial class Form1: Form
    {
        Image img;
        OpenFileDialog ofd = new OpenFileDialog();
        Rectangle Rect = new Rectangle();
        Point a;
        Point b;
        float cont = 0;
        Boolean mouseDown = false;
      
        public Form1()
        {
            InitializeComponent();
        }
        void reload()
        {
            img = Image.FromFile(ofd.FileName);
            pictureBox1.Image = img;
        }
        void f1()
        {
            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
             new float[] {0,0,1,0,0},
             new float[] {0,1,0,0,0},
             new float[] {0,0,0,0,0},
             new float[] {0,0,0,1,0},
             new float[]{0,0,0,0,1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }
        void f2()
        {
           img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
              new float[]{.393f, .349f, .272f, 0, 0},
              new float[]{.769f, .686f, .534f, 0, 0},
              new float[]{.189f, .168f, .131f, 0, 0},
              new float[]{0, 0, 0, 1, 0},
              new float[]{0, 0, 0, 0, 1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }
   
 void f3()
        {
            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
              new float[]{.3f, .3f, .3f, 0, 0},    //Grayscale Filter//
              new float[]{.59f, .59f, .59f, 0, 0},
              new float[]{.11f, .11f, .11f, 0, 0},
              new float[]{0, 0, 0, 1, 0},
              new float[]{0, 0, 0, 0, 1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }
        void f4()
        {
            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
             new float[] {1,1,0,0,0,0},
             new float[] {0,0,1,0,0},
             new float[] {0,0,0,0,0},
             new float[] {0,0,0,1,0},
             new float[]{0,0,0,0,1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }
        void f5()
        {
            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
             new float[] {1,1,1,0,0},
             new float[] {0,0,1,1,0},
             new float[] {0,0,0,0,0},
             new float[] {0,0,0,1,0},
             new float[]{0,0,0,0,1} });
           
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }
       
        void bright()
        {
            img = pictureBox1.Image;
            float fvalue = trackBar3.Value / 50f;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
            new float[] {1,0,0,0,0},
             new float[] {0,1,0,0,0},
             new float[] {0,0,1,0,0 },
             new float[] {0,0,0,0,0},
             new float[]{fvalue,fvalue,fvalue,1,1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }
        
        void contrast()
        {
            cont = 0.2f * trackBar2.Value;
         
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
              new float[]{contrast ,0f,0f,0f,0f },
              new float[]{0f,contrast,0f,0f,0f },
              new float[]{0f,0f,contrast,0f,0f },
              new float[]{0f,0f,0f,1f,0f },
              new float[]{0.001f,0.001f,0.001f,0f,1f} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width,img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
        }
        void hue()
        {

            img = pictureBox1.Image;
            Bitmap bmpinverted = new Bitmap(img.Width, img.Height);
            ImageAttributes ia = new ImageAttributes();
            ColorMatrix cmpicture = new ColorMatrix(new float[][] {
             new float[] {1,0,(trackBar1.Value),0,0,0,0},
             new float[] {0,1,0,0,0},
             new float[] {0,0,0,0,0},
             new float[] {0,0,0,1,0},
             new float[]{0,0,0,0,1} });
            ia.SetColorMatrix(cmpicture);
            Graphics grps = Graphics.FromImage(bmpinverted);
            grps.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, ia);
            grps.Dispose();
            pictureBox1.Image = bmpinverted;
            pictureBox2.Image = pictureBox1.Image;
            
        }
     
       

 private void btn_Open_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            ofd.Filter = "images|*.png;*.jpg;*.jpeg;*.gif";

            if(ofd.ShowDialog()==DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(ofd.FileName);
                pictureBox2.Image = new Bitmap(ofd.FileName);
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                textBox1.Text = ofd.FileName;
                img = Image.FromFile(ofd.FileName);
                label1.Text = pictureBox1.Image.Size.ToString();
            }
        }
        private void btn_Set_Click(object sender, EventArgs e)
        {
            int width = Convert.ToInt32(txt_width.Text), height = Convert.ToInt32(txt_Height.Text);
            img = Resize(img, width, height);
            pictureBox2.Image = img;
            txt_width.Text = "";
            txt_Height.Text = "";
        
        }
        new Image Resize(Image image,int width,int height)
        {
            Bitmap bmp = new Bitmap(width, height);
            Graphics graphic = Graphics.FromImage(bmp);
            graphic.DrawImage(image, 0, 0, width, height);
            graphic.Dispose();
            return bmp;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "JPEG|*.JPG|PNG|*.PNG|GIF|*.GIF|BMP|*.BMP|All files (*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (Bitmap bitmap = new Bitmap(pictureBox2.Image))
                    {
                        bitmap.Save(sfd.FileName, ImageFormat.Jpeg);
                    }
                    MessageBox.Show("Picture Saved Successfully.");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An Error has occured);
                }
            }
        }
      private void btn_Rotate_Click(object sender, EventArgs e)
        {
            pictureBox2.Image = pictureBox1.Image;
            pictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipX);
            pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipX);
            pictureBox1.Refresh();
        }

        private void btn_Reload_Click(object sender, EventArgs e)
        {
            txt_Width.Text = "";
            txt_Height.Text = "";
            trk_Hue.Value = 0;
            trk_Contrast.Value = 0;
            trk_Bright.Value = 0;
            reload();
        }

        private void btn_f1_Click_1(object sender, EventArgs e)
        {
            reload();
            f1();
        }

        private void btn_f2_Click(object sender, EventArgs e)
        {
            reload();
            f2();
        }
       private void btn_f3_Click(object sender, EventArgs e)
        {
            reload();
            f3();
           
        }
       private void btn_f4_Click(object sender, EventArgs e)
        {
            reload();
            f4();
        }

        private void btn_f5_Click(object sender, EventArgs e)
        {
            reload();
            f5();
        }

        private void trk_Hue_ValueChanged(object sender, EventArgs e)
        {
            reload();
            hue();
        }
       private void trk_Contrast_ValueChanged_2(object sender, EventArgs e)
        {

            contrast();

        }
         private void trk_Contrast_Scroll(object sender, EventArgs e)
        {
            reload();
        }
       private void trk_Bright_ValueChanged_1(object sender, EventArgs e)
        {

           bright();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
            a= e.Location;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if(mouseDown==true)
            {
                b = e.Location;
                Refresh();
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if(mouseDown==true)
            {
                b = e.Location;
                mouseDown = false;

           if(Rect!=null)
                {
                    Bitmap bitm = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
                    Bitmap crop = new Bitmap(Rect.Width, Rect.Height);
                    Graphics g=  Graphics.FromImage(crop);
                    g.DrawImage(bitm, 0, 0, Rect, GraphicsUnit.Pixel);
                    pictureBox2.Image = crop;
                   
                }
            }
        }

           private Rectangle GetRect()
        {
            Rect = new Rectangle();
            Rect.X = Math.Min(a.X, b.X);
            Rect.Y = Math.Min(a.Y, b.Y);
            Rect.Width = Math.Abs(a.X - b.X);
            Rect.Height = Math.Abs(a.Y - b.Y);
            return Rect;
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if(Rect!=null)
            {
                e.Graphics.DrawRectangle(Pens.Aqua, GetRect());
            }
        }
     
        private void btn_Normal_Click_1(object sender, EventArgs e)
        {
           
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
        }

        private void btn_Stretch_Click_1(object sender, EventArgs e)
        {
           
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void btn_AutoSize_Click(object sender, EventArgs e)
        {
           
            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
        }

        private void btn_Center_Click(object sender, EventArgs e)
        {
         pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        }

        private void btn_Zoom_Click(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
        }
     }
}