WPF InteropBitmap为位图位图、WPF、InteropBitmap

2023-09-06 06:21:00 作者:不愛就滾,別浪費老娘青春

我有一个奇怪的问题。我想检索WebBrowser控件已加载的图像。下面code正常工作的WinForms应用程序:

  IHTMLControlRange imgRange =(IHTMLControlRange)((HTMLBody)__ ie.NativeDocument.BODY).createControlRange();

            的foreach(IHTMLImgElement IMG在__ie.NativeDocument.Images)
            {
                imgRange.add((IHTMLControlElement)IMG);
                imgRange.execCommand(复制,假,空);

                System.IO.MemoryStream流=新System.IO.MemoryStream();
                使用(BMP位=(位图)Clipboard.GetDataObject()的GetData(DataFormats.Bitmap))
                {
                        bmp.Save(流System.Drawing.Imaging.ImageFormat.Bmp);
                        VAR图像= System.Drawing.Image.FromStream(流);
                }
            }
 

但是,同样的code,如果我在WPF应用程序中使用提供了错误

 使用(BMP位=(位图)Clipboard.GetDataObject().......
 
CDR怎样将位图转为矢量图

的错误是如下:

  

无法投类型'System.Windows.Interop.InteropBitmap的对象类型System.Drawing.Bitmap'。

我要如何解决这个问题?

请任何人都可以提供任何指导。

感谢你在前进。

解决方案

您正在运行到的问题是,有两种不同的剪贴板类,一为的WinForms,和一个是 WPF 。在的WinForms其中一个返回位图是适用于的WinForms,即 System.Drawing.Bitmap ,这在code使用的是其复制到为System.Drawing.Image 。

这些类型的位图都没有使用WPF用这样的事实,你不能将什么WPF版本的剪贴板类是给你一个类型,是用的WinForms有用的预期,而不是​​真正的问题。

您的问题是,WPF,你需要一个类型位图,您可以使用WPF使用方法:的BitmapSource 。这是你可以用WPF控件使用像图片。所以,回到你的问题:

如果您使用的是WPF4您可以使用 Clipboard.GetImage 返回一个的BitmapSource ,正是你所需要的 如果您使用的是其他任何东西,你可以用托马斯·莱维斯克的技术描述的这里

在总结:

有关的WinForms:WinForms的剪贴板=>的WinForms位图 对于WPF:WPF剪贴板=> WPF位图

I am having a strange problem. I am trying to retrieve the images already loaded in webbrowser control. The following code works fine in a WinForms application:

IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)__ie.NativeDocument.BODY).createControlRange();

            foreach (IHTMLImgElement img in __ie.NativeDocument.Images)
            {
                imgRange.add((IHTMLControlElement)img);
                imgRange.execCommand("Copy", false, null);

                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
                {
                        bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                        var image = System.Drawing.Image.FromStream(stream);
                }
            }

But the same code if I use in WPF application gives error on

using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().......

The error is as follows:

"Unable to cast object of type 'System.Windows.Interop.InteropBitmap' to type 'System.Drawing.Bitmap'."

How do I solve this?

Please can anyone provide any guidance.

Thank you in advance.

解决方案

The issue you are running into is that there are two different Clipboard classes, one for WinForms, and one for WPF. The WinForms one returns bitmaps that are suitable for use with WinForms, i.e. System.Drawing.Bitmap, which in the code you are using copies it to a System.Drawing.Image.

Those types of bitmaps are not useful with WPF so the fact that you can't convert what the WPF version of the Clipboard class is giving you to a type that is useful with WinForms is expected and not really your problem.

Your problem is that for WPF you need a type of bitmap that you can use with WPF: a BitmapSource. That is something you can use with WPF controls like Image. So, back to your question:

if you are using WPF4 you can use Clipboard.GetImage which returns a BitmapSource, exactly what you need if you are using anything else, you can use Thomas Levesque's technique described here

In summary:

For WinForms: WinForms clipboard => WinForms bitmaps For WPF: WPF clipboard => WPF bitmaps
 
精彩推荐
图片推荐