【问题标题】:Drag images from Firefox to my application将图像从 Firefox 拖到我的应用程序
【发布时间】:2012-06-24 11:15:04
【问题描述】:

我可以将图像从 Firefox 拖放到 Windows 资源管理器,然后保存图像。我可以在我自己的应用程序中以某种方式做同样的事情吗,即将图像从 Firefox 拖到我的应用程序的某个部分,然后将图像放入应用程序?

我的应用程序是使用 .NET 4 和 WPF 构建的。

编辑: John Koerner 让我参与其中,但不是我想要的……

如果我将一个文件从 Firefox 拖到 Windows 资源管理器中,该文件将完全按照我从中拖拽它的网站上的状态保存。也就是说,它具有相同的文件名、文件格式和文件大小。好像它是直接从网站上保存的,就好像我右键单击它并选择了“另存为”一样。我唯一得到的是临时文件夹中位图的路径。不错,但不是我想要的。我想我可以把那个位图压缩成JPEG或其他格式,但我真的更喜欢得到原件。由于这种行为是我在将图像拖动到 Windows 资源管理器时得到的,我想也许我可以在我自己的应用程序中得到它。

【问题讨论】:

    标签: .net wpf firefox drag-and-drop


    【解决方案1】:

    您必须允许在窗口上放置,然后处理放置事件。然后,您可以读取 FileDrop 以获取文件在磁盘上的位置并将其加载到图像或您需要的任何其他位置。

    public MainWindow()
    {
        InitializeComponent();
        this.AllowDrop = true;
        this.Drop += new DragEventHandler(MainWindow_Drop);
    }
    
    void MainWindow_Drop(object sender, DragEventArgs e)
    {
        BitmapImage bi = new BitmapImage(new Uri(((string[])e.Data.GetData("FileDrop"))[0]));
        image1.Source = bi;
    
        // Get the different parameters available and see which work for you.
        foreach (var param in e.Data.GetFormats())
            Console.WriteLine(param);
    }
    

    这是我从 firefox 拖到我的应用程序时得到的参数列表。您可能对文件名或文件名W 感兴趣。将这些字符串与 GetData 方法一起使用以获取所需的数据。

    text/x-moz-url
    FileGroupDescriptor
    FileGroupDescriptorW
    FileContents
    UniformResourceLocator
    UniformResourceLocatorW
    text/x-moz-url-data
    text/x-moz-url-desc
    text/uri-list
    text/_moz_htmlcontext
    text/_moz_htmlinfo
    text/html
    HTML Format
    Text
    UnicodeText
    System.String
    application/x-moz-nativeimage
    DeviceIndependentBitmap
    FileDrop
    FileNameW
    FileName
    Preferred DropEffect
    application/x-moz-file-promise-url
    application/x-moz-file-promise-dest-filename
    DragImageBits
    DragContext
    

    【讨论】:

    • 谢谢约翰!但是,我在问题中添加了更多信息以明确我想要的行为。
    • @haagel 我更新了答案,为您指明了正确的方向。
    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多