将正确的 BytesArray 传递到 Image 的 MemoryStream 中

发布于 2024-09-08 23:03:23 字数 281 浏览 3 评论 0原文

如果我想从字节数组创建图像,有一种方法可以确定我将正确的字节数组传递给 MemoryStream。

MemoryStream mStream = new MemoryStream();
mStream.Write(byteArray, 0, byteArray.Lenth);
Image imgObj = Image.FromStream(mStream);

如果可能的话,如何更正 byteArray 使其成为图像的有效 byteArray?

there a way to determine that I am passing the right byte array to the MemoryStream if I want to create an Image out of byte array.

MemoryStream mStream = new MemoryStream();
mStream.Write(byteArray, 0, byteArray.Lenth);
Image imgObj = Image.FromStream(mStream);

How can I, if possible Correct the byteArray that it is a valid byteArray for an Image?

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

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

发布评论

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

评论(4

ㄖ落Θ余辉 2024-09-15 23:03:23

这是一个非常不祥的问题,您肯定知道您从哪里读取数据?当您使用Image.FromStream创建图像时,如果无法识别格式,将会抛出ArgumentException。为什么不使用该机制来识别不正确的数据流,而不是重新发明轮子?

This is a really ominous question, surely you must know where you are reading your data from? When you create an image using Image.FromStream, an ArgumentException will be thrown if it cannot recognise the format. Why don't you use that mechanism for identifying an incorrect stream of data, rather than re-invent the wheel?

空心↖ 2024-09-15 23:03:23

我自己也做了一些程序化的图像处理。您要做的就是找到您正在修改的图像格式的规范,并确保您做了您应该做的一切。例如,png 文件被分块并且每个部分都有校验和,因此如果您更改该块中的某些内容,则必须在该部分末尾重新计算校验和。

I've done a bit of programatic image manipulation myself. The thing you'll want to do is find the spec for the image format that you are modifying and make sure you do everythign you should. For example png files are chunked and have checksums on each section so if you change something in that chunk you have to recalculate the checksum at the end of the section.

绻影浮沉 2024-09-15 23:03:23

阅读您的问题和评论后,我认为您正在尝试的是在将图像放入 Image 类之前通过操作字节数组来操作图像。现在您声称您的字节数组对于这种图像格式已损坏,以及如何纠正它。

所以这个问题的答案是:你损坏了它,你会修复它。

但要真正解决您的问题,如果您的目标是操作图片本身,只需将其加载到临时 Image 中并使用 Graphics 类来操作您的图片。然后将结果放入您喜欢的真实图像对象中。准备好了,处理字节数组没有任何麻烦。

After reading your questions and your comments, i think what you're trying is to manipulate the image by manipulating the byte array before you put it into the Image class. And now you claim that your byte array is corrupt for this image format and how you can correct it.

So the answer to this question would be: You corrupted it, you'll fix it.

But to really solve your problem, if your goal is to manipulate the picture itself, just load it into an interims Image and use the Graphics class to manipulate your picture. Afterwards put the result into the real image object you like. Ready, without any hassle about working on the byte array.

等数载,海棠开 2024-09-15 23:03:23

这就是答案 C# 和 GDI+ 图像处理

OT:我不知道如何在评论中添加链接,所以我将其放在答案中。

here's the answer Image Processing for Dummies with C# and GDI+

OT: i don't know how to put links on comments so I put it in the answers.

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