将createGraphics保存在文件中作为图像

时间:2010-08-23 02:25:03

标签: c# gdi+ picturebox paint

如何将我的图形值传输到位图,以便将其保存为jpg或bmp文件。

这是我的代码:

private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
    {
       using(var p = new Pen(Color.Blue, 4)){
           for (int i = 0; i < _listPS.Count; i++)
           {
            e.Graphics.DrawLine(_pen, _listPS[i], _listPE[i]);
           }
       }
    }

假设_listPS和_listPE具有值。

啊!大声解决了! :) 这是我的解决方案:

private Bitmap _mybitmap;
private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
    {   
        _mybitmap = new Bitmap(pictureBox1.Width, pictureBox1.Heigth);
        Graphics _tempg = Graphics.FromImage(_mybitmap);

       using(var p = new Pen(Color.Blue, 4){
           for (int i = 0; i < _listPS.Count; i++)
           {
               e.Graphics.DrawLine(_pen, _listPS[i], _listPE[i]);
               _tempg.DrawLine(_pen, _listPS[i], _listPE[i]);
           }

           _tempg.Dispose();
        }
    }

1 个答案:

答案 0 :(得分:1)

试试这个

Bitmap _image = new Bitmap(100, 100);
Graphics _g = Graphics.FromImage(_image);

//Graphics _g = pictureBox1.CreateGraphics();
Pen _pen = new Pen(Color.Red, 3);
Point myPoint1 = new Point(10, 20);
Point myPoint2 = new Point(30, 40);

for (int i = 0; i < _listPS.Count; i++)
{
    _g.DrawLine(_pen, _listPS[i], _listPE[i]);
}

_image.Save(@"D:\test.bmp");
_image.Dispose();
_g.Dispose();