下面就是源代碼。寫算法不是我的強項,不過偶爾勉為其難的寫個可以跑跑的不求甚解版還是可以做到的,不指望可以幫到你,只希望沒有誤導你。
public static Bitmap KiMosaic(Bitmap b, int val)
{
if (b.Equals(null))
{
return null;
}
int w = b.Width;
int h = b.Height;
int stdR, stdG, stdB;
stdR = 0;
stdG = 0;
stdB = 0;
BitmapData srcData = b.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* p = (byte*)srcData.Scan0.ToPointer();
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (y % val == 0)
{
if (x % val == 0)
{
stdR = p[2]; stdG = p[1]; stdB = p[0];
}
else
{
p[0] = (byte)stdB;
p[1] = (byte)stdG;
p[2] = (byte)stdR;
}
}
else
{
// 復制上一行
byte * pTemp = p - srcData.Stride;
p[0] = (byte)pTemp[0];
p[1] = (byte)pTemp[1];
p[2] = (byte)pTemp[2];
}
p += 3;
} // end of x
p += srcData.Stride - w * 3;
} // end of y
b.UnlockBits(srcData);
}
return b;
}