110,533
社区成员
发帖
与我相关
我的任务
分享
byte[,] Data;//保存像素矩阵
int width_ = 0;
int heigth_ = 0;
int chnum_ = 0;
long buff_ = 0;
int tool = axCKVisionCtrl1.GetTool("采集图像");
buff_ = axCKVisionCtrl1.GetImageBuffer(tool, ref width_, ref heigth_, ref chnum_);
Bitmap bmp = new Bitmap(width_,heigth_);
// Lock the bitmap's bits.
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
this.Width = bmpData.Width;
this.Height = bmpData.Height;
Data = new byte[Height, Width];
unsafe
{
byte* ptr = (byte*)buff_;
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{
//将24位的RGB彩色图转换为灰度图
int temp = (int)(0.114 * (*ptr++)) + (int)(0.587 * (*ptr++))+(int)(0.299 * (*ptr++));
Data[i, j] = (byte)temp;
}
ptr += bmpData.Stride - Width * 3;//指针加上填充的空白空间
}
}
bmp.UnlockBits(bmpData);
pictureBox1.Image = bmp;
我的代码如上所示,width_为获取到的宽度,height_为获取到的高度,buff_为获取到的图像首地址,现在运行到“ int temp = (int)(0.114 * (*ptr++)) + (int)(0.587 * (*ptr++))+(int)(0.299 * (*ptr++));”这句话就报错,提示““System.AccessViolationException”类型的未经处理的异常在 WindowsFormsApplication1.exe 中发生
其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”