Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 13 Dec 2010
Copyright © CodeProject, 1999-2014
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