C# .NET - How to apply RGB values in each raw data pixel? But I had Problem for the following Pixel

Asked By rajkumar palanisamy on 16-Jun-14 06:38 AM
PixelFormat.Format48bppRgb
PixelFormat.Format32bppArgb
PixelFormat.Format32bppPArgb
PixelFormat.Format32bppRgb

My process:
        Step 1: Open Tiff image in File Dialog.
        Step 2: Opened image convert to Bitmap
        Step 3: Bitmap image Passed to private byte[] GetRawBytesFromBmp(Bitmap bmp)
        Step 4: Lock the bitmap so we can access the raw pixel data  
        Step 5: Now I Get BytesPerPixel

        if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb || bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed || bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format48bppRgb)
          {
            BytesPerPixel = System.Drawing.Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
            rawStride = BytesPerPixel * width;
          }
        Step 6:Conver to BytesPerPixel to Raw bytes
         raw = new byte[rawStride * height];

        Step 7:I use unsafe code for apply rgb values each raw data pixel for PixelFormat.Format24bpprgb and Format8bppIndexed


        byte* bmpPtr = (byte*)data.Scan0.ToPointer();

              fixed (byte* rawPtr = raw)
                if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
                {
                  for (row = 0; row < height; ++row)
                  {
                    for (column = 0; column < rawStride; column += BytesPerPixel)
                    {
                      // rawPtr[row * rawStride + column] = bmpPtr[row * bmpStride + column];  //b
                      //   rawPtr[row * rawStride + column + 1] = bmpPtr[row * bmpStride + column + 1]; //g
                      // rawPtr[row * rawStride + column + 2] = bmpPtr[row * bmpStride + column + 2]; //r

                      rawPtr[row * rawStride + column] = bmpPtr[row * bmpStride + column + 2];   //b
                      rawPtr[row * rawStride + column + 1] = bmpPtr[row * bmpStride + column + 1]; //g
                      rawPtr[row * rawStride + column + 2] = bmpPtr[row * bmpStride + column]; ; //r
                    }
                  }
                  // SwapRedBlue(raw);
                }
                else if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
                {
                  for (row = 0; row < height; ++row)
                  {   // iterate over width (columns)
                    for (column = 0; column < rawStride; column += BytesPerPixel)
                    {
                      rawPtr[row * rawStride + column] = bmpPtr[row * bmpStride + column];  //r
                    }
                  }
                }

         Step 8: I have problem in the section  PixelFormat48bppRgb so please help me
              else if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format48bppRgb)
                {
                 fixed (byte* rawPt = raw)
                      for (int y = 0; y < data.Height; y++)
                    {
                   byte* rawPtr = (byte*)data.Scan0 + (y * data.Stride);

                    for (int x = 0; x < data.Width; x++)
                    {
                  //rawPtr[x * BytesPerPixel] = 255;

                      rawPt[x * BytesPerPixel] = rawPtr[y * data.Width+x+2];//b
                      rawPt[x * BytesPerPixel+1] = rawPtr[y * data.Width + x + 1];//g
                      rawPt[x * BytesPerPixel + 2] = rawPtr[y * data.Width + x];//r

                   }
               }

                }
           Step 9: I have problem in the section PixelFormat32bpprgb so please help me
              else if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppRgb || bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb || bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
              {
                byte* bData1Scan0Ptr = (byte*)data.Scan0.ToPointer();
                byte* nextBase = bData1Scan0Ptr + data.Stride;

                for (int y = 0; y < data.Height; ++y)
                 {
                   ushort* pRow = (ushort*)bData1Scan0Ptr;

                    for (int x = 0; x < data.Width; ++x)
                     {
                       var red = pRow[2];
                       var green = pRow[1];
                       var blue = pRow[0];

                       pRow += 4;
                      }

                    bData1Scan0Ptr = nextBase;
                   nextBase += data.Stride;
                  }

               }