Search This Blog

Title of the confirm dialog

Description of what is about to happen

Bitmap Color Replacement

Replace specific bitmap pixels color with specific intensity(0~100%) and/or limited alpha  into new defined color. First get bitmap pixels by width and height by an iteration then do process of pixel color replacement.


List of functions

○ Replace Color.
○ Replace Non Transparent Color.
○ Replace Non Transparent ColorA.
○ Set Replace All Color Bitmap (2 overloads).
○ Set Replace Color Bitmap(2 overloads).

Class Lerp is needed and can be viewed here


Source Code


  1 
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace TechScales
{
    internal static class BitmapExtensions
    {
        /// <summary>
        /// Replace color with limited alpha and specific intensity
        /// </summary>
        /// <param name="source"></param>
        /// <param name="to"></param>
        /// <param name="minAlphaReplace"></param>
        /// <param name="intensity"></param>
        /// <returns></returns>
        internal static Bitmap SetReplaceAllColorBitmap(
            this Bitmap source, Color to, byte minAlphaReplace = 0, float intensity = .25f)
        {
            if (source == null) { return null; }
            if (intensity < 0.1f && intensity > 1f) { intensity = 1f; }
            Bitmap bm = new Bitmap(source);
            for (int w = 0; w < source.Width - 1; w++)
            {
                for (int h = 0; h < source.Height - 1; h++)
                {
                    Color c = bm.GetPixel(w, h);
                    if (c.A > minAlphaReplace)
                    {
                        c = Color.FromArgb(c.A, intensity == 1f ? to : c.Lerp(to, intensity));
                        bm.SetPixel(w, h, c);
                    }
                }
            }
            return bm;
        }
        /// <summary>
        /// Replace color with specific intensity
        /// </summary>
        /// <param name="source"></param>
        /// <param name="to"></param>
        /// <param name="minAlphaReplace"></param>
        /// <param name="intensity"></param>
        /// <returns></returns>
        internal static Bitmap SetReplaceAllColorBitmap(
            this Bitmap source, Color to, float intensity)
        {
            return SetReplaceAllColorBitmap(source, to, 0, intensity);
        }
        /// <summary>
        /// Replace all color into new color with specific intensity
        /// </summary>
        /// <param name="source">gambar</param>
        /// <param name="col">warna baru</param>
        /// <param name="intensity"></param>
        /// <returns></returns>
        internal static Bitmap SetReplaceColorBitmap(
            this Bitmap source, Color col, float intensity = 1f)
        {
            if (source == null) { return null; }
            if (intensity < 0.1f && intensity > 1f) { intensity = 1f; }
            Bitmap bm = new Bitmap(source);
            for (int w = 0; w < source.Width - 1; w++)
            {
                for (int h = 0; h < source.Height - 1; h++)
                {
                    Color c = bm.GetPixel(w, h);
                    if (c.A > 0)
                    {
                        c = Color.FromArgb(c.A, intensity == 1f ? col : c.Lerp(col, intensity)); 
                        bm.SetPixel(w, h, c);
                    }
                }
            }
            return bm;
        }
        /// <summary>
        /// Replace specific color into new color with specific intensity
        /// </summary>
        /// <param name="source">gambar</param>
        /// <param name="from">warna diganti</param>
        /// <param name="to">watrna pengganti</param>
        /// <param name="intensity">intensitas</param>
        /// <returns></returns>
        internal static Bitmap SetReplaceColorBitmap(
            this Bitmap source, Color @from, Color to, float intensity = 1f)
        {
            if (source == null) { return null; }
            if (from.Equals(to)) { return source;                  }
            if (intensity < 0.1f && intensity > 1f) { intensity = 1f; }
            Bitmap bm = new Bitmap(source);
            for (int w = 0; w < source.Width - 1; w++)
            {
                for (int h = 0; h < source.Height - 1; h++)
                {
                    Color c = bm.GetPixel(w, h);
                    if (c.R == from.R && c.G == from.B && c.B == from.B)
                    {
                        c = Color.FromArgb(c.A, intensity == 1f ? to : c.Lerp(to, intensity));
                        bm.SetPixel(w, h, c);
                    }
                }
            }
            return bm;
        }
        private static unsafe Bitmap ReplaceNonTransparentColorA(
            Bitmap source,
            Color replacement,
            byte alphaLimitMinim = 5)
        {
            const int PIXEL_SIZE = 4;                   // 32 bits per pixel
            var target = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb);
            BitmapData sourceData = null, targetData = null;

            try
            {
                sourceData = source.LockBits(
                  new Rectangle(0, 0, source.Width, source.Height),
                  ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                targetData = target.LockBits(
                  new Rectangle(0, 0, target.Width, target.Height),
                  ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                for (int y = 0; y < source.Height; ++y)
                {
                    byte* sourceRow = (byte*)sourceData.Scan0 + (y * sourceData.Stride);
                    byte* targetRow = (byte*)targetData.Scan0 + (y * targetData.Stride);

                    for (int x = 0; x < source.Width; ++x)
                    {
                        byte b = sourceRow[x * PIXEL_SIZE + 0];
                        byte g = sourceRow[x * PIXEL_SIZE + 1];
                        byte r = sourceRow[x * PIXEL_SIZE + 2];
                        byte a = sourceRow[x * PIXEL_SIZE + 3];

                        r = replacement.R;
                        g = replacement.G;
                        b = replacement.B;

                        targetRow[x * PIXEL_SIZE + 0] = b;
                        targetRow[x * PIXEL_SIZE + 1] = g;
                        targetRow[x * PIXEL_SIZE + 2] = r;
                        targetRow[x * PIXEL_SIZE + 3] = a;
                    }
                }
            }
            finally
            {
                if (sourceData != null) { source.UnlockBits(sourceData); }
                if (targetData != null) { target.UnlockBits(targetData); }
            }
            return target;
        }
        internal static Bitmap ReplaceNonTransparentColor(this 
            Bitmap source,
            Color replacement,
            byte alphaLimitMinim = 5)
        {
            return ReplaceNonTransparentColorA(source, replacement, alphaLimitMinim);
        }
        internal static Bitmap ReplaceColor(
            this Bitmap source, Color @from, Color to, float intensity = 1f)
        {
            return SetReplaceColorBitmap(source, from, to, intensity);
        }
    }
}

Test

Result
1.   2.   3.   4.   5.   6.
Code
1. Bitmap bm = original bitmap
2. bm.ReplaceColor(Color.Black, Color.DodgerBlue, 0.85f);
3. bm.ReplaceNonTransparentColor(Color.Green, 5);
4. bm.SetReplaceAllColorBitmap(Color.DodgerBlue, 0, 0.25f);
5. bm.SetReplaceAllColorBitmap(Color.Yellow, 0.65f);
6. bm.SetReplaceAllColorBitmap(Color.Black, Color.Red, 0.85f);

TOP