.net 从字节数组中获取 Image 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3290060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting an Image object from a byte array
提问by Dan Williams
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).
我有一个图像的字节数组(存储在数据库中)。我想创建一个 Image 对象,创建几个不同大小的 Image 并将它们存储回数据库(将其保存回字节数组)。
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.
我不担心数据库部分或调整大小。但是有没有一种简单的方法可以在不将文件保存到文件系统的情况下加载 Image 对象,然后在完成调整大小后将其放回字节数组中?如果可以的话,我想把这一切都记在心里。
Something like:
Image myImage = new Image(byte[]);
or
myImage.Load(byte[]);
回答by Dave Markle
You'd use a MemoryStream to do this:
您将使用 MemoryStream 来执行此操作:
byte[] bytes;
...
using (var ms = new System.IO.MemoryStream(bytes)) {
using(var img = Image.FromStream(ms)) {
...
}
}
回答by RenniePet
Only answering the first half of the question: Here's a one-liner solution that works fine for me with a byte array that contains an image of a JPEG file.
只回答问题的前半部分:这是一个单行解决方案,它适用于包含 JPEG 文件图像的字节数组。
Image x = (Bitmap)((new ImageConverter()).ConvertFrom(jpegByteArray));
EDIT: And here's a slightly more advanced solution: https://stackoverflow.com/a/16576471/253938
编辑:这里有一个更高级的解决方案:https: //stackoverflow.com/a/16576471/253938
回答by Adam Robinson
Based on your comments to another answer, you can try this for performing a transformation on an image that's stored in a byte[]then returning the result as another byte[].
根据您对另一个答案的评论,您可以尝试使用此方法对存储在 a 中的图像执行转换,byte[]然后将结果作为 another 返回byte[]。
public byte[] TransformImage(byte[] imageData)
{
using(var input = new MemoryStream(imageData))
{
using(Image img = Image.FromStream(input))
{
// perform your transformations
using(var output = new MemoryStream())
{
img.Save(output, ImageFormat.Bmp);
return output.ToArray();
}
}
}
}
This will allow you to pass in the byte[]stored in the database, perform whatever transformations you need to, then return a new byte[]that can be stored back in the database.
这将允许您传入byte[]存储在数据库中的数据,执行您需要的任何转换,然后返回一个byte[]可以存储回数据库的新数据。
回答by Dan Williams
I thought I'd add this as an answer to make it more visible.
我想我会添加这个作为答案,使其更加明显。
With saving it back to a byte array:
将其保存回字节数组:
public Image localImage;
public byte[] ImageBytes;
public FUU_Image(byte[] bytes)
{
using (MemoryStream ImageStream = new System.IO.MemoryStream(bytes))
{
localImage = Image.FromStream(ImageStream);
}
localImage = ResizeImage(localImage);
using (MemoryStream ImageStreamOut = new MemoryStream())
{
localImage.Save(ImageStreamOut, ImageFormat.Jpeg);
ImageBytes = ImageStreamOut.ToArray();
}
}

