Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何正确读取图像文件?_C#_Image_Bitmap_Filestream - Fatal编程技术网

C# 如何正确读取图像文件?

C# 如何正确读取图像文件?,c#,image,bitmap,filestream,C#,Image,Bitmap,Filestream,我尝试使用FileStream读取图像文件,它成功地读取了它,但它输出了此错误消息 “参数无效” 从溪流打开时,溪流必须保持打开状态 我建议您使用位图的构造函数,将文件路径作为参数 return new Bitmap(Fname); 使用 并使用该图像 Bitmap bm= new Bitmap(I); 或 你可以像这样编辑你的代码 public Bitmap streamimage(string Fname) { Bitmap bm; FileStream stream =

我尝试使用
FileStream
读取图像文件,它成功地读取了它,但它输出了此错误消息

“参数无效”


从溪流打开时,溪流必须保持打开状态

我建议您使用位图的构造函数,将文件路径作为参数

return new Bitmap(Fname);
使用

并使用该图像

Bitmap bm= new Bitmap(I);

你可以像这样编辑你的代码

public Bitmap streamimage(string Fname)
{
    Bitmap bm;
    FileStream stream = new FileStream(Fname, FileMode.Open, FileAccess.Read);
    bm = (Bitmap)Image.FromStream(stream);
    return bm;
}

你知道吗?您可以尝试删除该
流。Close()
,它将自动关闭,因为它使用
包装在
中。注意……我可以知道“参数无效”的原因吗?啊,是的,MSDN引用:“您必须在映像的生命周期内保持流打开。”选项1非常理想(+1),它会处理所有内部。
Bitmap bm= new Bitmap(I);
Bitmap bm= new Bitmap("FilePath");
public Bitmap streamimage(string Fname)
{
    Bitmap bm;
    FileStream stream = new FileStream(Fname, FileMode.Open, FileAccess.Read);
    bm = (Bitmap)Image.FromStream(stream);
    return bm;
}