简体   繁体   English

使用目录asp.net创建位图图像时参数无效

[英]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. 但是当我创建位图obj时,它在循环内给出了错误,它说“ invalid parameter”。即使我检查了目录中文件的位置是否正确,但仍然显示错误。 please tell me whats wrong. 请告诉我怎么了。 thanks in advance 提前致谢

Get rid of the System.IO.Path.GetFileName call: 摆脱对System.IO.Path.GetFileName调用:

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 . GetFileName D:\\Folder\\234324234\\1.png名称的完整路径截断,例如D:\\Folder\\234324234\\1.png只是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. 我还考虑使用带过滤器GetFiles

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. 现在,您的代码正在提取所有文件,包括可能不是图像的隐藏文件,例如Thumbs.db文件或desktop.ini文件,这些文件是Windows资源管理器内部用于存储有关目录元数据的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM