代码如下
public Graphics g;
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            g = e.Graphics;
            g.FillEllipse(new SolidBrush(Color.Red), 5, 5, 200, 100);
            g.DrawRectangle(new Pen(Color.Blue, 4.0f), 230, 5, 150, 100);
            Font font = new Font("楷体_GB2313", 32);
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            g.DrawString("点击按扭保存图像", font, new SolidBrush(Color.Blue), 15, 110);        }        private void save_Click(object sender, EventArgs e)
        {
                     
Bitmap image = new Bitmap(this.Width, this.Height,this.g);//此名有问题,参数无效。那如何解决,
            //Graphics imageG = Graphics.FromImage(image);
            //imageG.Clear(this.BackColor);
            //Form1_Paint(this, new PaintEventArgs(imageG, this.ClientRectangle));
            image.Save("saveform.jpg", ImageFormat.Jpeg);
        }

解决方案 »

  1.   

    Bitmap image = new Bitmap(this.Width, this.Height,g);//
      

  2.   

    你设个断点看一下this.Width, this.Height,this.g这三个都是什么值
      

  3.   

    this.g是什么啊?大概是这里出了问题,仔细查查看。
      

  4.   

    两种解决办法:
    一、Bitmap image = new Bitmap(this.Width, this.Height,this.g)改为
    Bitmap image = new Bitmap(this.Width, this.Height)二、在Bitmap image = new Bitmap(this.Width, this.Height,this.g)前加上
    g = this.CreateGraphics();
      

  5.   

    Graphics用Bipmap来构建
    在保存前,把Graphics创建为PaintEventArgs去调用Form1_Paint
      

  6.   

    private void Form1_Paint(object sender, PaintEventArgs e)
            {
                g = e.Graphics;
            }//在Form1_Paint中的e.Graphics,出来这个函数后e.Graphics就不能用了。
    //把 Bitmap image = new Bitmap(this.Width, this.Height,this.g)
    //这句放到Form1_Paint中试试。
      

  7.   

    回jointan()你的方法我已以用过,是可以的,也就是我注释的地方,我就不
    明白在//Form1_Paint(this, new PaintEventArgs(imageG, this.ClientRectangle));
    这句做了什么,与下面的写法区别在哪
    回楼上你的方法我早就试过如下,还是不行,要不你们自己试一下
    public partial class Form1 : Form
        {
           
           public bool msave;
            public Form1()
            {
            
                InitializeComponent();
                msave = false;
            }        private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                g.FillEllipse(new SolidBrush(Color.Red), 5, 5, 200, 100);
                g.DrawRectangle(new Pen(Color.Blue, 4.0f), 230, 5, 150, 100);
                Font font = new Font("楷体_GB2313", 32);
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                g.DrawString("点击按扭保存图像", font, new SolidBrush(Color.Blue), 15, 110);
                if (msave)
                {
                    msave = false;
                    Bitmap image = new Bitmap(this.Width, this.Height, g);
                   image.Save("123.jpg", ImageFormat.Jpeg);
                }        }        private void save_Click(object sender, EventArgs e)
            {
                
                msave =true;
                this.Invalidate(this.ClientRectangle);
            }
        }
      

  8.   

    g = this.CreateGraphics();  如何让下一个函数访问是关键
    也就是你要定义全局的
      

  9.   

    运行了,没有问题啊。Form显示正常。只是保存图片有问题,没有把图片保存上。
      

  10.   

    Bitmap image = new Bitmap(this.Width, this.Height, g);
                   image.Save("123.jpg", ImageFormat.Jpeg);
    你的image有内容吗,没有.
    你再添加一句this.BackgroundImage = image可以看到更奇特的现象,呵呵
      

  11.   

    Bitmap image = new Bitmap(this.Width, this.Height, g);
    这个image是没有图像的
    Graphics dc=Graphics.FromImage;
    用dc画图
      

  12.   

    如果只是要保存当前对话框的内容可以用下面的办法,感觉你的办法由问题,就算是image中有内容也是你后来画上去的内容,而不是真正的对话框的内容。
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]   
    public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight,IntPtr hdcSrc,int nXSrc, int nYSrc, int dwRop);   
    private Bitmap memoryImage;   
    private void CaptureScreen()   
    {   
    Graphics mygraphics = this.CreateGraphics();   
    Size s = this.Size;   
    memoryImage = new Bitmap(s.Width, s.Height, mygraphics);   
    Graphics memoryGraphics = Graphics.FromImage(memoryImage);   
    IntPtr dc1 = mygraphics.GetHdc();   
    IntPtr dc2 = memoryGraphics.GetHdc();   
    BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);  
    mygraphics.ReleaseHdc(dc1);   
    memoryGraphics.ReleaseHdc(dc2);   
    memoryImage.Save(@"c:\1243.jpg", ImageFormat.Jpeg);
    }
      

  13.   

    回wdy9927() 可以啦
    把memoryImage = new Bitmap(s.Width, s.Height, mygraphics);改成
    memoryImage = new Bitmap(this.ClientRectangle.Width,this.ClientRectangle.Height, mygraphics);
    呵呵你用的是VC中的思想吧,还是C#本来就有这种方法...................