Click here to Skip to main content
11,496,146 members (452 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C# GDI+
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 13-Dec-10 3:48am
Edited 13-Dec-10 7:40am
v4
Comments
Abdul Quader Mamun at 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;
        }
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 13 Dec 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100