Bitmap中RGB数据提取以及4字节对齐

在对bitmap类图像进行RGB数据提取时,个人发生过图像错位的情况,而这种情况的原因是由于4字节的对齐:内存分配单位是4字节,而位图中每行象素的数据是连续的,下一行不能和上一行共一个分配单元(4字节),所以每行象素的数据长度必须是4字节的倍数,当不足时不足的部分将用0填充

所以一个每行17像素的Bitmap,其实际存储的像素数据为17*3=51字节,而51/4=12.75,也就是说会占用13个分配单元,其中剩下的第52字节就会用0填充,此时如果不将其跳过仍然将其作为图像数据读取就会造成图像的错位

public static bool GetRGB(Bitmap Source, out int[,] R, out int[,] G, out int[,] B)//分离RGB波段

        {
            
            int iWidth = Source.Width;

            int iHeight = Source.Height;

            Rectangle rect = new Rectangle(0, 0, iWidth, iHeight);//将Bitmap锁定到系统内存中,获得BitmapData

            System.Drawing.Imaging.BitmapData bmpData = Source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, Source.PixelFormat);

            IntPtr iPtr = bmpData.Scan0;//获取第一个像素地址

            int four_byte_for_width = (iWidth * 3 + 3) / 4 * 4;//计算4字节对齐后每行的字节数

            int iBytes = four_byte_for_width * iHeight;

            byte[] PixelValues = new byte[iBytes];

            System.Runtime.InteropServices.Marshal.Copy(iPtr, PixelValues, 0, iBytes);

            Source.UnlockBits(bmpData);//解锁

            R = new int[iHeight, iWidth];

            G = new int[iHeight, iWidth];

            B = new int[iHeight, iWidth];

            int iPoint = 0;

            for (int i = 0; i < iHeight; i++)

            {

                for (int j = 0; j < iWidth; j++)

                {

                    B[i, j] = Convert.ToInt32(PixelValues[iPoint++]);

                    G[i, j] = Convert.ToInt32(PixelValues[iPoint++]);

                    R[i, j] = Convert.ToInt32(PixelValues[iPoint++]);

                }
                while ((iPoint) % 4 != 0)//跳过4字节对齐中的空值字节
                {
                    iPoint++;
                }
            }

            return true;

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值