用C#绘制图像

时间:2012-02-29 09:49:25

标签: c# bitmap system.drawing

Baiscally,我正在尝试绘制一个图像,上面有一个大数字(我的迭代变量的当前值):

var imageList = new List<Image>();

for (int i = 1; i <= totalCount; i++)    
{
    using (Bitmap bmp = new Bitmap(800,2000))
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.DrawString(i.ToString(), 
                     new Font("Arial", 40), 
                     Brushes.Black, 
                     new PointF(400,1000));   
    }
    imageList.Add(bmp);
}

但是从这个图片列表中读取时出现错误:参数无效。我做错了什么?

1 个答案:

答案 0 :(得分:3)

在将using范围放入列表之前,位图将被销毁。删除using

using会在您的图片上调用Dispose,这会使对象无效。所以,当你把它放在List中时,你会放入一个死对象。

List<Image> imageList = new List<Image>();
for (int i = 1; i <= totalCount; i++)    
{
        Bitmap bmp = new Bitmap(800,2000))
        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.DrawString(i.ToString(), new Font("Arial", 40), Brushes.Black, new PointF(400,1000));   
        }

        imageList.Add(bmp);
}