【发布时间】:2015-02-23 21:23:15
【问题描述】:
我有一个简单的应用程序,我通过按钮图像加载到 flowlayoutpanel 作为 PictureBox 表单。然后我需要将这些图像拖放到 PictureBoxes 中。拖放工作正常,但我不知道如何在 PictureBox 中获取拖放图像的路径?
这是我的部分代码:
private void SelectFilesButtonClick(object sender, EventArgs e)
{
InitializeOpenFileDialog();
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
// Read the files
foreach (String file in openFileDialog1.FileNames)
{
soubory.AppendText(file+ System.Environment.NewLine);
// Create a PictureBox.
try
{
PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(file);
pb.Height =100;
pb.Width = 100;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Image = loadedImage;
pb.MouseDown += new MouseEventHandler(pb_MouseDown);
flowLayoutPanel1.Controls.Add(pb);
}
catch (Exception ex)
{
// Could not load the image - probably related to Windows file system permissions.
MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
+ ". You may not have permission to read the file, or " +
"it may be corrupt.\n\nReported error: " + ex.Message);
}
}
}
}
void pb_MouseDown(object sender, MouseEventArgs e)
{
PictureBox img = sender as PictureBox;
img.DoDragDrop(img.Image, DragDropEffects.Move);
}
void PictureBox1DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
void PictureBox1DragDrop(object sender, DragEventArgs e)
{
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
PictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
}
【问题讨论】:
-
图片框完全不知道图片的来源,因此不包含其原始路径。一种快速的方法是将其填充到 .Tag 道具中;
pb.Tag = file;