从浏览器到WPF应用程序拖放图片 [英] Drag and Drop image from browser to WPF Application

查看:492
本文介绍了从浏览器到WPF应用程序拖放图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的WPF应用程序中实现功能将图像从一个浏览器,进入我的WPF应用程序的窗口。



中的代码正常工作与Firefox和Windows资源管理器,但与Chrome和IE中出现的问题(还没有尝试过任何其他浏览器,只是还没有)。



下面的代码片段:

 私人无效DRAG_ENTER(对象发件人,DragEventArgs E)
{
的foreach(在e.Data.GetFormats字符串格式())
Console.WriteLine(格式);
Console.WriteLine(效果:+ e.AllowedEffects);
}

私人无效DRAG_DROP(对象发件人,DragEventArgs E)
{
的String []文件=(字符串[])e.Data.GetData(DataFormats。 FileDrop);
ImageSourceConverter器=新ImageSourceConverter();
的foreach(在文件中字符串的文件)
{
如果(converter.IsValid(文件))
{
//执行与图像
的东西}
}
}

综观输出,似乎火狐实际保存图像复制到剪贴板,而Chrome的只是抓住了图像的HTML,而IE不会做任何事情。



任何人有一些有识之士就如何我?可能会得到跨浏览器的功能。






更新
一对夫妇的解决方法我发现是解析HTML(浏览器/火狐)的图像源,然后使用类似WebClient的对象的源下载。
宁愿一个方法,虽然,有文件类型更强的检查。



IE9和Firefox都有一个DeviceIndependentBitmap文件格式拖动非时可用-hyperlink形象。这似乎是一个更安全的替代品,虽然镀铬似乎不支持它。它还与超链接的图像不是那么有用。






通过火狐,输出(DRAG_ENTER因为某些原因被解雇了两次):

 文本/ X-MOZ-URL 
FileGroupDescriptor
FileGroupDescriptorW
FileContents
UniformResourceLocator
UniformResourceLocatorW
文本/ X-MOZ-URL数据
文本/ X-MOZ-URL-说明
文/ URI列表
文/ _moz_htmlcontext
文/ _moz_htmlinfo
text / html的
HTML格式
文本
UnicodeText
System.String
应用程序/ x-MOZ-nativeimage
DeviceIndependentBitmap
FileDrop
FileNameW
的FileName
的首选DROPEFFECT
应用程序/ x-MOZ文件,承诺的URL
应用程序/ x-MOZ文件-promise-DEST文件名
DragImageBits
DragContext
功效:链接,所有的

铬(DRAG_ENTER也被解雇了两次):

  DragContext 
DragImageBits
FileGroupDescriptorW
FileContents
HTML格式
text / html的
文本/ X-MOZ-URL
UniformResourceLocatorW
UniformResourceLocator
文本
UnicodeText
System.String
功效:复制,移动,链接

的Internet Explorer(再次,DRAG_ENTER发射两次):

  UntrustedDragDrop 
msSourceUrl
FileGroupDescriptor
FileGroupDescriptorW
FileContents
UniformResourceLocator
作用:链接


解决方案

您可以使用FileGroupDescriptorW和FileContent格式,让您的数据




  • FileGroupDescriptorW是描述你的数据(如FileDescriptors'的数组:名称,大小,修改时间,...)

  • FileContent包含您的文件内容。



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

  VAR FILESTREAM =(MemoryStream的[])dataObject.GetData(FileContents); 

如果你想在如何使用FileGroupDescriptor(W)进行更部门教程,我可以推荐这教程上codeproject.com。它谈论拖放放大器;下降,由微软的Outlook,但它使用相同的IDataObject的格式


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.

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

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.

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.

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.


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

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):

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):

UntrustedDragDrop
msSourceUrl
FileGroupDescriptor
FileGroupDescriptorW
FileContents
UniformResourceLocator
Effects: Link

解决方案

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

  • FileGroupDescriptorW is an array of FileDescriptors' that describe your data (e.g.: name, size, modified-time, ...)
  • FileContent contains your file contents.

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.

这篇关于从浏览器到WPF应用程序拖放图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆