本文介绍了如何Bitmap.Save(流,的imageformat)格式化的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非透明的,彩色的位图长2480,宽3507。

I have a non transparent, colour bitmap with length 2480 and width 3507.

使用 Bitmap.GetPixel(INT X,int y)对我能够得到位图中的每个像素的颜色信息。

Using Bitmap.GetPixel(int x, int y) I am able to get the colour information of each pixel in the bitmap.

如果我喷的位图到一个byte []:

If I squirt the bitmap into a byte[]:

MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Bmp);
ms.Position = 0;
byte[] bytes = ms.ToArray();

那么我希望有相同的信息,即我可以去字节[1000]和读取的颜色信息的像素。

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.

事实证明,我的字节数组比我预期的要大。我想我会得到一个包含有2480 x 3507 = 8697360元。相反,我得到一个数组8698438元 - 某种头的我presume

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.

在什么样的格式在我的数组中的字节存储?是否有一个头长其次是Alpha,红,绿,蓝值1078字节,每个字节的元素,还是其他什么东西?

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.

推荐答案

您正在呼叫的GetBuffer 返回底层的字节数组 - 这是大于的实际长度流。

You're calling GetBuffer which returns the underlying byte array - that's bigger than the actual length of the stream.

的使用

byte[] bytes = ms.ToArray();

的使用的GetBuffer 但与 ms.Length 一起使用。

or use GetBuffer but in conjunction with ms.Length.

说了这么多,你将其保存为BMP - 所以就会有头信息为好;它不喜欢的第一个字节将重新present第一像素。不幸的是有没有原始的形象,据我所看到的格式...这就是它听起来像什么,你真正想要的。

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.

您可以使用 Bitmap.LockBits ,然后将数据从那里复制,如果你想......

You could use Bitmap.LockBits and then copy the data from there, if you want...

这篇关于如何Bitmap.Save(流,的imageformat)格式化的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:25