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

}

猜你喜欢

转载自blog.csdn.net/zhuxipan1990/article/details/83110877