How to convert byte array to image file?

后端 未结 3 670
我寻月下人不归
我寻月下人不归 2021-02-02 09:52

I have browsed and uploaded a png/jpg file in my MVC web app. I have stored this file as byte[] in my database. Now I want to read and convert the byte[] to original file. How c

相关标签:
3条回答
  • 2021-02-02 10:24

    Create a memory stream from the byte[] array in your database and then use Image.FromStream.

    byte[] image = GetImageFromDatabase();
    MemoryStream ms = new MemoryStream(image);
    Image i = Image.FromStream(ms);
    
    0 讨论(0)
  • 2021-02-02 10:41

    May you have trouble with the mentioned solutions on DotNet Core 3.0 or higher
    so my solution is:

    using(var ms = new MemoryStream(yourByteArray)) {
       using(var fs = new FileStream("savePath", FileMode.Create)) {
          ms.WriteTo(fs);
       }
    }
    
    0 讨论(0)
  • 2021-02-02 10:43
    1. Create a MemoryStream passing the array in the constructor.
    2. Read the image from the stream using Image.FromStream.
    3. Call theImg.Save("theimage.jpg", ImageFormat.Jpeg).

    Remember to reference System.Drawing.Imaging and use a using block for the stream.

    0 讨论(0)
提交回复
热议问题