And*_*erd 13 .net wpf drag-and-drop bitmap winforms
我希望我的WPF应用程序成为放置目标,我希望能够从任何网页拖动图像.
当从网页拖动图像时,显然它是"DragImageBits"格式,可以反序列化以键入ShDragImage.(请参阅问题的底部,了解我如何定义它)
如何将其转换为WPF图像?
这是我目前的尝试.(如果有人知道做出绝望的正确方法,我全都耳朵)
private void UserControl_Drop(object sender, System.Windows.DragEventArgs e)
{
string[] formats = data.GetFormats();
// DragImageBits
if (formats.Contains("DragImageBits"))
{
MemoryStream imageStream = data.GetData("DragImageBits") as MemoryStream;
// Now I'm deserializing this, the only way I know how
imageStream.Seek(0, SeekOrigin.Begin);
BinaryReader br = new BinaryReader(imageStream);
ShDragImage shDragImage;
shDragImage.sizeDragImage.cx = br.ReadInt32();
shDragImage.sizeDragImage.cy = br.ReadInt32();
shDragImage.ptOffset.x = br.ReadInt32();
shDragImage.ptOffset.y = br.ReadInt32();
shDragImage.hbmpDragImage = new IntPtr(br.ReadInt32());
shDragImage.crColorKey = br.ReadInt32();
var systemDrawingBitmap = System.Drawing.Bitmap.FromHbitmap(shDragImage.hbmpDragImage);
Run Code Online (Sandbox Code Playgroud)
在这一点上,我得到一个类型的例外,System.Runtime.InteropServices.ExternalException
消息就是这样Generic GDI+ error
.
有谁知道我应该做什么?
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Win32Point
{
public int x;
public int y;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Win32Size
{
public int cx;
public int cy;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct ShDragImage
{
public Win32Size sizeDragImage;
public Win32Point ptOffset;
public IntPtr hbmpDragImage;
public int crColorKey;
}
Run Code Online (Sandbox Code Playgroud)
And*_*erd 19
这是我学到的东西:
"DragImageBits"由windows shell提供,仅用于拖动光标,而不是用于最终数据.shell通过调整大小和透明度将图像转换为适当的拖动光标.
例如,如果您拖动此图片:
SHDRAGIMAGE将呈现如下:
如果你真的想从SHDRAGIMAGE中提取图像,这里是代码.(部分解除了这个答案)
MemoryStream imageStream = data.GetData("DragImageBits") as MemoryStream;
imageStream.Seek(0, SeekOrigin.Begin);
BinaryReader br = new BinaryReader(imageStream);
ShDragImage shDragImage;
shDragImage.sizeDragImage.cx = br.ReadInt32();
shDragImage.sizeDragImage.cy = br.ReadInt32();
shDragImage.ptOffset.x = br.ReadInt32();
shDragImage.ptOffset.y = br.ReadInt32();
shDragImage.hbmpDragImage = new IntPtr(br.ReadInt32()); // I do not know what this is for!
shDragImage.crColorKey = br.ReadInt32();
int stride = shDragImage.sizeDragImage.cx * 4;
var imageData = new byte[stride * shDragImage.sizeDragImage.cy];
// We must read the image data as a loop, so it's in a flipped format
for (int i = (shDragImage.sizeDragImage.cy - 1) * stride; i >= 0; i -= stride)
{
br.Read(imageData, i, stride);
}
var bitmapSource = BitmapSource.Create(shDragImage.sizeDragImage.cx, shDragImage.sizeDragImage.cy,
96, 96,
PixelFormats.Bgra32,
null,
imageData,
stride);
Run Code Online (Sandbox Code Playgroud)
如果您想将DragImageBits用于其预期目的(作为拖动图像),请参阅此博客以获取一个简单的可下载示例.
从网页拖动图像变得复杂,因为Firefox,Chrome和IE9都为您提供了一组不同的格式.此外,您希望同时处理图像和图像超链接,并再次对它们进行不同的处理.
Google和Firefox提供"text/html"格式,可以将单个HTML元素作为图像.Google将其作为ASCII字符串提供给您,Firefox将其作为unicode字符串提供给您.所以这是我写的代码来处理它:
System.Windows.IDataObject data = e.Data;
string[] formats = data.GetFormats();
if (formats.Contains("text/html"))
{
var obj = data.GetData("text/html");
string html = string.Empty;
if (obj is string)
{
html = (string)obj;
}
else if (obj is MemoryStream)
{
MemoryStream ms = (MemoryStream)obj;
byte[] buffer = new byte[ms.Length];
ms.Read(buffer, 0, (int)ms.Length);
if (buffer[1] == (byte)0) // Detecting unicode
{
html = System.Text.Encoding.Unicode.GetString(buffer);
}
else
{
html = System.Text.Encoding.ASCII.GetString(buffer);
}
}
// Using a regex to parse HTML, but JUST FOR THIS EXAMPLE :-)
var match = new Regex(@"<img[^/]src=""([^""]*)""").Match(html);
if (match.Success)
{
Uri uri = new Uri(match.Groups[1].Value);
SetImageFromUri(uri);
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,正则表达式将处理直图像和图像超链接.
我的SetImageFromUri
功能:
private void SetImageFromUri(Uri uri)
{
string fileName = System.IO.Path.GetTempFileName();
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(uri, fileName);
}
using (FileStream fs = File.OpenRead(fileName))
{
byte[] imageData = new byte[fs.Length];
fs.Read(imageData, 0, (int)fs.Length);
this.ImageBinary = imageData;
}
File.Delete(fileName);
}
Run Code Online (Sandbox Code Playgroud)
对于IE9,您可以处理"FileDrop"格式.这在IE9中运行良好.Chrome不支持它.Firefox支持它,但将图像转换为位图并将透明像素转换为黑色.因此,如果"text.html"格式不可用,则只应处理"FileDrop"格式.
else if (formats.Contains("FileDrop"))
{
var filePaths = (string[])data.GetData("FileDrop");
using (var fileStream = File.OpenRead(filePaths[0]))
{
var buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);
this.ImageBinary = buffer;
}
}
Run Code Online (Sandbox Code Playgroud)
如果从IE9拖动图像超链接,则不提供"FileDrop"格式.我还没弄明白如何将图像从IE9中的图像超链接拖到我的图像控件上.
如果您正在使用此示例,但仍需要将此二进制数据转换为图像,则这是一个有用的代码段:
BitmapImage sourceImage = new BitmapImage();
using (MemoryStream ms = new MemoryStream(imageBinary))
{
sourceImage.BeginInit();
sourceImage.CacheOption = BitmapCacheOption.OnLoad;
sourceImage.StreamSource = ms;
sourceImage.EndInit();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2683 次 |
最近记录: |