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;
}