C# 公共控件之pictureBox

Posted 无声蝉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于C# 公共控件之pictureBox的主要内容,如果未能解决你的问题,请参考以下文章

c# winform如何取得一个区域的所有控件对象

C# WinForm 透明控件 PictureBox透明

C# WinForm应用程序,PictureBox控件透明效果的相关问题。

C# 公共控件之 dateTimePicker

C# 公共控件之maskedTextBox

C# 公共控件之ListView