Marshal.Copy generates an "Attempted to read or write protectedmemory" error

A

aaron.radich

The Marshal.Copy statement in the following code block generates an
"Attempted to read or write protected memory. This is often an
indication that other memory is corrupt." error. This happens when
the bitmap.PixelFormat is set to Format4bppIndexed (I've set to this
format because the bitmap uses a 16 entry color palette). The
PixelFormat.Format32bppArgb doens't generate the error, but the
bitmaps don't have the image. I'm assuming because it's not indexed
when using the PixelFormat.Format32bppArgb. Please let me know how I
can get around the protected memory error.

Aaron

private Bitmap CopyDataToBitmap(ref byte[] data, ColorPalette
cpPalette)
{
// here create the Bitmap to the know height, width and format
//Bitmap bmp = new Bitmap(iSCREEN_WIDTH, iSCREEN_HEIGHT,
PixelFormat.Format32bppArgb);
Bitmap bmp = new Bitmap(iSCREEN_WIDTH, iSCREEN_HEIGHT,
PixelFormat.Format4bppIndexed);
bmp.Palette = cpPalette;

// create a BitmapData and Lock all pixels to be written
BitmapData bmpData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.WriteOnly, bmp.PixelFormat);

// copy the data from the byte array into BitmapData.Scan0
// the following statement generates the following error if the
bitmap pixel format is set to 4bppIndexed
// error: "Attempted to read or write protected memory. This is
often an indication that other memory is corrupt."
Marshal.Copy(data, 0, bmpData.Scan0, data.Length);

// unlock the pixels
bmp.UnlockBits(bmpData);

// return the bitmap
return bmp;
}
 
A

aaron.radich

The Marshal.Copy statement in the following code block generates an
"Attempted to read or write protected memory.  This is often an
indication that other memory is corrupt." error.  This happens when
the bitmap.PixelFormat is set to Format4bppIndexed (I've set to this
format because the bitmap uses a 16 entry color palette).  The
PixelFormat.Format32bppArgb doens't generate the error, but the
bitmaps don't have the image.  I'm assuming because it's not indexed
when using the PixelFormat.Format32bppArgb.  Please let me know how I
can get around the protected memory error.

You need to post a concise-but-complete code sample for anyone to know for  
sure what your problem is.

Until you do that, here's my guess: the length of your byte[] is not the  
same as expected for the image.  Since you're using the length of the  
byte[], that suggests that for some reason your Bitmap instance isn't  
allocating as much memory as you have in your byte[].  This could either  
be because the byte[] is too long, or because you're using the wrong  
dimensions for the Bitmap.

If you want something more specific than that, you need to post a  
concise-but-complete code sample that reliably demonstrates the problem.  
For that matter, there's no guarantee that my guess is correct, so such a 
sample would be required if you want to be assured of a _correct_ answer, 
never mind specific.

Pete

Sorry I didn't include all the detail. The bitmap image is 300 x
216. The byte[] array length is 64800 (300x216 = 64,800). The
ColorPalette has 16 entries in it. Am I missing something? Should
the byte array be a different size?

Aaron
 
A

aaron.radich

The Marshal.Copy statement in the following code block generates an
"Attempted to read or write protected memory.  This is often an
indication that other memory is corrupt." error.  This happens when
the bitmap.PixelFormat is set to Format4bppIndexed (I've set to this
format because the bitmap uses a 16 entry color palette).  The
PixelFormat.Format32bppArgb doens't generate the error, but the
bitmaps don't have the image.  I'm assuming because it's not indexed
when using the PixelFormat.Format32bppArgb.  Please let me know how I
can get around the protected memory error.

You need to post a concise-but-complete code sample for anyone to know for  
sure what your problem is.

Until you do that, here's my guess: the length of your byte[] is not the  
same as expected for the image.  Since you're using the length of the  
byte[], that suggests that for some reason your Bitmap instance isn't  
allocating as much memory as you have in your byte[].  This could either  
be because the byte[] is too long, or because you're using the wrong  
dimensions for the Bitmap.

If you want something more specific than that, you need to post a  
concise-but-complete code sample that reliably demonstrates the problem.  
For that matter, there's no guarantee that my guess is correct, so such a 
sample would be required if you want to be assured of a _correct_ answer, 
never mind specific.

Pete


FYI, I fixed the issue by changing the length to this:

int iBytes = bmpData.Stride * bmp.Height;
//Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
Marshal.Copy(data, 0, bmpData.Scan0, iBytes);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top