如何将字节数组转换为图像文件?

时间:2022-12-12 23:00:06

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?

我浏览过并在我的MVC网络应用程序中上传了一个png / jpg文件。我已将此文件存储为数据库中的byte []。现在我想读取byte []并将其转换为原始文件。我怎样才能做到这一点?

2 个解决方案

#1


28  

  1. Create a MemoryStream passing the array in the constructor.
  2. 创建一个在构造函数中传递数组的MemoryStream。
  3. Read the image from the stream using Image.FromStream.
  4. 使用Image.FromStream从流中读取图像。
  5. Call theImg.Save("theimage.jpg", ImageFormat.Jpeg).
  6. 调用im.Save(“theimage.jpg”,ImageFormat.Jpeg)。

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

请记住引用System.Drawing.Imaging并对流使用using块。

#2


17  

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

从数据库中的byte []数组创建一个内存流,然后使用Image.FromStream。

byte[] image = GetImageFromDatabase();
MemoryStream ms = new MemoryStream(image);
Image i = Image.FromStream(ms);

#1


28  

  1. Create a MemoryStream passing the array in the constructor.
  2. 创建一个在构造函数中传递数组的MemoryStream。
  3. Read the image from the stream using Image.FromStream.
  4. 使用Image.FromStream从流中读取图像。
  5. Call theImg.Save("theimage.jpg", ImageFormat.Jpeg).
  6. 调用im.Save(“theimage.jpg”,ImageFormat.Jpeg)。

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

请记住引用System.Drawing.Imaging并对流使用using块。

#2


17  

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

从数据库中的byte []数组创建一个内存流,然后使用Image.FromStream。

byte[] image = GetImageFromDatabase();
MemoryStream ms = new MemoryStream(image);
Image i = Image.FromStream(ms);