Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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;


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?
Posted
Updated 26-Jun-12 19:19pm
v2
Comments
Bernhard Hiller 27-Jun-12 5:13am    
Are pictureWater.Width and width the same? Also height.
Did you try ImageLockMode.ReadWrite?
MicroSoft's example at http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx uses some Marshalling.

Instead of
C#
byte* pixel = (byte*)data.Scan0.ToPointer();


use
C#
byte* pixel = (byte*)data.Scan0;


Refer this link[^] for bitmap read write operaton.
 
Share this answer
 
yes, i have tried ImageLockMode.ReadWrite too, but still no use..

and width = canvas.Width;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900