-我在使用Bitmap.SetPixel时遇到异常:
 “如果再调用”GetHdc方法后使用Graphics对象,请调用ReleaseHdc方法”
-在VS2005下
using (Bitmap bmp = new Bitmap("1.bmp"))
                {
                    bmp.SetPixel(0, 0, Color.Blue);
                }而且在使用MSDN中SetPixel方法的示例的时候也是相同的错误 
而且似乎没有直接调用Win32API。  请问怎么回事?

解决方案 »

  1.   

    在实验Bitmap类的时候
    这个语句可以在随便一个ButtonClick事件中测试
    而且根据MSDN的Bitmap.SetPixel示例在OnPaint中也不行
      

  2.   

    大家可以看看MSDN的System.Drawing.Bitmap下的SetPixel的示例
    试一下就可以看到如果再调用”GetHdc方法后使用Graphics对象,请调用ReleaseHdc方法”的错误
      

  3.   

    下面是我参照msdn里楼主说的System.Drawing.Bitmap下的SetPixel的示例做的例子,也没有问题啊
    protected override void OnPaint(PaintEventArgs e)
            {
                Bitmap myBitmap = new Bitmap("1.bmp");            // Draw myBitmap to the screen.
                e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width,
                    myBitmap.Height);            // Set each pixel in myBitmap to black.
                for (int Xcount = 0; Xcount < myBitmap.Width/10; Xcount++)
                {
                    for (int Ycount = 0; Ycount < myBitmap.Height/10; Ycount++)
                    {
                        myBitmap.SetPixel(Xcount, Ycount, Color.Black);
                    }
                }            // Draw myBitmap to the screen again.
                e.Graphics.DrawImage(myBitmap, 0, 0,
                    myBitmap.Width, myBitmap.Height);            base.OnPaint(e);
            }
      

  4.   

    我的是Win2003 sp1 VS2005 professional build8.0.502742 DX9.0C
    XP-sp2底下也试了!
    都是在“myBitmap.SetPixel(Xcount, Ycount, Color.Black);”语句通过编译,但实例化时弹出错误:未处理InvalidOperationException:
    “如果再调用”GetHdc方法后使用Graphics对象,请调用ReleaseHdc方法”
    :(
    大家都没有这样的情形么?