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;
using System.Drawing.Imaging;

namespace Ex02_6
{
    public partial class Form1 : Form
    {

        Bitmap myBitmap;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        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);
            myBitmap = new Bitmap(myImage);
            this.BackgroundImage = myBitmap;
            this.BackgroundImageLayout = ImageLayout.Stretch;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Image myImage = System.Drawing.Image.FromFile(openFileDialog1.FileName);
            myBitmap = new Bitmap(myImage);
            Rectangle rect = new Rectangle(0, 0, myBitmap.Width, myBitmap.Height);
            //将指定图像锁定到内存中
            System.Drawing.Imaging.BitmapData bmpData = myBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, myBitmap.PixelFormat);
            // 获得图像中第一个像素数据的地址
            IntPtr ptr = bmpData.Scan0;
            int bytes = myBitmap.Width * myBitmap.Height * 3;
            byte[] rgbValues = new byte[bytes];
            // 使用RGB值为声明的rgbValues数组赋值
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            for (int counter = 0; counter < rgbValues.Length; counter += 3)
                rgbValues[counter] = 255;
            // 使用RGB值为图像的像素点着色
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
            // 从内存中解锁图像
            myBitmap.UnlockBits(bmpData);
            this.BackgroundImage = myBitmap;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int intWidth = this.BackgroundImage.Width;           //定义变量并赋值为窗体背景图像的宽度和高度
            int intHeight = this.BackgroundImage.Height;
            Graphics myGraphics = this.CreateGraphics();         //定义Graphics对象实例  
            myGraphics.Clear(Color.WhiteSmoke);
            Bitmap bitmap = new Bitmap(intWidth, intHeight);     //定义Bitmap对象实例  
            int i = 0;
            while (i <= intWidth / 2)
            {
                for (int m = 0; m <= intHeight - 1; m++)
                {
                    bitmap.SetPixel(i, m, myBitmap.GetPixel(i, m));          //调用SetPixel()方法
                }
                for (int n = 0; n <= intHeight - 1; n++)
                {
                    bitmap.SetPixel(intWidth - i - 1, n, myBitmap.GetPixel(intWidth - i - 1, n));
                }
                i++;
                this.Refresh();                                        //刷新窗体
                this.BackgroundImage = bitmap;
                System.Threading.Thread.Sleep(10);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int intWidth = this.BackgroundImage.Width;
            int intHeight = this.BackgroundImage.Height;
            Graphics myGraphics = this.CreateGraphics();
            myGraphics.Clear(Color.WhiteSmoke);
            Bitmap bitmap = new Bitmap(intWidth, intHeight);
            int i = 0;
            while (i <= intHeight / 2)
            {
                for (int m = 0; m <= intWidth - 1; m++)
                {
                    bitmap.SetPixel(m, i, myBitmap.GetPixel(m, i));
                }
                for (int n = 0; n <= intWidth - 1; n++)
                {
                    bitmap.SetPixel(n, intHeight - i - 1, myBitmap.GetPixel(n, intHeight - i - 1));
                }
                i++;
                this.Refresh();
                this.BackgroundImage = bitmap;
                System.Threading.Thread.Sleep(10);
            }
        }
    }
}
