的大小与BMP内存流的大小不同

的大小与BMP内存流的大小不同

本文介绍了位图字节[]的大小与BMP内存流的大小不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过TCP/IP连接发送位图.到目前为止,我的程序可以正常工作.但是在调试过程中,我发现了位图byte []的奇怪值.

I´m trying to send a bitmap over a tcp/ip connection. So far my programm works as it should. But during debugging I discovered a strange value of my bitmap byte[].

我打开一个24位的位图并将其转换为16位.位图是800x600,所以byte []的长度应该是800 * 800 * 2Byte = 960000Byte ...但是我的数组是960054 ...

I open a 24bit bitmap and convert it to 16bit. The bitmap is 800x600 so the byte[] length should be 800*800*2Byte = 960000Byte... But my array is 960054...

多余的字节从哪里来??

Where do the extra Bytes come from??

        Console.WriteLine("Bitmap auf 16Bit anpassen...\n");
        Rectangle r = new Rectangle(0,0,bitmap_o.Width, bitmap_o.Height);
        Bitmap bitmap_n = bitmap_o.Clone(r, PixelFormat.Format16bppRgb555);
        bitmap_n.Save("test2.bmp");

        Console.WriteLine("Neue Bitmap-Eigenschaften:");
        Console.WriteLine(bitmap_n.Width.ToString());
        Console.WriteLine(bitmap_n.Height.ToString());
        Console.WriteLine(bitmap_n.PixelFormat.ToString());

        byte[] data = new byte[0];
        MemoryStream mem_stream = new MemoryStream();
        bitmap_n.Save(mem_stream, ImageFormat.Bmp);
        data = mem_stream.ToArray();
        mem_stream.Close();

        Console.WriteLine(data.Length.ToString());

        stream.Write(data, 0, 960000);
        Console.WriteLine("Sending data...");

推荐答案

多余的字节是文件头,例如:

The extra bytes is the file header, which contains for example:

  • 位图文件签名
  • 图像尺寸(像素大小)
  • 位深度
  • 分辨率(ppi)

像素数据中也可能有多余的字节.在您的情况下,两个字节各占800像素,每条扫描线占1600字节,但是,例如,如果三个字节有145个像素,则占435字节,因此将填充字节添加到每条扫描线以使其等于436.被四等分.

There can also be extra bytes within the pixel data. In your case 800 pixels at two bytes each makes for 1600 bytes per scan line, but if you had for example 145 pixels at three bytes each would make 435 bytes, so a padding byte would be added to each scan line to make it 436 that is evenly divisable by four.

参考: BMP文件格式

这篇关于位图字节[]的大小与BMP内存流的大小不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:46