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;
}
"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;
}