ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Change color of icon to another using C# and VB.Net in ASP.Net<p>Hi&nbsp;<a class="username" rel="RaulSoni">RaulSoni,</a></p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;gvOriginalIcons&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:ImageField DataImageUrlField=&#34;Value&#34; HeaderText=&#34;Icon&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;gvChangedIcons&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:ImageField DataImageUrlField=&#34;Value&#34; HeaderText=&#34;Changed Icon&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Change Color&#34; runat=&#34;server&#34; OnClick=&#34;ChangeColor&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Collections.Generic; using System.Drawing; using System.IO; using System.Web.UI.WebControls;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Collections.Generic Imports System.Drawing Imports System.IO Imports System.Web.UI.WebControls</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.gvOriginalIcons.DataSource = this.GetIcons(&#34;~/Icon&#34;); this.gvOriginalIcons.DataBind(); } } private List&lt;ListItem&gt; GetIcons(string path) { string[] icons = Directory.GetFiles(Server.MapPath(path)); List&lt;ListItem&gt; files = new List&lt;ListItem&gt;(); foreach (string filePath in icons) { string fileName = Path.GetFileName(filePath); files.Add(new ListItem(fileName, path + &#34;/&#34; + fileName)); } return files; } protected void ChangeColor(object sender, EventArgs e) { Bitmap bmp = null; string[] icons = Directory.GetFiles(Server.MapPath(&#34;~/Icon&#34;)); foreach (string icon in icons) { bmp = (Bitmap)System.Drawing.Image.FromFile(icon); bmp = this.ChangeColor(bmp, Color.Blue); bmp.Save(Server.MapPath(&#34;~/ChangedIcon/&#34;) + Path.GetFileName(icon)); } this.gvChangedIcons.DataSource = this.GetIcons(&#34;~/ChangedIcon&#34;); 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 &lt; bitmap.Width; i++) { for (int j = 0; j &lt; bitmap.Height; j++) { originalColor = bitmap.GetPixel(i, j); if (originalColor.A &gt; 150) { newBitmap.SetPixel(i, j, newColor); } else { newBitmap.SetPixel(i, j, originalColor); } } } return newBitmap; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;~/Icon&#34;) 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 &amp; &#34;/&#34; &amp; 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(&#34;~/Icon&#34;)) 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(&#34;~/ChangedIcon/&#34;) + Path.GetFileName(icon)) Next Me.gvChangedIcons.DataSource = Me.GetIcons(&#34;~/ChangedIcon&#34;) 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 &gt; 150 Then newBitmap.SetPixel(i, j, newColor) Else newBitmap.SetPixel(i, j, originalColor) End If Next Next Return newBitmap End Function</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/P92aCt9.gif" alt="" width="249" height="341" /></span></strong></p>https://www.aspforums.net:443/Threads/177319/Change-color-of-icon-to-another-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/177319/Change-color-of-icon-to-another-using-C-and-VBNet-in-ASPNet/Wed, 20 Feb 2019 01:03:43 GMT