saif
saif

Reputation: 123

invalid parameter while creating bitmap image with directory asp.net

i am trying to access images from a directory one by one. here is the code

string[] filesindirectory = Directory.GetFiles(("D:/Folder/32373577989/"));

    foreach (string item in filesindirectory)
    {
        Bitmap bitMapImage = new Bitmap(System.IO.Path.GetFileName(item));
    }

but it gives error within the loop when i create bitmap obj, it says "invalid parameter".Even i checked the location to files inside the directory is correct but still shows error. please tell me whats wrong. thanks in advance

Upvotes: 0

Views: 37

Answers (1)

vcsjones
vcsjones

Reputation: 141638

Get rid of the System.IO.Path.GetFileName call:

foreach (string item in filesindirectory)
{
    Bitmap bitMapImage = new Bitmap(item);
}

GetFileName is truncating the full path to just the name, like D:\Folder\234324234\1.png to just 1.png. When you remove the path like that it is most likely trying to load the image by the current working directory of the process.

I would also consider using the overload of GetFiles that takes in a filter.

Right now your code is pulling in all files, including hidden ones that might not be an image, like a Thumbs.db file or a desktop.ini file - files that Windows Explorer internally uses for storing metadata about the directory.

Upvotes: 3

Related Questions