获取图像对象从一个字节数组数组、字节、图像、对象

2023-09-03 03:31:44 作者:与孤独握手言和

我有一个字节数组图像(存储在数据库中)。我想创建一个Image对象,创造出不同尺寸的多个图像并将其回存储在数据库(后面将其保存为一个字节数组)。

我不担心数据库的一部分,或进行调整。但有一个简单的方法来加载图像对象而不将文件保存到文件系统,然后将它放回一个字节数组时,我做了调整呢?我想,如果我可以做这一切在内存中。

 喜欢的东西:
图片MYIMAGE =新的图像(字节[]);
要么
myImage.Load(字节[]);
 

解决方案

您会使用一个MemoryStream做到这一点:

 字节[]字节;
...
使用(VAR毫秒=新System.IO.MemoryStream(字节)){
   使用(VAR IMG = Image.FromStream(MS)){
      ...
   }
}
 
Day70,案例 复制文本文件,字节流读数据 一次读一个字节数组数据 ,案例 复制图片

I've got a byte array for an image (stored in the database). I want to create an Image object, create several Images of different sizes and store them back in the database (save it back to a byte array).

I'm not worried about the database part, or the resizing. But is there an easy way to load an Image object without saving the file to the file system, and then put it back in a byte array when I'm done resizing it? I'd like to do it all in memory if I can.

Something like:
Image myImage = new Image(byte[]);
or
myImage.Load(byte[]);

解决方案

You'd use a MemoryStream to do this:

byte[] bytes;
...
using (var ms = new System.IO.MemoryStream(bytes)) {
   using(var img = Image.FromStream(ms)) {
      ...
   }
}