C# 位图扫描0,跨出

C# 位图扫描0,跨出,c#,bitmap,unsafe,C#,Bitmap,Unsafe,我正在编写一个程序,将xored delta应用于现有位图。我遇到的问题是,在第一次迭代中,它似乎偏离了5个像素,导致了一些有趣的色彩效果 private void ApplyDelta(ref Bitmap bitmapA, Bitmap bitmapB, Rectangle bounds) { if (bounds.Width != bitmapB.Width || bounds.Height != bitmapB.Height || bitmapA.PixelFor

我正在编写一个程序,将xored delta应用于现有位图。我遇到的问题是,在第一次迭代中,它似乎偏离了5个像素,导致了一些有趣的色彩效果

private void ApplyDelta(ref Bitmap bitmapA, Bitmap bitmapB, Rectangle bounds)
    {
        if (bounds.Width != bitmapB.Width || bounds.Height != bitmapB.Height || bitmapA.PixelFormat != bitmapB.PixelFormat)
        {
            return;
        }

        BitmapData bmdA = bitmapA.LockBits(bounds, ImageLockMode.ReadWrite, bitmapA.PixelFormat);
        BitmapData bmdB = bitmapB.LockBits(new Rectangle(0, 0, bitmapB.Width, bitmapB.Height), ImageLockMode.ReadOnly, bitmapB.PixelFormat);
        unsafe
        {
            int bytesPerPixel = Image.GetPixelFormatSize(bitmapA.PixelFormat) / 8;
            for (int y = 0; y < bmdA.Height; y++)
            {
                byte* rowA = (byte*)bmdA.Scan0 + (y * bmdA.Stride);
                byte* rowB = (byte*)bmdB.Scan0 + (y * bmdB.Stride);
                for (int x = 0; x < bmdA.Width * bytesPerPixel; x++)
                {
                    rowA[x] ^= rowB[x];
                }
            }
        }

        bitmapA.UnlockBits(bmdA);
        bitmapB.UnlockBits(bmdB);
    }
private void ApplyDelta(参考位图位图位图A、位图位图位图B、矩形边界)
{
if(bounds.Width!=bitmapB.Width | | bounds.Height!=bitmapB.Height | | | bitmapA.PixelFormat!=bitmapB.PixelFormat)
{
返回;
}
BitmapData bmdA=bitmapA.LockBits(边界、ImageLockMode.ReadWrite、bitmapA.PixelFormat);
BitmapData bmdB=bitmapB.LockBits(新矩形(0,0,bitmapB.Width,bitmapB.Height),ImageLockMode.ReadOnly,bitmapB.PixelFormat);
不安全的
{
int bytesperpoixel=Image.GetPixelFormatSize(bitmapA.PixelFormat)/8;
对于(int y=0;y
结果:


步长是一行像素的宽度加上一些填充,以便每行从4字节边界开始以提高效率。从BobPowell.net:

步幅属性。。。以字节为单位保留一行的宽度。然而,行的大小可能不是像素大小的精确倍数,因为为了提高效率,系统确保将数据打包到从四字节边界开始并填充到四字节倍数的行中。这意味着,例如,17像素宽的24位每像素图像的跨距为52。每行中使用的数据将占用317=51字节,1字节的填充将每行扩展到52字节或134字节。17像素宽的4BppIndexed图像的步幅为12。其中九个字节,或者更准确地说是八个半字节,将包含数据,并且该行将用另外3个字节填充到4个字节的边界

有关更多详细信息,请参阅


编辑:重新阅读你的问题,我不确定这是否适用于你的情况。但请确保在计算时记住填充。

经过一些调查后,我从增量生成器中得到了糟糕的位图增量。不将边界应用到bitmapB是没有意义的。bitmapB比bitmapA小,它只是更改位图的一小部分