winform,我用GDI+在窗体上画图,Graphics g = form.CreateGraphics(); 可是我怎样能把我画的保存为一张图片呢?

解决方案 »

  1.   

    我想你要保存的图最好先画到内存中,如:
      Bitmap bmp = new Bitmap(1024,768);
        Graphics g=Graphics.FromImage(bmp)
    在上面这个g中画完后,用bmp.Save(.....)方法保存即可
      

  2.   

    我不明白你为何一定要在Form中画,你可以在内存中画完再放到Form中去的,这也是GDI+的一种叫双缓冲技术的。
      

  3.   

    我的内容很大,用几千条数据,在picture中画占的内存有100M.所以最后选择在form里画
      

  4.   

    在窗体上画图,必然在Paint事件里实现,把事件参数中的Graphics传给一个方法,要生成图片时也调用它。
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Draw(e.Graphics);
    }
    private void Draw(Grapics g)
    {
      ...
    }
    private void btnSaveToImg_Click(object sender, System.EventArgs e)
    {
      Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, this.CreateGraphics());
      Draw(Graphics.FromImage(bmp));
      bmp.Save("C:\abc.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
    }