Image processing 图像填充不正确

Image processing 图像填充不正确,image-processing,bitmap,bitmapdata,Image Processing,Bitmap,Bitmapdata,输出 我认为下面的代码没有给出正确的结果 以下代码有什么问题 public class ImagePadder { public static Bitmap Pad(Bitmap image, int newWidth, int newHeight) { int width = image.Width; int height = image.Height; if (width >= newWidth) throw new

输出

我认为下面的代码没有给出正确的结果

以下代码有什么问题

public class ImagePadder
{
    public static Bitmap Pad(Bitmap image, int newWidth, int newHeight)
    {
        int width = image.Width;
        int height = image.Height;

        if (width >= newWidth) throw new Exception("New width must be larger than the old width");
        if (height >= newHeight) throw new Exception("New height must be larger than the old height");

            Bitmap paddedImage = Grayscale.CreateGrayscaleImage(newWidth, newHeight);

            BitmapLocker inputImageLocker = new BitmapLocker(image);
            BitmapLocker paddedImageLocker = new BitmapLocker(paddedImage);

            inputImageLocker.Lock();
            paddedImageLocker.Lock();

            //Reading row by row
            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {     
                    Color col = inputImageLocker.GetPixel(x, y);

                    paddedImageLocker.SetPixel(x, y, col);
                }
            }

            string str = string.Empty;                

            paddedImageLocker.Unlock();
            inputImageLocker.Unlock();

            return paddedImage;            
    }
}

Windows位图的布局与您预期的不同。图像的底线是内存中的第一行,并从那里向后继续。当高度为负值时,它也可以以另一种方式布置,但这种情况并不常见

位图中偏移量的计算似乎考虑到了这一点,因此您的问题必须更加微妙

int i = (Height - y - 1) * Stride + x * cCount;
问题是,
BitmapData
类已经考虑到了这一点,并试图为您修复它。我上面描述的位图是自下而上的位图。从
BitmapData.Stride的文档中:

跨距是一行像素(扫描线)的宽度,四舍五入到四字节边界如果步幅为正值,则位图为自上而下。如果步幅为负数,则位图为自底向上。


它旨在与
Scan0
属性一起使用,以一致的方式访问位图,无论是自顶向下还是自底向上。

Windows位图的布局与您预期的不同。图像的底线是内存中的第一行,并从那里向后继续。当高度为负值时,它也可以以另一种方式布置,但这种情况并不常见

位图中偏移量的计算似乎考虑到了这一点,因此您的问题必须更加微妙

int i = (Height - y - 1) * Stride + x * cCount;
问题是,
BitmapData
类已经考虑到了这一点,并试图为您修复它。我上面描述的位图是自下而上的位图。从
BitmapData.Stride的文档中:

跨距是一行像素(扫描线)的宽度,四舍五入到四字节边界如果步幅为正值,则位图为自上而下。如果步幅为负数,则位图为自底向上。

它旨在与
Scan0
属性一起使用,以一致的方式访问位图,无论是自顶向下还是自下而上