anyone please help.. how to convert image 8bppIndexed into array and convert array into image 8bppIndexed..?? give me some example coding which is resemble of my code below.. my code below only for 24bppRgb.. now i need for 8bppIndexed.. please help me.....
i have convert image into array using RGB colour 24bppRgb :
public static void convertRgbImageToArray(Bitmap bm, out double[,] r, out double[,] g, out double[,] b)
{
r = new double[bm.Width, bm.Height];
g = new double[bm.Width, bm.Height];
b = new double[bm.Width, bm.Height];
Rectangle rect = new Rectangle(0,0,bm.Width,bm.Height);
BitmapData bmd = bm.LockBits(
rect,
ImageLockMode.ReadOnly,
bm.PixelFormat
);
int bytes = bmd.Stride * bmd.Height;
byte[] bgrVal = new byte[bytes];
IntPtr ptr = bmd.Scan0;
System.Runtime.InteropServices.Marshal.Copy(ptr, bgrVal, 0, bytes);
bm.UnlockBits(bmd);
for (int x = 0; x < bm.Width; x++)
{
for (int y = 0; y < bm.Height; y++)
{
int m = x * 3 + y * bmd.Stride;
b[x,y] = (bgrVal[m]);
g[x, y] = (bgrVal[m + 1]);
r[x, y] = (bgrVal[m + 2]);
}
}
return;
}
And to convert array into RGB :
public static Bitmap convertRgbArrayToImage(double[,]r,double[,]g, double[,]b)
{
Bitmap bm = new Bitmap(b.GetLength(0), b.GetLength(1), PixelFormat.Format24bppRgb);
Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height);
BitmapData bmd = bm.LockBits(
rect,
ImageLockMode.ReadOnly,
bm.PixelFormat
);
int bytes = bmd.Stride * bmd.Height;
byte[] bgrVal = new byte[bytes];
for (int x = 0; x < bm.Width; x++)
{
for (int y = 0; y < bm.Height; y++)
{
int m = x * 3 + y * bmd.Stride;
double bb = (b[x, y]);
if (bb < 0) bb = 0;
if (bb > 255) bb = 255;
bgrVal[m] = (byte)bb;
double gg = (g[x, y]);
if (gg < 0) gg = 0;
if (gg > 255) gg = 255;
bgrVal[m + 1] = (byte)gg;
double rr = (r[x, y]);
if (rr < 0) rr = 0;
if (rr > 255) rr = 255;
bgrVal[m + 2] = (byte)rr;
}
}
IntPtr ptr=bmd.Scan0;
System.Runtime.InteropServices.Marshal.Copy(bgrVal, 0, ptr, bytes);
bm.UnlockBits(bmd);
return bm;
}