位图到整数例程生成异常

Bitmap to Integer routine generating exception

    public static bool IsGrayscale(Bitmap bitmap)
    {
        return bitmap.PixelFormat == PixelFormat.Format8bppIndexed ? true : false;
    }

.

    public static int[,] ToInteger(Bitmap image)
    {
        if (Grayscale.IsGrayscale(image))
        {
            Bitmap bitmap = (Bitmap)image.Clone();

            int[,] array2d = new int[bitmap.Width, bitmap.Height];

            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                                     ImageLockMode.ReadWrite,
                                                     PixelFormat.Format8bppIndexed);
            int bytesPerPixel = sizeof(byte);

            unsafe
            {
                byte* address = (byte*)bitmapData.Scan0;

                int paddingOffset = bitmapData.Stride - (bitmap.Width * bytesPerPixel);

                for (int i = 0; i < bitmap.Width; i++)
                {
                    for (int j = 0; j < bitmap.Height; j++)
                    {
                        int iii = 0;

                        //If there are more than 1 channels...
                        //(Would actually never occur)
                        if (bytesPerPixel >= sizeof(int))
                        {
                            byte[] temp = new byte[bytesPerPixel];

                            //Handling the channels.
                            //PixelFormat.Format8bppIndexed == 1 channel == 1 bytesPerPixel == byte
                            //PixelFormat.Format32bpp       == 4 channel == 4 bytesPerPixel == int
                            for (int k = 0; k < bytesPerPixel; k++)
                            {
                                temp[k] = address[k];
                            }

                            iii = BitConverter.ToInt32(temp, 0);
                        }
                        else//If there is only one channel:
                        {
                            iii = (int)(*address);
                        }

                        array2d[i, j] = iii;

                        address += bytesPerPixel;
                    }

                    address += paddingOffset;
                }
            }

            bitmap.UnlockBits(bitmapData);

            return array2d;
        }
        else
        {
            throw new Exception("Not a grayscale");
        }
    }

以下行中的异常:

 iii = (int)(*address);

An unhandled exception of type 'System.AccessViolationException' occurred in Fast Fourier Transform.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

该异常的原因是什么?

我该如何解决?

.

.

P.S. 我正在使用以下 PNG 图片:

您在循环中将 bitmap.Width 误认为是 bitmap.Height。您应该迭代外循环中的高度和内循环中的宽度,因为步幅代表整个宽度 + 图像的偏移量。然后你可以在行的基础上添加 padding,而不是为遍历的每一行添加。所以

for (int i = 0; i < bitmap.Height; i++)
{
    for (int j = 0; j < bitmap.Width; j++)
    {

正在工作。此外,您必须将数组访问从 array2d[i, j] = iii 交换到 array2d[j, i] = iii,因为索引现在属于图像的另一个维度