1. 程式人生 > >C# 公共控制元件之pictureBox

C# 公共控制元件之pictureBox

1、新增控制元件

2、分別實現是三個button功能

        private void 開啟_Click(object sender, EventArgs e)
        {
            string pathname = string.Empty;
            OpenFileDialog file = new OpenFileDialog();
            file.InitialDirectory = ".";
            file.Filter = "所有檔案(*.*)|*.*";
            file.ShowDialog();
            if (file.FileName != string.Empty)
            {
                try
                {
                    pathname = file.FileName;   //獲得檔案的絕對路徑
                    this.pictureBox1.Load(pathname);
                    儲存.Enabled = true;
                    翻轉90.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }  
        }

        private void 儲存_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.ShowDialog();
            if (save.FileName != string.Empty)
            {
                pictureBox1.Image.Save(save.FileName);
            }  
        }
        private void 翻轉90_Click(object sender, EventArgs e)
        {
            pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
            pictureBox1.Refresh();//重新整理圖片框

            //順時針旋轉90度 RotateFlipType.Rotate90FlipNone 
            //逆時針旋轉90度 RotateFlipType.Rotate270FlipNone 
            //水平翻轉 RotateFlipType.Rotate180FlipY 
            //垂直翻轉 RotateFlipType.Rotate180FlipX
        }

3、實現滑動滑鼠,圖片放大縮小功能

public Form1()
{
    InitializeComponent();
    pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
}
void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
    pictureBox1.Width += e.Delta;
    pictureBox1.Height += e.Delta;
    this.Width += e.Delta;
    this.Height += e.Delta;
}

4、實現“十字游標”

Point lastPoint = new Point(-1, -1);
private void picturebox_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Location != lastPoint)//保持十字游標不消失
        pictureBox1.Refresh();
    lastPoint = e.Location;
    DrawReversibleLine(lastPoint);
}

void DrawReversibleLine(Point p)
{
    ControlPaint.DrawReversibleLine(
        pictureBox1.PointToScreen(new Point(0, p.Y)),
        pictureBox1.PointToScreen(new Point(pictureBox1.Width, p.Y)),
        SystemColors.Control);

    ControlPaint.DrawReversibleLine(
        pictureBox1.PointToScreen(new Point(p.X, 0)),
        pictureBox1.PointToScreen(new Point(p.X, pictureBox1.Height)),
        SystemColors.Control);//Color.ForestGreen

}