Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
public Bitmap Background(Bitmap source)
        {
            width = source.Width;
            height = source.Height;

            BitmapData srcData = source.LockBits(new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, source.PixelFormat);

            byte* src = (byte*)srcData.Scan0.ToPointer();

            dstImage = new Bitmap(width, height,       PixelFormat.Format8bppIndexed);

            BitmapData dstData = dstImage.LockBits(new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, dstImage.PixelFormat);

            byte* dst = (byte*)dstData.Scan0.ToPointer();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++, src++, dst++)
                {
                    *dst = *src;
                }
                src = src + srcData.Stride - srcData.Width;     
                dst =  dst + dstData.Stride - dstData.Width;
            }
            source.UnlockBits(srcData);
            dstImage.UnlockBits(dstData);
            return dstImage;
        }


Hi,

I have this piece of code (please see above) to try to copy one image into another. The original image is grayscale but the final image obtained is colored. After several days of debugging, I realized that the R, G and B components of the final image are not equal while the initial image have equal RGB. My questions are in three parts:

1. What is wrong with the code below
2. The initial and the final images are Format8bppIndexed and I wasn't expecting it to have different channels for R, G and B (it even still has an alpha components)
3. Can someone please refer me to documents or articles describing the detailed structures of Format8bppIndexed image

Thank you.
Posted
Updated 13-Dec-10 6:40am
v4
Comments
Abdul Quader Mamun 13-Dec-10 12:40pm    
Spelling check.

1 solution

You have to copy the color palette too! dstImage.Palette = source.Palette;Btw. you should fix your pointers when using unsafe code!
http://msdn.microsoft.com/en-us/library/f58wzh21(VS.80).aspx[^]
I used the following version of your snippet:

public static unsafe Bitmap CopyToNewBitmap(Bitmap source)
        {
            int width = source.Width;
            int height = source.Height;
            BitmapData srcData = source.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, source.PixelFormat);
            byte* src = (byte*)srcData.Scan0.ToPointer();
            Bitmap dstImage = new Bitmap(width, height, PixelFormat.Format8bppIndexed);

            dstImage.Palette = source.Palette; // copy color palette too!
            BitmapData dstData = dstImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, dstImage.PixelFormat);
             byte* dst = (byte*)dstData.Scan0.ToPointer();
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++, src++, dst++)
                {
                    *dst = *src;
                }
                src = src + srcData.Stride - srcData.Width;
                dst = dst + dstData.Stride - dstData.Width;
            }
            source.UnlockBits(srcData); dstImage.UnlockBits(dstData);
            return dstImage;
        }
 
Share this answer
 

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