帮解决。一个自定义控件,在上面画了一个框,想对这个控件截图,输出后没有画的框
UserControlNode control = (UserControlNode)((Panel)palPic.Controls["panelGraph"]).Controls[0];
            Bitmap bitmap = new Bitmap(control.Width, control.Height);
            control.DrawToBitmap(bitmap, new Rectangle(0, 0, control.Width, control.Height));
            //ControlImage.DrawToBitmap(control, bitmap, new Rectangle(0, 0, control.Width, control.Height));
            bitmap.Save(@"C:\Documents and Settings\Administrator\桌面\1.png");

解决方案 »

  1.   


                Bitmap bmp = new Bitmap(100, 100);
                Graphics g = Graphics.FromImage(bmp);
                g.DrawLine(Pens.Red, 0, 0, 100, 100);
                control.BackgroundImage = bmp;
    需要的时候,将bmp存为文件
      

  2.   

    谢谢,已解决是由于我在on_paint中
    将 Graphics g = e.Graphics;
    改为
    Graphics g = this.CreateGraphics();
    的原因。但是我要调用这个方法
    private void UserControlNode_Paint(object sender, PaintEventArgs e)如原来我是直接调用 
    UserControlNode_Paint(this, null);
    因为没有e的参数。所以做了如上修改,
    可以以后调用这个方法时,这个e 如何传参数呀。
      

  3.   

    不用直接调用,调用绘图控件的Invalidate即可触发Paint
      

  4.   

    果然可以,谢谢。
    如果想重绘,就用
    this.Invalidate();您真是厉害。佩服。
      

  5.   

        public partial class UserControl3 : UserControl
        {
            public UserControl3()
            {
                InitializeComponent();
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                // Create pen.
                Pen redPen = new Pen(Color.Red, 3);            // Create coordinates of rectangle to bound ellipse.
                int x = 0;
                int y = 0;
                int width = 100;
                int height = 200;            // Create start and sweep angles on ellipse.
                int startAngle = 45;
                int sweepAngle = 270;            // Draw arc to screen.
                e.Graphics.DrawArc(redPen, x, y, width, height, startAngle, sweepAngle);        }
        }