Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi there.
I'm searching for an algorithm that can remove a range of defined colors from images with high accuracy .For i want it to be fast,the pointers must be used.
Can anyone help me out?

I have tried the following piece of code, but it's not that accurate and fast(Beacuse i want to remove a number of colors from the image) and the speed is important in my program.

I'd be so glad to if you help me with this code!

What I have tried:

public static unsafe Bitmap RemoveColor(Bitmap bitmap,Color c)
        {
            using (var bmp = bitmap)
            {
                var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppPArgb);
                var white = Color.White.ToArgb();
                var black = Color.FromArgb(c.R, c.G, c.B).ToArgb();

                try
                {
                    var length = (int*)data.Scan0 + bmp.Height * bmp.Width;
                    for (var p = (int*)data.Scan0; p < length; p++)
                        if (*p > black) *p = white;

                    bmp.UnlockBits(data);

                    return new Bitmap(bmp);
                }
                finally
                {
                    bmp.UnlockBits(data);;
                }
            }
        }
Posted
Updated 25-May-22 9:11am
v2
Comments
[no name] 25-May-22 11:05am    
I use WriteableBitmap and it's plenty fast.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.writeablebitmap?view=windowsdesktop-6.0

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