如何正确读取图像文件?

时间:2016-05-23 02:03:43

标签: c# image bitmap filestream

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

  

"参数无效"。

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

2 个答案:

答案 0 :(得分:2)

使用

Image I = Image.FromFile("FilePath");

并使用该图像

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

答案 1 :(得分:1)

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

我建议你使用Bitmap的构造函数,它将文件路径作为参数。

return new Bitmap(Fname);