非托管/不安全代码的访问冲突异常 [英] Access violation Exception with Unmanaged/unsafe code

查看:82
本文介绍了非托管/不安全代码的访问冲突异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用不安全的代码控制位图数据,该代码在2008年可以正常运行,而同一代码在vs 2010中运行时会引发内存访问冲突异常.

代码如下:

Hi,

I have control bitmap data using unsafe code,which is working perfectly in 2008 whereas the same code is raising memory access violation exception when run in vs 2010.

The code is as below:

Bitmap canvas = new Bitmap(pictureWater.Width, pictureWater.Height);
BitmapData data = canvas.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

byte* pixel = (byte*)data.Scan0.ToPointer();
pixel[0] = (byte)0;//blue
pixel[1] = (byte)color;//green
pixel[2] = (byte)0;//red
pixel[3] = (byte)255;



我已经使用验证工具验证了框架.Net 4.0并成功.
该系统在32位Windows XP Service Pack3上运行.

任何人都可以帮忙,这可能是问题所在吗?



I have verified framework .Net 4.0 using verification tool and succeeded.
The system is running on 32 bit windows XP service pack3

Can anybody help,what might be the problem?

推荐答案

而不是
byte* pixel = (byte*)data.Scan0.ToPointer();



使用



use

byte* pixel = (byte*)data.Scan0;



请参考此链接 [



Refer this link[^] for bitmap read write operaton.


是的,我也尝试过ImageLockMode.ReadWrite,但仍然没有用. />
和width = canvas.Width;
yes, i have tried ImageLockMode.ReadWrite too, but still no use..

and width = canvas.Width;


这篇关于非托管/不安全代码的访问冲突异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆