Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good morning to everyone. Please help:
Bitmap b= new Bitmap("H:/project/program/bala.bmp");
int[] image=new int[b.Width*b.Height];
BitmapData lockData = b.LockBits(
   new Rectangle(0, 0, b.Width, b.Height),
   System.Drawing.Imaging.ImageLockMode.ReadWrite ,
   System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
try
{
 System.Runtime.InteropServices.Marshal.Copy(
   lockData.Scan0, image, 0, image.Length);

 Marshal.Copy(image, 0, lockData.Scan0, image.Length);
}
catch (Exception e)
{
 Console.WriteLine(e);
 }
b.UnlockBits(lockData);

This program produces the error
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)


[From second Question]
Good morning to everyone.
I have tried the SetPixel function to set the pixel value in the image. But the SetPixel method shows an error for the indexed pixel format images. Please reply for this question.
Posted
Updated 9-Dec-09 0:13am
v4

1 solution

A guess: Marshal.Copy is overloaded, and the type of object it copies is given by the first parameter (http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.copy.aspx[^]), here it's 'int'.

Your bitmap only has 8 bits per pixel, so when you try to copy 'length' number of ints, you're copying four times as much space as the bitmap has: Each int you copy contains 4 pixels.
 
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