C# 两PictureBox图片交换(ImageLacation) 选中边框 外部图片可直接拖入到PictureBox中显示

// 实现两个PictureBox图片内容交换,选中PictureBox带有红色边框,外部图片可直接拖入到PictureBox中显示。代码如下:

 

        PictureBox picBoxSource = null;
        string picBoxPath = string.Empty;
        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (null != picBoxSource)
            {
                picBoxPath = (sender as PictureBox).ImageLocation;
                (sender as PictureBox).ImageLocation = picBoxSource.ImageLocation;
                picBoxSource.ImageLocation = picBoxPath;
                picBoxSource = null;
            }
            else
            {
                picBoxSource = sender as PictureBox;
                PictureBoxBorder(picBoxSource);
            }
        }

        private void pictureBox_DragDrop(object sender, DragEventArgs e)
        {
            PictureBox picBox = (PictureBox)sender;
            string fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            picBox.ImageLocation = fileName;
        }
        private void pictureBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else
                e.Effect = DragDropEffects.None;
        }

        private void PictureBoxBorder(PictureBox pictureBox)
        {
            Graphics g = pictureBox.CreateGraphics();
            Pen pen = new Pen(Color.Red, 5);
            g.DrawRectangle(pen, pictureBox.ClientRectangle.X, pictureBox.ClientRectangle.Y,
                                       pictureBox.ClientRectangle.X + pictureBox.ClientRectangle.Width,
                                       pictureBox.ClientRectangle.Y + pictureBox.ClientRectangle.Height);
        }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值