Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When converting an Image to 8bpp there are vertical lines in result.
The Code used is,

C#
static Bitmap To8bpp(Bitmap original)
        {
            int width = original.Width;
            int height = original.Height;
            Bitmap result = new Bitmap(width, height);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    Color color = original.GetPixel(x, y);
                    result.SetPixel(x, y, color);
                }
            }
            Rectangle rectangle = new Rectangle(0, 0, width, height);
            PixelFormat pixelFormat = PixelFormat.Format8bppIndexed;
            BitmapData bitmapData = result.LockBits(rectangle, ImageLockMode.ReadOnly, result.PixelFormat);
            Bitmap bitmap = new Bitmap(width * 4, height, bitmapData.Stride, pixelFormat, bitmapData.Scan0);
            result.UnlockBits(bitmapData);
            return bitmap;
        }


What I have tried:

For whatever reason there are vertical lines in the resulting image. I have tried googling the reason for this with no success in explaining why this is occurring. If anyone understands what's happening here or knows of a good link that could better explain this would be great. Any help in this matter is highly appreciated.
Posted
Updated 30-Jan-23 8:24am
v2

1 solution

I did a quick Google Search: c# gdi+ image to 8bpp[^]

Found this: GDI+ Error converting 24bpp JPG to 8bpp Indexed format (PNG or GIF)[^]

Which then pointed to this: C# - Loading an indexed color image file correctly[^] with a working solution.

I have not tried the solution, so I can't confirm that it will work. However, it appears like a valid solution. If not, there are other discussions on this which appear to be a common issue that is resolvable.
 
Share this answer
 
Comments
charles henington 30-Jan-23 14:27pm    
Thanks, yes I've read these, I can convert the image to 8bpp but I was curious to why there are vertical lines in the output image.

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