using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "*.jpg,*.jpeg,*.bmp|*.jpg;*.jpeg;*.bmp";    //打开图像的类型
            openFileDialog1.ShowDialog();                                        //打开对话框
            Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName);    //创建Image对象实例
            this.BackgroundImage = myImage;
            this.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics myGraphics = this.CreateGraphics();                         //创建Graphics对象实例
            Bitmap myBitmap = new Bitmap(this.BackgroundImage);                  //创建Bitmap对象实例
            int myWidth, myHeight, i, j, iAvg, iPixel;                          //定义变量
            Color myColor, myNewColor;
            RectangleF myRect;
            myWidth = myBitmap.Width;                                        //为变量赋值
            myHeight = myBitmap.Height;
            myRect = new RectangleF(0, 0, myWidth, myHeight);
            Bitmap bitmap = myBitmap.Clone(myRect, System.Drawing.Imaging.PixelFormat.DontCare);    //调用Clone方法复制图像
            i = 0;
            while (i < myWidth - 1)
            {
                j = 0;
                while (j < myHeight - 1)
                {
                    myColor = bitmap.GetPixel(i, j);                             //调用GetPixel方法获取像素点的颜色
                    iAvg = (myColor.R + myColor.G + myColor.B) / 3;
                    iPixel = 0;
                    if (iAvg >= 128)
                        iPixel = 255;
                    else
                        iPixel = 0;
                    myNewColor = Color.FromArgb(255, iPixel, iPixel, iPixel);     //调用FromArgb（）方法创建图像颜色
                    bitmap.SetPixel(i, j, myNewColor);                            //调用SetPixel（）方法设置像素点的颜色
                    j = j + 1;
                }
                i = i + 1;
            }
            myGraphics.Clear(Color.WhiteSmoke);
            myGraphics.DrawImage(bitmap, new Rectangle(0, 0, myWidth, myHeight));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Bitmap myBitmap = new Bitmap(this.BackgroundImage);              //创建Bitmap对象实例
            for (int i = 0; i < myBitmap.Width - 1; i++)
            {
                for (int j = 0; j < myBitmap.Height - 1; j++)
                {
                    Color Color1 = myBitmap.GetPixel(i, j);                    //调用GetPixel方法获取像素点的颜色
                    Color Color2 = myBitmap.GetPixel(i + 1, j + 1);
                    int red = Math.Abs(Color1.R - Color2.R + 128);             //调用绝对值Abs函数
                    int green = Math.Abs(Color1.G - Color2.G + 128);
                    int blue = Math.Abs(Color1.B - Color2.B + 128);
                    //颜色处理
                    if (red > 255) red = 255;
                    if (red < 0) red = 0;

                    if (green > 255) green = 255;
                    if (green < 0) green = 0;

                    if (blue > 255) blue = 255;
                    if (blue < 0) blue = 0;
                    myBitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));        //用SetPixel（）方法设置像素点的颜色
                }
            }
            this.BackgroundImage = myBitmap;
            this.BackgroundImageLayout = ImageLayout.Stretch;
        }
    }
}
