Tôi đang cố gắng triển khai chức năng trong ứng dụng WPF để kéo hình ảnh ra khỏi trình duyệt và vào một cửa sổ trong ứng dụng WPF của mình.Kéo và thả hình ảnh từ trình duyệt đến Ứng dụng WPF
Mã hoạt động tốt với Firefox và Windows Explorer, nhưng vấn đề phát sinh với Chrome và IE (chưa thử bất kỳ trình duyệt nào khác).
Dưới đây là một đoạn mã:
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
}
}
}
Nhìn vào đầu ra, có vẻ như Firefox thực sự tiết kiệm hình ảnh vào clipboard, trong khi Chrome chỉ là grabbing html của hình ảnh, trong khi IE không làm bất cứ điều gì với nó.
Bất kỳ ai cũng có một số thông tin chi tiết về cách tôi có thể nhận được chức năng của trình duyệt chéo?
Cập nhật: Một vài cách giải quyết tôi đã tìm thấy là để phân tích html (Chrome/Firefox) cho một nguồn hình ảnh, sau đó tải từ nguồn sử dụng giống như đối tượng WebClient. Tuy nhiên, muốn có một phương pháp có kiểm tra mạnh hơn đối với loại tệp.
IE9 và Firefox đều có định dạng tệp DeviceIndependentBitmap có sẵn khi kéo hình ảnh không siêu liên kết. Đây dường như là một giải pháp thay thế an toàn hơn, mặc dù Chrome dường như không hỗ trợ nó. Nó cũng không hữu ích với hình ảnh siêu liên kết.
Với Firefox, sản lượng là (Drag_Enter vì một lý do bị sa thải hai lần):
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 cũng bị sa thải hai lần):
DragContext
DragImageBits
FileGroupDescriptorW
FileContents
HTML Format
text/html
text/x-moz-url
UniformResourceLocatorW
UniformResourceLocator
Text
UnicodeText
System.String
Effects: Copy, Move, Link
Internet Explorer (một lần nữa , drag_enter đã kích hoạt hai lần):
UntrustedDragDrop
msSourceUrl
FileGroupDescriptor
FileGroupDescriptorW
FileContents
UniformResourceLocator
Effects: Link