Click here to Skip to main content
11,930,788 members (57,158 online)
Rate this:
Please Sign up or sign in to vote.
I am trying to display a image using pointers in c#
The code is as below.
byte* ptr = (byte*)image.Scan0.ToPointer() + py * stride + px * pixelSize;
                            ptr[0] = color.B;
                            ptr[1] = color.G;
                            ptr[2] = color.R;
                            ptr[3] = (byte)255;
When i run it in VS 2008 it works fine,but the same code in VS 2010 is raising an exception as followed.

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Any idea appreciated..!
Posted 25-Jun-12 2:08am
A. Ganzer 25-Jun-12 9:42am
It would be much better to see the complete source code that works the bitmap.

E.g. there is no call to LockBits() and UnlockBits() visible in your code. What is all done between these calls?

And very important: do you read the image data from a file or a resource? What platform is the project compiled for? On which platform (32/64 bit) runs Visual Studio?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Yeps. The issue is related to VS2010, especially .NET Framework 4.0

I recommend you use .NET Framework Setup Verification Tool to verify the .NET Framework 4 to see if it is healthy.

If this tool verify .NET Framework 4 successfully, I think this problem is related to your application. You can try to debug them in new environment.

If .NET Framework 4 failed to be verified, you can try to repair or reinstall it. Please try repair in Control Panel first. If this problem still exists, I recommend you repair it via command line:[^]

If the repair fails again, I recommend you uninstall .NET Framework 4 via the command line that page provided. After you uninstall it, reinstall it. Ensure the state of .NET Framework 4 is healthy, then try to run your application.
Sandeep Mewara 25-Jun-12 9:14am
Comment from OP:
Thank you,
I am currently using .Net 4.0.We had the application running in 2008 but now moved it into 2010.

I have verified .Net framework using .NET Framework Setup Verification Tool,and the the verification is successful.

So now i think that is not the problem.

Is there any possibility of other reasons,may be???
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

I run your code on following Systems: Windows 7, 32 bit and 64 bit. The program is created for any CPU and for x86 explicitly by using VS 2010 and .NET 3.5 as well as 4.0. All is done well.

Probably you have not posted the complete code. E.g: what is width and height?

The only possible error I have found is the formula:

(byte*)image.Scan0.ToPointer() + py * stride + px * pixelSize

You are possibly calcutating a pixel that is beyond the available number of pixels of the image. Where do you get the used variables from? Did you trace out or debug the content of the used pointers? Maybe there are some different project settings between VS 2008 and VS 2010 which causes a different calculation of the memory addresses.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

First we are locking bits then on that image we are drawing

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] = color.B;//blue
pixel[1] = color.G;//green
pixel[2] = (byte)0;//red
pixel[3] = (byte)255;

And the solution is running on 32 bit platform

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 | Mobile
Web04 | 2.8.151126.1 | Last Updated 27 Jun 2012
Copyright © CodeProject, 1999-2015
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