Button b = new Button();
            this.Controls.Add(b);
            Graphics g = this.CreateGraphics();            IntPtr p = g.GetHdc();
            Bitmap bit = new Bitmap(10, 10, 1, PixelFormat.Format24bppRgb, p);            
            g.ReleaseHdc(p);
            this.BackgroundImage = bit;
报的错误是运行时遇到问题,需要关闭
哪位能帮我看看错在什么地方

解决方案 »

  1.   

    Bitmap bit = new Bitmap(10, 10, 1, PixelFormat.Format24bppRgb, p);
      

  2.   

    Bitmap bit = new Bitmap(10, 10, 1, PixelFormat.Format24bppRgb, p);            
    ====================
    把p去掉,p指的是数据流地址不是HDC
    Bitmap bit = new Bitmap(10, 10, 1, PixelFormat.Format24bppRgb );            
      

  3.   

    Bitmap bit = new Bitmap(10, 10, 1, PixelFormat.Format24bppRgb, p);            
    ====================
    把p去掉,p指的是数据流地址不是HDC
    //Bitmap bit = new Bitmap(10, 10, 1, PixelFormat.Format24bppRgb );  
    //修改为:    
    Bitmap bit = new Bitmap(10, 10, PixelFormat.Format24bppRgb );
      

  4.   

    谢谢,改成你那样的话,那就和GRAPHICS没关系了吧我找到了刚才的错误,把代码修改成这样
     Button b = new Button();
                this.Controls.Add(b);
                Graphics g = this.CreateGraphics();            IntPtr p = g.GetHdc();
                //Bitmap bit = new Bitmap(10, 10, 1, PixelFormat.Alpha, p);
                Bitmap bit = new Bitmap(100, 100,4, PixelFormat.Format24bppRgb,p);
    ////第三个参数必须是4的倍数
                g.ReleaseHdc(p);
                this.BackgroundImage = bit;但又有了新的错误了:
    有关调用实时(JIT)调试而不是此对话框的详细信息,
    请参见此消息的结尾。************** 异常文本 **************
    System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
       在 System.Drawing.SafeNativeMethods.Gdip.GdipCreateTexture(HandleRef bitmap, Int32 wrapmode, IntPtr& texture)
    在 System.Drawing.TextureBrush..ctor(Image image, WrapMode wrapMode)
       在 System.Windows.Forms.ControlPaint.DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft)
       在 System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
       在 System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
       在 System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
       在 System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)
       在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
       在 System.Windows.Forms.Control.WmEraseBkgnd(Message& m)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
       在 System.Windows.Forms.Form.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
      

  5.   

    DC指的是显示设备的上下文,那个函数最后一个参数要求的事图像内容在内存的指针,根本不是一回事,本来就没关系。
    要显示图像,或者说在Graphics这个图形对象绘制这个image,你需要创建image对象后,
    用Graphics.DrawImage