// 实现两个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);
}