Click here to Skip to main content
13,138,372 members (51,123 online)
Rate this:
Please Sign up or sign in to vote.

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;

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 26-Jun-12 19:05pm
Updated 26-Jun-12 19:19pm
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 uses some Marshalling.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

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

Refer this link[^] for bitmap read write operaton.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

yes, i have tried ImageLockMode.ReadWrite too, but still no use..

and width = canvas.Width;

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 3 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100