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 using
scope before it's put into your list. Remove the using
.
using
will call Dispose
on your image, which will invalidate the object. So, when you put it in the List
you're putting in a dead object.
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);
}