【发布时间】:2016-08-04 17:11:34
【问题描述】:
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”类型的未处理异常 发生在 Fast Fourier Transform.exe 中
附加信息:试图读取或写入受保护的内存。 这通常表明其他内存已损坏。
该异常的原因是什么?
我该如何解决这个问题?
.
.
P.S.我使用的是以下PNG图片:
【问题讨论】:
-
要直接使用指针,你必须将代码段声明为不安全的。
-
@Kevin,我已经宣布它是不安全的。否则,它不应该一直在运行。
-
你能发一个示例图片吗?因为对我来说代码运行良好
-
@technikfischer,完成!请看一下。
-
如何加载图片?因为使用此图像,代码告诉我它不是灰度的。或者原始文件是哪种文件格式?
标签: c# exception image-processing bitmap bitmapdata