【问题标题】:How to save image from IFormFile如何从 IFormFile 保存图像
【发布时间】:2020-07-16 03:02:25
【问题描述】:

我的代码中有 IFormFile,我想将图像从这里保存到我的 PC。我该怎么做?

MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);

我尝试使用此代码,但我认为它是旧代码,所以它不起作用

【问题讨论】:

    标签: c# image asp.net-core file-upload iformfile


    【解决方案1】:

    这应该可以(如果您知道图像的格式,例如 Jpeg):

    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    returnImage.Save(@"c:/your/desired/path/here/name.jpeg");
    

    如果您不知道图像格式,下面的代码可帮助您将其保存为所需的格式(Bmp、Tiff、Jpeg、Png、Gif...):

    using System.Drawing.Imaging;//add this with rest of usings
    
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    returnImage.Save(@"c:/your/desired/path/here/name.png", ImageFormat.Png);//in this example the format would be png
    

    【讨论】:

    • Image应该是什么类型?我的 VS 告诉'System.Net.Mime.MediaTypeNames.Image',但没有方法 FromStream()
    • 我已经编辑了我的答案,以反映您不知道图像类型的时间。
    • 我试过了。这不是工作。我找到了这个答案stackoverflow.com/a/43017938/13122668
    【解决方案2】:

    我已经做到了。这是我使用的代码:

        using FreeImageAPI;                   
    var bytes = person.Image.GetBytes();
    
    
    MemoryStream ms = new MemoryStream(bytes.Result);
    
    var b = FreeImageBitmap.FromStream(ms);
        b.Save("23",FREE_IMAGE_FORMAT.FIF_JPEG);
    

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      相关资源
      最近更新 更多