【问题标题】:How to get path of image drag and dropped into PictureBox from flowlayoutpanel如何从flowlayoutpanel获取图像拖放到PictureBox的路径
【发布时间】: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;

标签: c# .net winforms


【解决方案1】:

您可以使用Image.Tag 传递有关路径的信息(因为您正在拖动Image):

void pb_MouseDown(object sender, MouseEventArgs e)
{
    var img = (PictureBox)sender;
    img.Image.Tag = path; // this can be already set earlier or e.g. copied from pb.Tag (if pb.Tag is used to store path)
    img.DoDragDrop(img.Image, DragDropEffects.Move);
}

void PictureBox1DragDrop(object sender, DragEventArgs e)
{
    PictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
    var path = PictureBox1.Image.Tag.ToString();
}

您还可以定义自己的数据格式来传递附加信息。见this answer

【讨论】:

  • 谢谢!使用 Tag 是最好的处理方式。
猜你喜欢
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多