从浏览器拖放图像到WPF应用程序。

时间:2022-11-13 12:21:51

I'm trying to implement functionality in my WPF application to drag an image out of a browser and into a window in my WPF app.

我正在尝试在WPF应用程序中实现功能,以从浏览器中拖出图像,并将其放到WPF应用程序的窗口中。

The code works fine with Firefox and Windows Explorer, but issues arise with Chrome and IE (haven't tried any other browsers just yet).

这个代码对Firefox和Windows资源管理器都很好用,但是Chrome和IE(还没有尝试其他浏览器)出现了问题。

Here's a code snippet:

这里有一个代码片段:

private void Drag_Enter(object sender, DragEventArgs e)
{
    foreach (string format in e.Data.GetFormats())
        Console.WriteLine(format);
    Console.WriteLine("Effects:" + e.AllowedEffects);
}

private void Drag_Drop(object sender, DragEventArgs e)
{
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
    ImageSourceConverter converter = new ImageSourceConverter();
    foreach (string file in files)
    {
        if (converter.IsValid(file))
        {
            // Do something with the image
        }
    }
 }

Looking at the output, it seems that Firefox actually saves the image to the clipboard, whereas Chrome is just grabbing the html of the image, while IE doesn't do anything with it.

看一下输出,Firefox实际上把图像保存到剪贴板,而Chrome只是抓取图像的html,而IE则不做任何事情。

Anyone have some insight as to how I may get cross-browser functionality?

对于如何获得跨浏览器的功能,谁有什么见解?


Update: A couple of workarounds I've found are to parse the html (Chrome/Firefox) for an image source, then download from the source using something like the WebClient object. Would prefer a method, though, that has stronger checking for file type.

更新:我找到的几个解决方法是解析一个图像源的html (Chrome/Firefox),然后使用WebClient对象之类的东西从源文件中下载。但是,更倾向于采用更强的文件类型检查方法。

IE9 and Firefox both have a DeviceIndependentBitmap file format that is available when dragging an non-hyperlink image. This seems to be a safer alternative, though Chrome does not seem to support it. It is also not so useful with hyperlink images.

IE9和Firefox在拖动非超链接图像时都有一个device独立位图文件格式。这似乎是一个更安全的选择,尽管Chrome似乎不支持它。它在超链接图像上也不是很有用。


With Firefox, the output is (Drag_Enter for some reason gets fired twice):

对于Firefox,输出是(出于某些原因,Drag_Enter会被触发两次):

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
Effects: Link, All

Chrome (drag_enter also gets fired twice):

Chrome (drag_enter也会被触发两次):

DragContext
DragImageBits
FileGroupDescriptorW
FileContents
HTML Format
text/html
text/x-moz-url
UniformResourceLocatorW
UniformResourceLocator
Text
UnicodeText
System.String
Effects: Copy, Move, Link

Internet Explorer (again, drag_enter fired twice):

Internet Explorer(再次,drag_enter触发了两次):

UntrustedDragDrop
msSourceUrl
FileGroupDescriptor
FileGroupDescriptorW
FileContents
UniformResourceLocator
Effects: Link

1 个解决方案

#1


1  

You can use the FileGroupDescriptorW and FileContent formats to get your data.

您可以使用FileGroupDescriptorW和FileContent格式获取数据。

  • FileGroupDescriptorW is an array of FileDescriptors' that describe your data (e.g.: name, size, modified-time, ...)
  • FileGroupDescriptorW是一组描述数据的文件描述符(例如:名称、大小、修改时间、…)
  • FileContent contains your file contents.
  • FileContent包含您的文件内容。

If you don't care about the filename and just need the binary content you could use

如果您不关心文件名,只需要您可以使用的二进制内容。

var filestream = (MemoryStream[])dataObject.GetData("FileContents");

If you want a more in-dept-tutorial on how to use the FileGroupDescriptor(W) I can recommend this tutorial on codeproject.com. It talks about drag&drop from MS Outlook, but it uses the same IDataObject formats.

如果您想要更多的关于如何使用FileGroupDescriptor(W)的深入教程,我可以推荐本教程在codeproject.com上。它讨论了来自MS Outlook的拖放,但是它使用了相同的IDataObject格式。

#1


1  

You can use the FileGroupDescriptorW and FileContent formats to get your data.

您可以使用FileGroupDescriptorW和FileContent格式获取数据。

  • FileGroupDescriptorW is an array of FileDescriptors' that describe your data (e.g.: name, size, modified-time, ...)
  • FileGroupDescriptorW是一组描述数据的文件描述符(例如:名称、大小、修改时间、…)
  • FileContent contains your file contents.
  • FileContent包含您的文件内容。

If you don't care about the filename and just need the binary content you could use

如果您不关心文件名,只需要您可以使用的二进制内容。

var filestream = (MemoryStream[])dataObject.GetData("FileContents");

If you want a more in-dept-tutorial on how to use the FileGroupDescriptor(W) I can recommend this tutorial on codeproject.com. It talks about drag&drop from MS Outlook, but it uses the same IDataObject formats.

如果您想要更多的关于如何使用FileGroupDescriptor(W)的深入教程,我可以推荐本教程在codeproject.com上。它讨论了来自MS Outlook的拖放,但是它使用了相同的IDataObject格式。