I am trying to convert an array of integers in to a bitmap. I have an array of integers in range [0 - 4095] and I want to display them in to a bitmap, than have the user pick an area in the bitmap make a copy of that area and then get the integers back.
I have code that works for cutting the area of the bitmap, but I am having problems with making the bitmap with values as high as 4095, and getting values back from the bitmap as high as 4095.
public Bitmap InttoBitmap(int[] array)
{
unsafe
{
int[] pixels = new int[array.Length];
for (int i = 0; i < keepWidth * keepHeight; i++)
{
pixels[i] = array[i] * 64;
}
IntPtr pixptr = Marshal.AllocHGlobal(keepWidth * keepHeight * 2);
Marshal.Copy(pixels, 0, pixptr, keepWidth * keepHeight);
Bitmap bitmap = new Bitmap(keepWidth, keepHeight, 2 * keepWidth, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale, pixptr);
return bitmap;
}
}
I get a run time error at:
Marshal.Copy(pixels, 0, pixptr, keepWidth * keepHeight);
it says:
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
update:
double Average_intensity2(Bitmap bmp)
{
double[,] image;
double answer = 0;
image = imageToByteArray(bmp);
double l = 0;
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
answer = answer + image[y,x];
l++;
}
}
answer = answer / l;
return answer;
}