Praveen Kumar
Praveen Kumar

Reputation: 27

Parameter is not valid exception while trying to create a new bitmap object

Bitmap bmp = new Bitmap(files2.FullName);

I have the above code where files2 is from

foreach (FileInfo files2 in files)

When I process a large number of images, it is giving the exception only for few images. Can any one help me on this?

Thanks

This is my code:

foreach (FileInfo files2 in files)
{
    string nametime = files2.FullName.ToString();
    DateTime createdTime = File.GetCreationTime(nametime);
    //Console.WriteLine(createdTime);

    //test
    if (createdTime.ToShortDateString() == DateTime.Today.ToShortDateString())
    {
        try
        {
            if (files2.Extension == ".jpg" || files2.Extension == ".png" || files2.Extension == ".JPG" || files2.Extension == ".PNG" || files2.Extension == ".jpeg" || files2.Extension == ".JPEG")
            {
                 Console.WriteLine("Name: " + files2.FullName);
                 Bitmap bmp = new Bitmap(files2.FullName);

Upvotes: 1

Views: 1894

Answers (1)

Simon MᶜKenzie
Simon MᶜKenzie

Reputation: 8664

Since this is only happening when you process large batches, it's probably a memory issue. The Bitmap contains unmanaged resources, so you should call Dispose on it when you're finished with it. The best way to do this is implicitly via a using statement:

using (Bitmap bmp = new Bitmap(files2.FullName))
{
    // Process the bitmap here
}

Upvotes: 1

Related Questions