从字节数组中获取 Image 对象
我有一个图像的字节数组(存储在数据库中)。我想创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 MemoryStream 来执行此操作:
You'd use a MemoryStream to do this:
根据您对另一个答案的评论,您可以尝试对存储在
byte[]
中的图像执行转换,然后将结果作为另一个byte[]
返回。这将允许您传入存储在数据库中的
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 anotherbyte[]
.This will allow you to pass in the
byte[]
stored in the database, perform whatever transformations you need to, then return a newbyte[]
that can be stored back in the database.仅回答问题的前半部分:这是一个单行解决方案,对于包含 JPEG 文件图像的字节数组来说,它对我来说效果很好。
编辑:
这是一个稍微更高级的解决方案: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.
EDIT:
And here's a slightly more advanced solution: https://stackoverflow.com/a/16576471/253938
我想我应该添加这个作为答案以使其更加明显。
将其保存回字节数组:
I thought I'd add this as an answer to make it more visible.
With saving it back to a byte array: