Content: Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Background: Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Pattern: Blank Waves Notes Sharp Wood Rockface Leather Honey Vertical Triangles
Welcome to Xbox Chaos: Modding Evolved

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

OrangeMohawk

C#
Bitmap Colorization

5 posts in this topic

Hey guys, I just wanted to share with the programmers around here something I recently stumbled upon. It will take a Bitmap Source and a Color and set all the RGB values of the pixels in the Bitmap Source to the color specified, while leaving the Alpha alone. This is really helpful for giving color to a white PNG image on a transparent background. 
 
Examples:
Before Colorization:
c29nhNZ.png
 
After Colorization
tN8rRUF.png
 
Before:
WBMg2S8.png
 
After:
dsGYS1b.png
 
 
Code:

using System.IO;using System.Runtime.InteropServices;using System.Windows;using System.Windows.Media;using System.Windows.Media.Imaging; namespace AppName.Helpers{    public class BitmapHelper    {        public static BitmapImage Colorize(BitmapSource image, Color color)        {            var pixels = GetPixels(image);            for (var width = 0; width < pixels.GetLength(0); width++)            {                for (var height = 0; height < pixels.GetLength(1); height++)                {                    pixels[width, height].Red = color.R;                    pixels[width, height].Green = color.G;                    pixels[width, height].Blue = color.B;                }            }             var wb = new WriteableBitmap(image);            PutPixels(wb, pixels, 0, 0);             var bmImage = new BitmapImage();            using (var stream = new MemoryStream())            {                var encoder = new PngBitmapEncoder();                encoder.Frames.Add(BitmapFrame.Create(wb));                encoder.Save(stream);                bmImage.BeginInit();                bmImage.CacheOption = BitmapCacheOption.OnLoad;                bmImage.StreamSource = stream;                bmImage.EndInit();                bmImage.Freeze();            }            return bmImage;        }         [StructLayout(LayoutKind.Sequential)]        public struct PixelColor        {            public byte Blue;            public byte Green;            public byte Red;            public byte Alpha;        }         private static PixelColor[,] GetPixels(BitmapSource source)        {            if (source.Format != PixelFormats.Bgra32)                source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);             var width = source.PixelWidth;            var height = source.PixelHeight;            var result = new PixelColor[width, height];             source.CopyPixels(result, width * 4);            return result;        }         private static void PutPixels(WriteableBitmap bitmap, PixelColor[,] pixels, int x, int y)        {            var width = pixels.GetLength(0);            var height = pixels.GetLength(1);            bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 4, x, y);        }    }     public static class BitmapSourceHelper    {        public static void CopyPixels(this BitmapSource source, ColorizeBitmap.PixelColor[,] pixels, int stride)        {            var height = source.PixelHeight;            var width = source.PixelWidth;            var pixelBytes = new byte[height * width * 4];            source.CopyPixels(pixelBytes, stride, 0);            for (var x = 0; x < width; x++)                for (var y = 0; y < height; y++)                    pixels[x, y] = new ColorizeBitmap.PixelColor                    {                        Blue = pixelBytes[(x * width + y) * 4 + 0],                        Green = pixelBytes[(x * width + y) * 4 + 1],                        Red = pixelBytes[(x * width + y) * 4 + 2],                        Alpha = pixelBytes[(x * width + y) * 4 + 3],                    };        }    }}

Source:
I got the code info from here, and manipulated it until I got it working for me.
 
Edit: Here's something I quickly wrote for color overlay situations. It's not the best but might be useful for multi-tone images whose color you want to change:

        public static BitmapImage ColorizeOverlay(BitmapSource image, Color color)        {            var pixels = GetPixels(image);            for (var width = 0; width < pixels.GetLength(0); width++)            {                for (var height = 0; height < pixels.GetLength(1); height++)                {                    pixels[width, height].Red = (byte)((color.R + pixels[width, height].Red)/2);                    pixels[width, height].Green = (byte)((color.G + pixels[width, height].Green) / 2);                    pixels[width, height].Blue = (byte)((color.B + pixels[width, height].Blue) / 2);                }            }             var wb = new WriteableBitmap(image);            PutPixels(wb, pixels, 0, 0);             var bmImage = new BitmapImage();            using (var stream = new MemoryStream())            {                var encoder = new PngBitmapEncoder();                encoder.Frames.Add(BitmapFrame.Create(wb));                encoder.Save(stream);                bmImage.BeginInit();                bmImage.CacheOption = BitmapCacheOption.OnLoad;                bmImage.StreamSource = stream;                bmImage.EndInit();                bmImage.Freeze();            }            return bmImage;        }
Edited by OrangeMohawk
7ime, Zerker24 and Stu Pickles like this

Share this post


Link to post
Share on other sites

I have written something similar in the past. Great job, This could be useful to someone that wants their icons color to change based on a theme.

That's exactly what I needed it for.

Share this post


Link to post
Share on other sites

That's exactly what I needed it for.

Me too, I managed to find mine in case someone needs a winforms version. It's actually almost the same.

 

        public Bitmap OverlayImage(Bitmap original, Color color)        {            Bitmap source = new Bitmap(original);            Bitmap rtnImage = new Bitmap(source.Width, source.Height);            Graphics graphics = Graphics.FromImage(rtnImage);            for (int x = 0; x < source.Width; x++)            {                for (int y = 0; y < source.Height; y++)                {                    Color originalColor = source.GetPixel(x, y);                    if (originalColor.A > 0)                    {                        int r = (byte)((color.R + originalColor.R) / 2);                        int g = (byte)((color.G + originalColor.G) / 2);                        int b = (byte)((color.B + originalColor. / 2);                        Color fillColor = Color.FromArgb(originalColor.A, r, g, ;                        graphics.FillRectangle(new SolidBrush(fillColor), x, y, 1, 1);                    }                }            }            return rtnImage;        }
Edited by Zerker24
OrangeMohawk likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now