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.

Recommended Answers

All 4 Replies

Hi,

You need to save them seperately, because when you merge them then you get the colors back. If you use the same value for all three components (rgb) then it willl be grayscale.
So if you saved the components separately, you can use later the

Color.FromArgb(alpha,red,green,blue)

and you'll get the original pixel back.

I hope it helps, gl

Dear, Thanks for guidance, I have saved them separately and since they were only one color so they seems to be of gray scale. But when I am combining them they are still of gray color.

Hi,

You need to save them seperately, because when you merge them then you get the colors back. If you use the same value for all three components (rgb) then it willl be grayscale.
So if you saved the components separately, you can use later the

Color.FromArgb(alpha,red,green,blue)

and you'll get the original pixel back.

I hope it helps, gl

How are you combining them? Can't debug code we can't see.

I am using following code:

 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;

        }

How are you combining them? Can't debug code we can't see.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.