Click here to Skip to main content
15,940,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to display a image using pointers in c#
The code is as below.
C#
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
Comments
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?

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: http://blogs.msdn.com/b/astebner/archive/2009/04/16/9553804.aspx[^]

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.
 
Share this answer
 
Comments
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???
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.
 
Share this answer
 
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();

C#
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
 
Share this answer
 
v2

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