接收从Web页面拖动到WPF窗口的图像

时间:2011-12-09 06:56:29

标签: .net wpf winforms drag-and-drop bitmap

我希望我的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);

此时我收到System.Runtime.InteropServices.ExternalException类型的例外情况,消息只是Generic GDI+ error

有谁知道我应该做什么?

<小时/> 以下是支持类定义。我从this blog entry复制了它们。

[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;
    }

1 个答案:

答案 0 :(得分:18)

这是我所学到的:

“DragImageBits”由windows shell提供,仅用于拖动光标,而不是用于最终数据。 shell通过调整大小和透明度将图像转换为适当的拖动光标。

例如,如果您拖动此图片:

Fully Opaque Image

SHDRAGIMAGE将呈现为:

enter image description here

如果你真的想从SHDRAGIMAGE中提取图像,这里是代码。 (部分取自this answer

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

如果您想将DragImageBits用于其预期目的(作为拖动图像),请参阅this blog以获取简单,可下载的示例。

<小时/> 因此,“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);
            }
        }

在这种情况下,正则表达式将处理直图像和图像超链接。

我的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);
    }

对于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;
        }
    }

如果从IE9拖动图像超链接,则不提供“FileDrop”格式。我还没想出如何将图像从IE9中的图像超链接拖到我的图像控件上。

<小时/> 额外信息

如果您正在使用此示例,但仍需要将此二进制数据转换为图像,则这是一个有用的代码段:

                BitmapImage sourceImage = new BitmapImage();
                using (MemoryStream ms = new MemoryStream(imageBinary))
                {
                    sourceImage.BeginInit();
                    sourceImage.CacheOption = BitmapCacheOption.OnLoad;
                    sourceImage.StreamSource = ms;
                    sourceImage.EndInit();
                }