我使用下面一种方法,将图像的像素的RGB值读入数组byte[] PixelValues中,读入后我发现了一个问题,很奇怪,但不知怎么解决;问题是:我发现有的图像,会在每一行后多出一个像素点(这一像素点的RGB值都是0),这样产生的问题是每一行都多一个像素点,PixelValues的大小就要变成(iWidth+1)*iHight*3才能把所有像素的值都存入数组中,如果只申请iWidth*iHeight*3的大小,由于每一行多出的那个像素(我也不知哪来的),就会导致最后一行丢失了后面的一部分像素(正好丢了iWidth个像素)但有些图像就没有这个问题,每一行后面就没有多出来那个像素,所以我要是都申请为(iWidth+1)*iHeight*3的话,这些图像又会有越界的错误...小弟刚学没多久,不太清楚两幅图像到底有什么不同,会导致这种问题的出现?这又该怎么处理呢?
Bitmap Source int iWidth = Source.Width;
int iHeight = Source.Height; Rectangle rect = new Rectangle(0, 0, iWidth, iHeight);
BitmapData bmpData = Source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, Source.PixelFormat);
IntPtr iPtr = bmpData.Scan0;
int iBytes = iWidth * iHeight * 3;
byte[] PixelValues = new byte[iBytes];
System.Runtime.InteropServices.Marshal.Copy(iPtr, PixelValues, 0, iBytes);
Source.UnlockBits(bmpData);
Bitmap Source int iWidth = Source.Width;
int iHeight = Source.Height; Rectangle rect = new Rectangle(0, 0, iWidth, iHeight);
BitmapData bmpData = Source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, Source.PixelFormat);
IntPtr iPtr = bmpData.Scan0;
int iBytes = iWidth * iHeight * 3;
byte[] PixelValues = new byte[iBytes];
System.Runtime.InteropServices.Marshal.Copy(iPtr, PixelValues, 0, iBytes);
Source.UnlockBits(bmpData);
byte[] PixelValues = new byte[iBytes]; 在非固定大小和颜色不确定的情况下.
1 色彩位
色彩位1位 4位 8位(256色) 16位 32位 都是错位..
2 补码(行对齐)
图形可能行最后有补码..会少读几个点的字节..更换成
int iBytes =bmpData.Stride * bmpData.Height;
byte[] PixelValues = new byte[iBytes];