【问题标题】:Draging a picturebox image outside the form将图片框图像拖到表单外
【发布时间】:2018-07-08 13:39:57
【问题描述】:

我一直在尝试以与 Windows 资源管理器相同的方式从表单边界之外的图片框中拖动图像,例如,将其拖放到外部网络浏览器中查看,Skype 发送等

切换到 VB.NET 以找到最基本的解决方案,但不幸的是没有运气。拖放效果显示在我的表单之外,但是,没有应用程序接受或正确响应图像放置。

在 Picturebox 的 MouseDown 事件上运行代码。

Me.DoDragDrop(PictureBox1.Image, DragDropEffects.All)

有什么想法吗? C# 或 VB.NET,两者都适合我。

谢谢

【问题讨论】:

    标签: c# .net vb.net controls


    【解决方案1】:

    我为 WPF 找到了这个解决方案:Drag and drop to Desktop / Explorer

    对于 winforms 的工作方式如下:

    private void DragDropImage()
    {
      var filename = "filename.jpg";
      var path = Path.Combine(Path.GetTempPath(), filename);
      this.pictureBox1.Image.Save(path);
      var paths = new[] { path };
      this.pictureBox1.DoDragDrop(new DataObject(DataFormats.FileDrop, paths), DragDropEffects.Copy);
    }
    
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Left)
        this.DragDropImage();
    }
    

    这会将图片框的内容复制到临时目录。然后做一个dodragdrop。它经过测试并为我工作。代码在 MouseMove 中执行并检查鼠标是否被点击。

    【讨论】:

    • 谢谢,我已经发布了一个答案评论,它将代码缩减为一行。非常感谢您的帮助。
    【解决方案2】:

    feal 的代码有效,但是,我认为不需要浪费时间/处理能力来保存图像,因此我将其修改为以下内容,以防将来有人遇到同样的问题。代码插入到控件的 MouseDown 事件中。

    VB.NET:

    Me.DoDragDrop(New DataObject(DataFormats.FileDrop, {"Image Path"}), DragDropEffects.All)
    

    C#:

    this.DoDragDrop(new DataObject(DataFormats.FileDrop, new[] {@"Image Path"}), DragDropEffects.All);
    

    非常感谢,feal。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多