0
Reply

Merging 3 grayscale bitmaps into RGB

iinfoque

iinfoque

Aug 24 2010 7:56 AM
1.9k


Hi friends, I have separated Red, Green and Blue color from a bitmap image and stored it in grayscale (obviously).  Now I want to know how can I merge it so that I can get the exact previous colour, when I am merging them by reading its color values it is giving  me grayscale image :( . Please help me out with some working sample code. Thanking in anticipation.
 
I am using following code to merge:
 public bool mergeToRGBandWriteBitmap(string sourceRFile, string sourceGFile, string sourceBFile, string targetFile)
        {
            bool isSuccessfullywritten = false;
            try
            {
                Bitmap bitmapRed = (Bitmap)Image.FromFile(sourceRFile);
                Bitmap bitmapGreen = (Bitmap)Image.FromFile(sourceGFile);
                Bitmap bitmapBlue = (Bitmap)Image.FromFile(sourceBFile);
   Bitmap afterMerge = mergeRGB(bitmapRed, bitmapGreen, bitmapBlue);
             isSuccessfullywritten = true;
  }  
 

public Bitmap mergeRGB(Bitmap sourceR, Bitmap sourceG, Bitmap sourceB)
        {
            Bitmap bm = new Bitmap(sourceR.Width, sourceR.Height);
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color cBitmapR = sourceR.GetPixel(x, y);
                    Color cBitmapG = sourceG.GetPixel(x, y);
                    Color cBitmapB = sourceB.GetPixel(x, y);
                    int luma = (int)(cBitmapR.R * 0.3 + cBitmapR.G * 0.59 + cBitmapR.B * 0.11);
                    bm.SetPixel(x, y, Color.FromArgb((int)cBitmapR.R, (int)cBitmapR.G, (int)cBitmapR.B));
                }
            }
            return bm;
        }