使用Graphics.GetHdc时,参数是无效异常 [英] Parameter is Invalid Exception when using Graphics.GetHdc

查看:485
本文介绍了使用Graphics.GetHdc时,参数是无效异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在制作一个应用程序来拍摄网站快照。
一切正常,直到现在:
我的应用程序需要很多照片,并且图像处理的过程如下完成:

<$ p使用(Graphics graphics = Graphics.FromImage(mBitmap))
{
IntPtr hdc = graphics.GetHdc(); $ p>
SendMessage(new HandleRef(mWebBrowser,mWebBrowser.Handle),791,hdc,(IntPtr)30);
的BitBlt(新HandleRef(图形,HDC),0,0,mBitmap.Width,mBitmap.Height,新HandleRef(图形,HDC),0,0,13369376);
graphics.ReleaseHdc();



$ b $ p
$ b

(这是来自WebBrowser控件的DrawToBitmap代码的变化。与ILSpy)。



该错误发生在IntPtr hdc = graphics.GetHdc();



在使用GetHdc方法时,我询问有人询问这个Parameter is Invalid,并且有人说可能是因为没有释放之前的GDI对象。这不是这种情况,因为Graphics对象正在使用using语句,位图也是如此......



我需要注意的是,我有很多webbrowser同时(我从不摧毁它们,我重新使用它们,这是我遇到的另一个错误,而这是我解决它的唯一方法......)

当我在TaskManager上查看GDI对象的数量时,我发现它数量巨大。但是 - 当我碰巧拥有最多的GDI对象时,错误没有发生......
我猜这些对象来自WebBrowsers ...?



ILSpy的原始DrawToBitmap代码是:
$ b $ pre $ int $ n $ n $ n $ n $ n ;
int nHeight = Math.Min(this.Height,targetBounds.Height);
位图图片=新位图(nWidth,nHeight,bitmap.PixelFormat);
using(Graphics graphics = Graphics.FromImage(image))
{
IntPtr hdc = graphics.GetHdc();
UnsafeNativeMethods.SendMessage(new HandleRef(this,this.Handle),791,hdc,(IntPtr)30);
using(Graphics graphics2 = Graphics.FromImage(bitmap))
{
IntPtr hdc2 = graphics2.GetHdc();
SafeNativeMethods.BitBlt(新HandleRef(图案2,HDC2),targetBounds.X,targetBounds.Y,nWidth,nHeight参数,新HandleRef(图形,HDC),0,0,13369376);
graphics2.ReleaseHdcInternal(hdc2);
}
graphics.ReleaseHdcInternal(hdc);
}

文档声称Webbrowser不支持DrawToBitmap,但它很好用,它只是抛出这个异常。

解决方案

即使这个帖子很老,我想帮助解决这个问题,因为我我有这样的解决方案:

  private static Graphics graphics = null; 
private static IntPtr hdc = IntPtr.Zero;
private static IntPtr dstHdc = IntPtr.Zero;

private static Capture()
{
if(graphics == null)
{
hdc = GetDC(IntPtr.Zero);
graphics = Graphics.FromImage(bitmap);
dstHdc = graphics.GetHdc();
}
BitBlt(dstHdc,0,0,screen_resolution.Width,screen_resolution.Height,hdc,0,0,RasterOperation.SRC_COPY);
}

对于我来说,解决方案是,我只调用graphics.GetHdc()当对象为零时。之后,我从不使用调用graphics.GetHdc()。


I have been making an application to take snapshots of websites. Everything works well, until now: I have the application take MANY photos, and the process of the image taking is done as following:

using (Graphics graphics = Graphics.FromImage(mBitmap))
{
   IntPtr hdc = graphics.GetHdc();
   SendMessage(new HandleRef(mWebBrowser, mWebBrowser.Handle), 791, hdc, (IntPtr)30);
   BitBlt(new HandleRef(graphics, hdc), 0, 0, mBitmap.Width, mBitmap.Height, new HandleRef(graphics, hdc), 0, 0, 13369376);
   graphics.ReleaseHdc();
}

(this is a varying of the code of DrawToBitmap from the WebBrowser control. taken with ILSpy).

The error occurs on the IntPtr hdc = graphics.GetHdc(); line.

I sought for people asking about this "Parameter is Invalid" when using the GetHdc method, and people said that it might be because of not releasing prior GDI objects. this is not the case, since the Graphics object is withing a using statement, and so is the bitmap...

I need to note that I have many webbrowsers at the same time (I never destroy them, I re-use them, this is for another error which I had and this was the only way I could solve it...)

When I'm looking on the TaskManager on the amount of GDI Object, I see it is a huge amount. But - the error did not occur when I happened to have the most GDI Objects... I guess that this amount of objects comes from the WebBrowsers...?

The original DrawToBitmap code from ILSpy is:

int nWidth = Math.Min(this.Width, targetBounds.Width);
int nHeight = Math.Min(this.Height, targetBounds.Height);
Bitmap image = new Bitmap(nWidth, nHeight, bitmap.PixelFormat);
using (Graphics graphics = Graphics.FromImage(image))
{
    IntPtr hdc = graphics.GetHdc();
    UnsafeNativeMethods.SendMessage(new HandleRef(this, this.Handle), 791, hdc, (IntPtr)30);
    using (Graphics graphics2 = Graphics.FromImage(bitmap))
    {
        IntPtr hdc2 = graphics2.GetHdc();
        SafeNativeMethods.BitBlt(new HandleRef(graphics2, hdc2), targetBounds.X, targetBounds.Y, nWidth, nHeight, new HandleRef(graphics, hdc), 0, 0, 13369376);
        graphics2.ReleaseHdcInternal(hdc2);
    }
    graphics.ReleaseHdcInternal(hdc);
}

The documentation claims that Webbrowser does not support DrawToBitmap, but it works nicely until some stage when it just throws this exception.

解决方案

Even this post is very old I want to help to solve this problem, because I had the same and I solved it this way:

private static Graphics graphics = null;
private static IntPtr hdc = IntPtr.Zero;
private static IntPtr dstHdc = IntPtr.Zero;

private static Capture()
{
    if (graphics == null)
    {
       hdc = GetDC(IntPtr.Zero);
       graphics = Graphics.FromImage(bitmap);
       dstHdc = graphics.GetHdc();
    }
    BitBlt(dstHdc, 0, 0, screen_resolution.Width, screen_resolution.Height, hdc, 0, 0, RasterOperation.SRC_COPY);
}

The solution for me was, that I call graphics.GetHdc() only one time when the object is Zero. After that I never use call graphics.GetHdc().

这篇关于使用Graphics.GetHdc时,参数是无效异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆