using System;
using System.Drawing;
using System.Windows.Forms;

namespace _12_7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static Bitmap Invert(Bitmap image)
        {
            Color color1;
            
            for (int i = 0; i < image.Width; i++)
                for (int j = 0; j < image.Height; j++)
                {
                    color1 = image.GetPixel(i, j);
                    image.SetPixel(i, j, Color.FromArgb(255 - color1.R,
                                   255 - color1.G, 255 - color1.B));
                }
            
            return image;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap myImage = new Bitmap(pictureBox1.Image);
            pictureBox2.Image = Invert(myImage);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Picture Files (*.jpg)|*.jpg|All Files (*.*)|*.*";
            openFileDialog1.FileName = string.Empty;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                pictureBox1.Load(openFileDialog1.FileName);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = string.Empty;
            saveFileDialog1.Filter = "bmp File (*.bmp)|*.bmp";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                pictureBox2.Image.Save(saveFileDialog1.FileName);
        }

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