【问题标题】:Having issues reading a image file and putting it into a byte array读取图像文件并将其放入字节数组时出现问题
【发布时间】:2019-09-14 03:49:08
【问题描述】:

所以我在 DbContext.SeedData.cs 中工作

我正在返回一个带有适当信息的方法,例如名称、id、描述。那些工作。我试图在 DbContext.SeedData.cs 中读取图像文件并将其分配到新的字节数组中。

Image = new byte[]{ FileStream(image, FileMode.Open, FileAccess.Read).Length }

我不断收到的错误是:不可调用的成员 'FileStream' 不能像方法一样使用。

如何在适当的上下文中使用 FileStream 来读取图像并将其转换为字节码?

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    您需要通过以下方式读取所有字节:

    var fs = new FileStream(image, FileMode.Open, FileAccess.Read);
    
    Image = new byte[fs.Length];
    
    fs.Read( Image, 0 , fs.Length);
    

    或使用

    Image = System.IO.File.ReadAllBytes(image);
    

    【讨论】:

      【解决方案2】:

      只需使用:

      Image = System.IO.File.ReadAllBytes(image);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        • 2019-07-27
        • 2015-07-05
        • 2011-10-21
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多