Baiscally, I’m trying to draw an image, with a big number (my iteration variable’s current value) on it:
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);
}
But I get an error when reading from this image list: Parameter is not valid. What am I doing wrong?
The bitmap is going to be destroyed by the
usingscope before it’s put into your list. Remove theusing.usingwill callDisposeon your image, which will invalidate the object. So, when you put it in theListyou’re putting in a dead object.