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_5
{
    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)
        {
            this.openFileDialog1.ShowDialog();
            if (this.openFileDialog1.FileName.Trim() == "")
                return;
            try
            {   //得到原始大小的图像
                Bitmap SrcBitmap = new Bitmap(this.openFileDialog1.FileName);
                //得到缩放后的图像
                MyBitmap = new Bitmap(SrcBitmap, this.pictureBox1.Width,
                    this.pictureBox1.Height);
                this.pictureBox1.Image = MyBitmap;
            }
            catch (Exception Err)
            {
                MessageBox.Show(this, "打开图像文件错误！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MyBitmap = (Bitmap)this.pictureBox1.Image.Clone();               //为共用变量MyBitmap赋值
            int dh = MyBitmap.Height / 20;                                   //定义变量并赋值
            int dw = MyBitmap.Width;
            Graphics g = this.pictureBox1.CreateGraphics();                 //定义Graphics对象实例
            g.Clear(Color.Gray);
            Point[] MyPoint = new Point[20];                                //定义点数组
            for (int y = 0; y < 20; y++)                                   //利用For循环为点数组赋值
            {
                MyPoint[y].X = 0;
                MyPoint[y].Y = y * dh;
            }
            Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
            for (int i = 0; i < dh; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    for (int k = 0; k < dw; k++)
                    {
                        bitmap.SetPixel(MyPoint[j].X + k, MyPoint[j].Y + i, MyBitmap.GetPixel(MyPoint[j].X + k, MyPoint[j].Y + i));
                    }
                }
                this.pictureBox1.Refresh();                                  //刷新图像
                this.pictureBox1.Image = bitmap;
                System.Threading.Thread.Sleep(100);
            }	

        }

        private void button3_Click(object sender, EventArgs e)
        {
            MyBitmap = (Bitmap)this.pictureBox1.Image.Clone();
            int dw = MyBitmap.Width / 30;
            int dh = MyBitmap.Height;
            Graphics g = this.pictureBox1.CreateGraphics();
            g.Clear(Color.Gray);
            Point[] MyPoint = new Point[30];
            for (int x = 0; x < 30; x++)
            {
                MyPoint[x].Y = 0;
                MyPoint[x].X = x * dw;
            }
            Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
            for (int i = 0; i < dw; i++)
            {
                for (int j = 0; j < 30; j++)
                {
                    for (int k = 0; k < dh; k++)
                    {
                        bitmap.SetPixel(MyPoint[j].X + i, MyPoint[j].Y + k, MyBitmap.GetPixel(MyPoint[j].X + i, MyPoint[j].Y + k));
                    }
                }
                this.pictureBox1.Refresh();
                this.pictureBox1.Image = bitmap;
                System.Threading.Thread.Sleep(100);
            }	
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();                  //关闭程序
        }
    }
}
