Welcome Guest! To enable all features, please Login or Register.



Last Go to last post Unread Go to first unread post
#1 Posted : Wednesday, March 8, 2017 8:08:15 AM(UTC)


Groups: Registered
Posts: 1

I have a Black & White image (BitsPerPixel = 1).
How can I change Black to Red?

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Friday, March 10, 2017 3:02:12 PM(UTC)

Joe Z  
Joe Z

Groups: Registered, Tech Support, Administrators
Posts: 63

Thanks: 2 times
Was thanked: 4 time(s) in 4 post(s)


To change your 1 BPP Black & White image to a Red & White image, you can use the RasterImage.GetPalette and RasterImage.SetPalette methods.

You first extract the current images palette and set that equal to a RasterColor array. This array now holds every unique color contained within the image. Since we are working with a 1 BPP image, the size of the array would be 2. Array index zero refers to the color white while array index one refers to the color black. Next we change the value at index one to RasterKnownColor.Red. This makes all pixels that were previously referring to the color black; now refer to the color red. Finally, we use the RasterColor array to set a new palette onto the image and save the result out.

I’ve included a snippet of code below so you can get a better understanding of this process.

            using (RasterCodecs codecs = new RasterCodecs())
                using (RasterImage image = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\OCR1.TIF"))
                    RasterColor[] pal = image.GetPalette();
                    int idx = 1; // if palette entry 1 is the black entry that you want to change
                    pal[idx] = RasterColor.FromKnownColor(RasterKnownColor.Red);
                    image.SetPalette(pal, 0, pal.Length);

                    codecs.Save(image, @"C:\Users\Desktop\result.png", RasterImageFormat.Png, 1);

Here are the before and after results from this process:

Edited by moderator Thursday, January 7, 2021 2:26:43 PM(UTC)  | Reason: Not specified

Joe Zhan
Developer Support Engineer
LEAD Technologies, Inc.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.047 seconds.