从字节数组中获取 Image 对象

发布于 2024-09-10 11:18:21 字数 280 浏览 2 评论 0原文

我有一个图像的字节数组(存储在数据库中)。我想创建一个 Image 对象,创建几个不同大小的图像并将它们存储回数据库中(将其保存回字节数组)。

我不担心数据库部分或调整大小。但是有没有一种简单的方法可以加载 Image 对象而不将文件保存到文件系统,然后在调整大小后将其放回字节数组中?如果可以的话,我想把这一切都记在记忆里。

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

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[]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

日久见人心 2024-09-17 11:18:22

您可以使用 MemoryStream 来执行此操作:

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

You'd use a MemoryStream to do this:

byte[] bytes;
...
using (var ms = new System.IO.MemoryStream(bytes)) {
   using(var img = Image.FromStream(ms)) {
      ...
   }
}
静水深流 2024-09-17 11:18:22

根据您对另一个答案的评论,您可以尝试对存储在 byte[] 中的图像执行转换,然后将结果作为另一个 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();
            }
        }
    }
}

这将允许您传入存储在数据库中的 byte[],执行您需要的任何转换,然后返回一个可以存储回的新 byte[]数据库。

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[].

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.

孤君无依 2024-09-17 11:18:22

仅回答问题的前半部分:这是一个单行解决方案,对于包含 JPEG 文件图像的字节数组来说,它对我来说效果很好。

Image x = (Bitmap)((new ImageConverter()).ConvertFrom(jpegByteArray));

编辑:
这是一个稍微更高级的解决方案:https://stackoverflow.com/a/16576471/253938

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.

Image x = (Bitmap)((new ImageConverter()).ConvertFrom(jpegByteArray));

EDIT:
And here's a slightly more advanced solution: https://stackoverflow.com/a/16576471/253938

薄荷港 2024-09-17 11:18:22

我想我应该添加这个作为答案以使其更加明显。

将其保存回字节数组:

    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();
        }

    }

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();
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文