My code works for some images but not others.
Image that works:
Image that doesn’t work (same image but smaller):
I first load the image and create an array of raw pixel data that for instance only includes the red channel data.
public byte[] GetPixels(Bitmap bmp)
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
int length = bmpData.Stride * bmpData.Height;
int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
byte[] pixelData = new byte[length];
byte[] newPixelData = new byte[length / bytesPerPixel];
Marshal.Copy(bmpData.Scan0, pixelData, 0, length);
for(int i = 0; i < newPixelData.Length; i++)
{
newPixelData[i] = pixelData[i*bytesPerPixel];
}
bmp.UnlockBits(bmpData);
bmp.Dispose();
return newPixelData;
}
Then I call a method that turns that byte[] into an 8bit per pixel indexed bitmap. I want everything else to be the same so width, height, resolution is all derived from the original image.
public Bitmap makeBitmap(byte[] bytes)
{
Bitmap bmp;
PixelFormat pxFormat = PixelFormat.Format8bppIndexed;
int stride = img.Width;
unsafe
{
fixed (byte* ptr = bytes)
{
bmp = new Bitmap(img.Width, img.Height, stride, pxFormat, (IntPtr)ptr);
}
}
bmp.SetResolution(img.HorizontalResolution,img.VerticalResolution);
ColorPalette palette = bmp.Palette;
for (int i = 0; i < 256; i++)
{
palette.Entries[i] = Color.FromArgb(i,i,i);
}
bmp.Palette = palette;
return bmp;
}
This program works fine with the 1st image I showed but won’t work with the second. here is the error I get:
The Stride seems to be the problem but i’m not sure how to fix it.. Can anyone advise on how to fix this? Imagine photoshop split channels. That’s sort of what I’m trying to do each channel will be a separate grayscale image). I’m doing calculations with the channel data as well so having the raw pixel data in a byte[] has really helped me cut down on memory usage with the larger images.