How to convert byte array to image file?

2019-03-12 05:07发布

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 can i achieve this?

2条回答
【Aperson】
2楼-- · 2019-03-12 05:34

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);
查看更多
地球回转人心会变
3楼-- · 2019-03-12 05:44
  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.

查看更多
登录 后发表回答