C# 公共控件之pictureBox

本文介绍了一个基于C#的图片编辑程序,实现了图片加载、保存、翻转和缩放功能。通过按钮点击,用户可以打开、保存图片,进行90度翻转,并使用鼠标滚轮调整图片大小。此外,还实现了十字光标功能,增强了用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值