AccessViolationException错误

时间:2012-12-10 09:15:53

标签: c# access-violation opencvsharp

我想问你一个问题。当我厌倦使用Marshal.ReadByte时,我收到一条错误“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”我搜索了一下但我仍然无法解决这个问题。你能帮助我吗?这是我的代码。

unsafe static void Main(string[] args)
        {
 IplImage I_input = Cv.LoadImage("1.6.tif", LoadMode.GrayScale);

  IntPtr ptr2 = I_input.ImageData;

            for (int x = 0; x < I_input.Width; x++)
            {
                for (int y = 0; y < I_input.Height; y++)
                {
                    int offset = (I_input.WidthStep * y) + (x * 3);
                    byte b = Marshal.ReadByte(ptr2, offset + 0);    // B
                    byte g = Marshal.ReadByte(ptr2, offset + 1);    // G
                    byte r = Marshal.ReadByte(ptr2, offset + 2);    // R
                    Marshal.WriteByte(ptr2, offset, r);
                    Marshal.WriteByte(ptr2, offset, g);
                    Marshal.WriteByte(ptr2, offset, b);
                }
            }
}

0 个答案:

没有答案