我可以将图片从Firefox拖放到Windows资源管理器中,然后保存图片。我能不能在我自己的应用程序中做同样的事情,也就是把一个图片从Firefox拖到我的应用程序的某个部分,然后把图片放到应用程序中?
我的应用程序是用.NET 4和WPF构建的。
编辑: John Koerner让我走了一部分路,但不完全是我想要的方式……
如果我将文件从Firefox拖动到Windows资源管理器,则该文件的保存方式与从中拖动该文件的网站上的文件完全相同。也就是说,它具有相同的文件名、文件格式和文件大小。它看起来就像是直接从网站上保存的,就像我右击它并选择了“另存为”一样。我得到的唯一信息是临时文件夹中的位图的路径。不错,但不完全是我想要的。我想我可以把那个位图压缩成JPEG或其他格式,但我真的更喜欢得到原图。由于此行为是我在将图像拖动到Windows资源管理器时得到的,因此我认为也许我可以在自己的应用程序中获得此行为。
发布于 2012-06-24 19:47:29
你必须允许在你的窗口上拖放,然后处理拖放事件。然后,您可以读取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拖拽到我的应用程序时得到的参数列表。您可能会对文件名或FilenameW感兴趣。将这些字符串与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
DragContexthttps://stackoverflow.com/questions/11176975
复制相似问题