【发布时间】:2017-04-16 08:43:23
【问题描述】:
下图显示了当我们转换大小为 1x2 的 24 位彩色图像时会发生什么。
.
下图显示了当我们转换大小为 1x2 的 32 位彩色图像时会发生什么。
我想,24 位图像将占用 6 个字节。但是,两者都占用 8 个字节。
因此,我的 C# 代码失败了。因为,它假定一个像素需要(ColorDepth/8)*Width*Height 字节数。
public static int[,] ToInteger(Bitmap bitmap)
{
BitmapLocker locker = new BitmapLocker(bitmap);
locker.Lock();
byte[] data = locker.ImageData;
int Width = locker.Width;
int Height = locker.Height;
int noOfBytesPerPixel = locker.BytesPerPixel;
int[,] integerImage = new int[Width, Height];
int byteCounter = 0;
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
int integer = BitConverter.ToInt32(data, byteCounter);
integerImage[i, j] = integer;
byteCounter += noOfBytesPerPixel;
}
}
locker.Unlock();
return integerImage;
}
那么,到底发生了什么?
【问题讨论】:
-
查看stride!
标签: c# image-processing bitmap