Bitmap.Save(Stream, ImageFormat) 如何格式化数据?
我有一个长度为 2480、宽度为 3507 的非透明彩色位图。
使用 Bitmap.GetPixel(int x, int y) 可以获取位图中每个像素的颜色信息。
如果我将位图喷射到 byte[]: 中,
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Bmp);
ms.Position = 0;
byte[] bytes = ms.ToArray();
那么我希望获得相同的信息,即我可以转到 bytes[1000] 并读取该像素的颜色信息。
事实证明我的字节数组比我预期的要大。我想我会得到一个包含 2480 x 3507 = 8697360 个元素的数组。相反,我得到了一个包含 8698438 个元素的数组 - 我认为是某种标头。
我的数组中的字节以什么格式存储?是否有一个 1078 字节长的标头,后面跟着每个字节元素的 Alpha、Red、Green、Blue 值,或者其他什么?
我所需要的只是每个像素的颜色信息。我不关心标题(或者实际上是透明度),除非我需要它来获取颜色信息。
I have a non transparent, colour bitmap with length 2480 and width 3507.
Using Bitmap.GetPixel(int x, int y)
I am able to get the colour information of each pixel in the bitmap.
If I squirt the bitmap into a byte[]:
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Bmp);
ms.Position = 0;
byte[] bytes = ms.ToArray();
then I'd expect to have the same information, i.e. I can go to bytes[1000] and read the colour information for that pixel.
It turns out that my array of bytes is larger than I anticipated. I thought I'd get an array with 2480 x 3507 = 8697360 elements. Instead I get an array with 8698438 elements - some sort of header I presume.
In what format the bytes in my array stored? Is there a header 1078 bytes long followed by Alpha, Red, Green, Blue values for every byte element, or something else?
All I need is the colour information for each pixel. I'm not concerned with the header (or indeed the transparency) unless I need it to get the colour info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用 GetBuffer ,它返回底层字节数组 - 它大于流的实际长度。
使用
或使用
GetBuffer
,但与ms.Length
结合使用。话虽如此,您将其保存为 BMP - 因此也会有标题信息;它不像第一个字节代表第一个像素。不幸的是,据我所知,没有“原始”图像格式......这听起来像是您真正想要的。
您可以使用
Bitmap.LockBits
然后从那里复制数据,如果你想要的话......
You're calling
GetBuffer
which returns the underlying byte array - that's bigger than the actual length of the stream.Either use
or use
GetBuffer
but in conjunction withms.Length
.Having said that, you're saving it as a BMP - so there'll be header information as well; it's not like the first byte will represent the first pixel. Unfortunately there's no "raw" image format as far as I can see... that's what it sounds like you really want.
You could use
Bitmap.LockBits
and then copy the data from there, if you want...如果我正确理解有关 Bitmap.Save 的文档,它会以指定的格式保存图像,这意味着您的字节数组中将包含位图标头。我认为您应该阅读一些有关 位图格式 的文档,以了解如何获取所需的信息你的数组
If I understand correctly the documentation about
Bitmap.Save
, it saves the image in the specified format, which means that you'll have in your bytes array, the bitmap header. I think you should read some documentation about the bitmap format to know how to get the needed information in your array