Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 :
C#
public static void convertRgbImageToArray(Bitmap bm, out double[,] r, out double[,] g, out double[,] b) 
        { 
            // make an array that would be refeneccing into the function
            r = new double[bm.Width, bm.Height];
            g = new double[bm.Width, bm.Height];
            b = new double[bm.Width, bm.Height];

            // threshold for image that has a 24bppRGB
            
            // Copy RGB value from the image
            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); // Copy data value RGB from pointer ptr into array bgrVal
            bm.UnlockBits(bmd);

            // devide array value into RGB colour (Red, Green, Blue).
            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 :
C#
public static Bitmap convertRgbArrayToImage(double[,]r,double[,]g, double[,]b)
        {
            // make image.
            Bitmap bm = new Bitmap(b.GetLength(0), b.GetLength(1), PixelFormat.Format24bppRgb);

            // make byte array that use for the image
            // Copy RGB value from image.
            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];

            // Copy pixel value into byte array
            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); // Copy array RGB value into pointer.
            bm.UnlockBits(bmd);
            return bm;
        }
Posted
Updated 7-Mar-12 16:51pm
v2

1 solution

You don't need "doubles" for your RGB's, their range is 0 -> 255, but that's by-the-by

Are you wanting to return the indices, or the RGB values? Basically, the difference between 24bppRgb is that there are 3 bytes per pixel, one for each colour component. In 8bppIndexed, there's 1, but rather than returning you your RGB value, it returns you an index that you can then look up against the palette of your bitmap - and this will provide your RGB values.

So where you do "x * 3", remove the "* 3", and remove the bits wher you reference bgrVal[m + 1] and bgrVal[m + 2], then use bgrVal[m] on the bitmap's palette parameter
 
Share this answer
 
Comments
dingoxiii66 12-Mar-12 2:32am    
thx.. i'll try it now..

regard..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900