我试图将图像从1bpp转换为8bpp:
BitmapData dstData = dstImage.LockBits(
new Rectangle(0, 0, dstImage.Width, dstImage.Height),
ImageLockMode.ReadWrite, dstImage.PixelFormat);
try
{
int width = srcData.Width;
int height = srcData.Height;
unsafe
{
byte* srcBase = (byte*)srcData.Scan0.ToPointer();
byte* dstBase = (byte*)dstData.Scan0.ToPointer();
int srcStride = srcData.Stride;
int dstStride = dstData.Stride;
for (int y = 0; y < height; y++)
{
ushort* src = (ushort*)(srcBase + y * srcStride);
ushort* dst = (ushort*)(dstBase + y * dstStride);
for (int x = 0; x < width; x++)
{
byte mask = (byte)(0x80 >> (x & 0x07));
*dst = ((*src) & mask) != 0 ? (ushort)255 : (ushort)0;
if (mask == 1)
{
src++;
}
dst++;
}
}
}
}
finally
{
dstImage.UnlockBits(dstData);
}
新图像不是预期的灰度图像。设置像素值的算法似乎是错误的。
答案 0 :(得分:0)
var src = srcBase + y * srcStride; var dst = dstBase + y * dstStride;
for (int x = 0; x < width; x++, dst++)
{
byte mask = (byte)(0x80 >> (x & 0x07));
*dst = ((*src) & mask) != 0 ? (byte)255 : (byte)0;
if (mask == 1)
{
src++;
}
}