Change color of icon to another using C# and VB.Net in ASP.Net

Last Reply 7 months ago By dharmendr

Posted 7 months ago

Possibly this is not the forum in which I should ask this question but I do not know if you can help me

I have several ICONS like the one I show you in these images url

All the drawings of the icon are in BLACK color and the background is WHITE

What I want to do is change the color of the outline or drawing of the BLACK icon to another color for example BLUE or GREEN or even WHITE as the background.

How could I do ???

You are viewing reply posted by: dharmendr 7 months ago.
Posted 7 months ago

Hi RaulSoni,

Check this example. Now please take its reference and correct your code.

HTML

<table>
    <tr>
        <td>
            <asp:GridView runat="server" ID="gvOriginalIcons" AutoGenerateColumns="false">
                <Columns>
                    <asp:ImageField DataImageUrlField="Value" HeaderText="Icon" />
                </Columns>
            </asp:GridView>
        </td>
        <td>
            <asp:GridView runat="server" ID="gvChangedIcons" AutoGenerateColumns="false">
                <Columns>
                    <asp:ImageField DataImageUrlField="Value" HeaderText="Changed Icon" />
                </Columns>
            </asp:GridView>
        </td>
    </tr>
</table>
<br />
<asp:Button Text="Change Color" runat="server" OnClick="ChangeColor" />

Namespaces

C#

using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Web.UI.WebControls;

VB.Net

Imports System.Collections.Generic
Imports System.Drawing
Imports System.IO
Imports System.Web.UI.WebControls

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.gvOriginalIcons.DataSource = this.GetIcons("~/Icon");
        this.gvOriginalIcons.DataBind();
    }
}

private List<ListItem> GetIcons(string path)
{
    string[] icons = Directory.GetFiles(Server.MapPath(path));
    List<ListItem> files = new List<ListItem>();
    foreach (string filePath in icons)
    {
        string fileName = Path.GetFileName(filePath);
        files.Add(new ListItem(fileName, path + "/" + fileName));
    }
    return files;
}

protected void ChangeColor(object sender, EventArgs e)
{
    Bitmap bmp = null;
    string[] icons = Directory.GetFiles(Server.MapPath("~/Icon"));
    foreach (string icon in icons)
    {
        bmp = (Bitmap)System.Drawing.Image.FromFile(icon);
        bmp = this.ChangeColor(bmp, Color.Blue);
        bmp.Save(Server.MapPath("~/ChangedIcon/") + Path.GetFileName(icon));
    }

    this.gvChangedIcons.DataSource = this.GetIcons("~/ChangedIcon");
    this.gvChangedIcons.DataBind();
}

public Bitmap ChangeColor(Bitmap bitmap, Color newColor)
{
    Color originalColor;
    Bitmap newBitmap = new Bitmap(bitmap.Width, bitmap.Height);
    for (int i = 0; i < bitmap.Width; i++)
    {
        for (int j = 0; j < bitmap.Height; j++)
        {
            originalColor = bitmap.GetPixel(i, j);
            if (originalColor.A > 150)
            {
                newBitmap.SetPixel(i, j, newColor);
            }
            else
            {
                newBitmap.SetPixel(i, j, originalColor);
            }
        }
    }

    return newBitmap;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.gvOriginalIcons.DataSource = Me.GetIcons("~/Icon")
        Me.gvOriginalIcons.DataBind()
    End If
End Sub

Private Function GetIcons(ByVal path As String) As List(Of ListItem)
    Dim icons As String() = Directory.GetFiles(Server.MapPath(path))
    Dim files As List(Of ListItem) = New List(Of ListItem)()

    For Each filePath As String In icons
        Dim fileName As String = System.IO.Path.GetFileName(filePath)
        files.Add(New ListItem(fileName, path & "/" & fileName))
    Next

    Return files
End Function

Protected Sub ChangeColor(ByVal sender As Object, ByVal e As EventArgs)
    Dim bmp As Bitmap = Nothing
    Dim icons As String() = Directory.GetFiles(Server.MapPath("~/Icon"))
    For Each icon As String In icons
        bmp = CType(System.Drawing.Image.FromFile(icon), Bitmap)
        bmp = Me.ChangeColor(bmp, Color.Blue)
        bmp.Save(Server.MapPath("~/ChangedIcon/") + Path.GetFileName(icon))
    Next

    Me.gvChangedIcons.DataSource = Me.GetIcons("~/ChangedIcon")
    Me.gvChangedIcons.DataBind()
End Sub

Public Function ChangeColor(ByVal bitmap As Bitmap, ByVal newColor As Color) As Bitmap
    Dim originalColor As Color
    Dim newBitmap As Bitmap = New Bitmap(bitmap.Width, bitmap.Height)
    For i As Integer = 0 To bitmap.Width - 1
        For j As Integer = 0 To bitmap.Height - 1
            originalColor = bitmap.GetPixel(i, j)
            If originalColor.A > 150 Then
                newBitmap.SetPixel(i, j, newColor)
            Else
                newBitmap.SetPixel(i, j, originalColor)
            End If
        Next
    Next

    Return newBitmap
End Function

Screenshot