当前位置: 首页 > 编程语言 > C#

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

时间:2023-04-11 02:02:55 C#

在我的WPF应用程序的窗口中从浏览器拖放图像到WPF应用程序。该代码在Firefox和WindowsExplorer中有效,但在Chrome和IE中存在问题(尚未尝试过任何其他浏览器)。这是一段代码:privatevoidDrag_Enter(objectsender,DragEventArgse){foreach(stringformatine.Data.GetFormats())Console.WriteLine(format);Console.WriteLine("效果:"+e.AllowedEffects);}privatevoidDrag_Drop(objectsender,DragEventArgse){string[]files=(string[])e.Data.GetData(DataFormats.FileDrop);ImageSourceConverter转换器=newImageSourceConverter();foreach(stringfileinfiles){if(converter.IsValid(file)){//Dosomethingwiththeimage}}}查看输出,似乎Firefox实际上将图像保存到剪贴板,而Chrome只是抓取图片的html,IE不会对它做任何事情。谁能阐明我如何获得跨浏览器功能?更新:我发现的一些解决方法是解析图像源(Chrome/Firefox)的html,然后使用类似WebClient对象的东西从源下载。但是,更喜欢一种更可靠地检查文件类型的方法。IE9和Firefox都有DeviceIndependentBitmap文件格式,在拖动非超链接图像时可用。这似乎是一个更安全的选择,尽管Chrome似乎不支持它。它对于超链接图像也不是那么有用。使用Firefox,输出是(由于某种原因Drag_Enter被触发两次):text/x-moz-urllisttext/_moz_htmlcontexttext/_moz_htmlinfotext/htmlHTML格式文本UnicodeTextSystem.Stringapplication/x-moz-nativeimageDeviceIndependentBitmapFileDropFileNameWFileNamePreferredDropEffectapplication/x-moz-file-promise-urlapplication/x-moz-file-promise-dest-filenameDragImageBitsDragContextEffects:Link,AllChrome(drag_enter也会触发两次):DragContextDragImageBitsFileGroupDescriptorWFileContentsHTMLFormattext/htmltext/x-moz-urlUniformResourceLocatorWUniformResourceLocatorTextUnicodeTextSystem.StringEffects:Copy,Move,链接InternetExplorer(同样,drag_enter触发两次):UntrustedDragDropmsSourceUrlFileGroupDescriptorFileGroupDescriptorWFileContentsUniformResourceLocatorEffects:LinkYoucanuseFileGroupDescriptorWandFileContent格式获取数据,如果你不关心文件名,只关心你可以使用的二进制内容varfilestream=(MemoryStream[])dataObject.GetData("FileContents");如果你想要一个关于如何使用FileGroupDescriptor(W)的例子,我可以在codeproject.com上推荐这个教程。它讨论从MSOutlook拖放,但它使用相同的IDataObject格式。以上就是C#学习教程的全部内容:从浏览器拖拽图片到WPF应用程序分享。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: