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?
4 Answers
- Create a MemoryStream passing the array in the constructor.
- Read the image from the stream using Image.FromStream.
- Call theImg.Save("theimage.jpg", ImageFormat.Jpeg).
Remember to reference System.Drawing.Imaging and use a using block for the stream.
1 Comment
CodeCaster
The Image roundtrip makes no sense if the byte array already contains a valid image. Just ues
File.WriteAllBytes() or whatever to directly write the byte array's contents to a file.Or just use this:
System.IO.File.WriteAllBytes(string path, byte[] bytes)
File.WriteAllBytes(String, Byte[]) Method (System.IO) | Microsoft Docs