获取图像对象从一个字节数组 [英] Getting an Image object from a byte array

查看:131
本文介绍了获取图像对象从一个字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

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

 字节[]字节;
...
使用(VAR毫秒=新System.IO.MemoryStream(字节)){
   使用(VAR IMG = Image.FromStream(MS)){
      ...
   }
}
 

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)) {
      ...
   }
}

这篇关于获取图像对象从一个字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆