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);
'System.AccessViolationException' 유형의 처리되지 않은 예외가 고속 푸리에 Transform.exe
부가 정보 발생하려고 보호 된 메모리를 읽거나 씁니다. 이것은 종종 다른 메모리가 손상되었음을 나타냅니다.
그 예외의 원인은 무엇인가?
어떻게 해결할 수 있습니까?
.
.
P. 나는 다음 PNG 이미지를 사용하고 있습니다 : 당신은 당신의 루프에서 bitmap.Height
에 대한 bitmap.Width
을 착각
포인터를 직접 사용하려면 코드 섹션을 안전하지 않은 것으로 선언해야합니다. – Kevin
@ 케빈, 나는 이미 그것을 안전하지 않다고 선언했다. 그렇지 않으면 실행되어서는 안됩니다. – anonymous
예제 사진을 게시 할 수 있습니까? 나에게 코드가 잘 작동하기 때문에 – technikfischer