将正确的 BytesArray 传递到 Image 的 MemoryStream 中
如果我想从字节数组创建图像,有一种方法可以确定我将正确的字节数组传递给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个非常不祥的问题,您肯定知道您从哪里读取数据?当您使用
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
, anArgumentException
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?我自己也做了一些程序化的图像处理。您要做的就是找到您正在修改的图像格式的规范,并确保您做了您应该做的一切。例如,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.
阅读您的问题和评论后,我认为您正在尝试的是在将图像放入 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 theGraphics
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.这就是答案 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.