Color Filtering in ASP.Net

Introduction: This article shows how to filter an image depending on color. An optical element that partially absorbs incident radiation is often called an absorption filter. The absorption is selective with respect to wavelength, or color, limiting the colors that are transmitted by limiting those that are absorbed. Color filters absorb all the colors not transmitted. They are used in photography, optical instruments and illuminating devices to control the amount and spectral composition of the light. 
 
This is very easy, just add a value to or subtract a value from each color. The most useful thing to do with this filter is to set two colors to -255 in order to strip them and see just one color component of the image. For example, for a red filter, keep the red component as it is and just subtract 255 from the green component and blue component.
 
Use the following procedure to see how color filtering can be done in ASP.NET.
 
Step 1: Open Visual Studio.
Step 2: Create a new website.
Step 3: Go to the Solution Explorer then right-click on the project then seelct "Add" > "New Folder".
Step 4: Give the name of the folder "Images".
Step 5: Go to the Solution Explorer and right-click on the project then seelct "Add" > "New Item..." (or press "Ctrl + Shift + A").
Step 6: Now add a new Web Form, give a name for the Web Form, like "ColorFiltering.aspx". After adding the web form you will see the following code.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ColorFiltering.aspx.cs" Inherits="ColorFiltering" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.         </div>  
  13.     </form>  
  14. </body>  
  15. </html>  
Step 7: Now quickly add one fileupload control, four buttons (Upload Button, Red-Filter-Button, Green-Filter-Button, Blue-Filter-Button) and one Image control. After adding it your aspx page code will become:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ColorFiltering.aspx.cs" Inherits="ColorFiltering" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             <asp:FileUpload ID="FileUpload1" runat="server" />  
  13.                 
  14.             <asp:Button ID="Button1" runat="server" Text="Upload" />  
  15.                
  16.             <asp:Button ID="RedFilter" runat="server" Text="Red Filter" />  
  17.                
  18.             <asp:Button ID="GreenFilter" runat="server" Text="Green Filter" />  
  19.                
  20.             <asp:Button ID="BlueFilter" runat="server" Text="Blue Filter" />  
  21.                
  22.             <br /><br />  
  23.             <asp:Image ID="Img" runat="server" />  
  24.             <br />  
  25.         </div>  
  26.     </form>  
  27. </body>  
  28. </html>  
Step 8: Now add the "System.Drawing" namespace in the code behind of this page or at the "ColorFiltering.aspx.cs" page.
 
Step 9: Now in the Upload Button's click event write the following code:
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     string filePath = Server.MapPath("Images/" + FileUpload1.FileName);  
  4.     FileUpload1.SaveAs(filePath);  
  5.     Img.ImageUrl = "Images/" + FileUpload1.FileName;  
  6.     Session["filepath"] = "Images/" + FileUpload1.FileName;  
  7. }  
Step 10: Now in the Red Filter Button's click event write the following code:
  1. protected void RedFilter_Click(object sender, EventArgs e)  
  2. {  
  3.     Bitmap temp = (Bitmap)_current;  
  4.     Bitmap bmap = (Bitmap)temp.Clone();  
  5.     Color c;  
  6.     for (int i = 0; i < bmap.Width; i++)  
  7.     {  
  8.         for (int j = 0; j < bmap.Height; j++)  
  9.         {  
  10.             c = bmap.GetPixel(i, j);  
  11.             PixelR = c.R;  
  12.             PixelG = c.G - 255;  
  13.             PixelB = c.B - 255;  
  14.   
  15.             PixelR = Math.Max(PixelR, 0);  
  16.             PixelR = Math.Min(255, PixelR);  
  17.   
  18.             PixelG = Math.Max(PixelG, 0);  
  19.             PixelG = Math.Min(255, PixelG);  
  20.   
  21.             PixelB = Math.Max(PixelB, 0);  
  22.             PixelB = Math.Min(255, PixelB);  
  23.             bmap.SetPixel(i, j, Color.FromArgb((byte)PixelR, (byte)PixelG, (byte)PixelB));  
  24.         }  
  25.     }  
  26.     _current = (Bitmap)bmap.Clone();  
  27.     Random rnd = new Random();  
  28.     int a=rnd.Next();  
  29.     _current.Save(Server.MapPath("Images/"+a+".png"));  
  30.     Img.ImageUrl = "Images/" + a + ".png";  
  31. }  
Step 11: Now in the Green Filter Button's click event write the following code:
  1. protected void GreenFilter_Click(object sender, EventArgs e)  
  2. {  
  3.     Bitmap temp = (Bitmap)_current;  
  4.     Bitmap bmap = (Bitmap)temp.Clone();  
  5.     Color c;  
  6.     for (int i = 0; i < bmap.Width; i++)  
  7.     {  
  8.         for (int j = 0; j < bmap.Height; j++)  
  9.         {  
  10.             c = bmap.GetPixel(i, j);  
  11.             PixelR = c.R - 255;  
  12.             PixelG = c.G;  
  13.             PixelB = c.B - 255;  
  14.   
  15.             PixelR = Math.Max(PixelR, 0);  
  16.             PixelR = Math.Min(255, PixelR);  
  17.   
  18.             PixelG = Math.Max(PixelG, 0);  
  19.             PixelG = Math.Min(255, PixelG);  
  20.   
  21.             PixelB = Math.Max(PixelB, 0);  
  22.             PixelB = Math.Min(255, PixelB);  
  23.             bmap.SetPixel(i, j, Color.FromArgb((byte)PixelR, (byte)PixelG, (byte)PixelB));  
  24.         }  
  25.     }  
  26.     _current = (Bitmap)bmap.Clone();  
  27.     Random rnd = new Random();  
  28.     int a = rnd.Next();  
  29.     _current.Save(Server.MapPath("Images/" + a + ".png"));  
  30.     Img.ImageUrl = "Images/" + a + ".png";  
  31. }  
Step 12: Now in the Blue Filter Button's click event write the following code: 
  1. protected void BlueFilter_Click(object sender, EventArgs e)  
  2. {  
  3.     Bitmap temp = (Bitmap)_current;  
  4.     Bitmap bmap = (Bitmap)temp.Clone();  
  5.     Color c;  
  6.     for (int i = 0; i < bmap.Width; i++)  
  7.     {  
  8.         for (int j = 0; j < bmap.Height; j++)  
  9.         {  
  10.             c = bmap.GetPixel(i, j);  
  11.             PixelR = c.R - 255;  
  12.             PixelG = c.G - 255;  
  13.             PixelB = c.B;  
  14.   
  15.             PixelR = Math.Max(PixelR, 0);  
  16.             PixelR = Math.Min(255, PixelR);  
  17.   
  18.             PixelG = Math.Max(PixelG, 0);  
  19.             PixelG = Math.Min(255, PixelG);  
  20.   
  21.             PixelB = Math.Max(PixelB, 0);  
  22.             PixelB = Math.Min(255, PixelB);  
  23.             bmap.SetPixel(i, j, Color.FromArgb((byte)PixelR, (byte)PixelG, (byte)PixelB));  
  24.         }  
  25.     }  
  26.     _current = (Bitmap)bmap.Clone();  
  27.     Random rnd = new Random();  
  28.     int a = rnd.Next();  
  29.     _current.Save(Server.MapPath("Images/" + a + ".png"));  
  30.     Img.ImageUrl = "Images/" + a + ".png";  
  31. }  
Step 13: On the page load event write the following code:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (Session["filepath"] != null)  
  4.     {  
  5.         Img.ImageUrl = Session["filepath"].ToString();  
  6.         _current = (Bitmap)Bitmap.FromFile(Server.MapPath(Session["filepath"].ToString()));  
  7.     }  
  8. }  
Complete Code
 
ColorFiltering.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ColorFiltering.aspx.cs" Inherits="ColorFiltering" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             <asp:FileUpload ID="FileUpload1" runat="server" />  
  13.                 
  14.             <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />  
  15.                
  16.             <asp:Button ID="RedFilter" runat="server" Text="Red Filter" OnClick="RedFilter_Click" />  
  17.                
  18.             <asp:Button ID="GreenFilter" runat="server" Text="Green Filter" OnClick="GreenFilter_Click"/>  
  19.                
  20.             <asp:Button ID="BlueFilter" runat="server" Text="Blue Filter" OnClick="BlueFilter_Click"/>  
  21.                
  22.             <br /><br />  
  23.             <asp:Image ID="Img" runat="server" />  
  24.             <br />  
  25.         </div>  
  26.     </form>  
  27. </body>  
  28. </html>  
ColorFiltering.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8.   
  9. public partial class ColorFiltering : System.Web.UI.Page  
  10. {  
  11.     Bitmap _current;  
  12.     private int PixelR = 0;  
  13.     private int PixelG = 0;  
  14.     private int PixelB = 0;  
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.         if (Session["filepath"] != null)  
  18.         {  
  19.             Img.ImageUrl = Session["filepath"].ToString();  
  20.             _current = (Bitmap)Bitmap.FromFile(Server.MapPath(Session["filepath"].ToString()));  
  21.         }  
  22.     }  
  23.     protected void Button1_Click(object sender, EventArgs e)  
  24.     {  
  25.         string filePath = Server.MapPath("Images/" + FileUpload1.FileName);  
  26.         FileUpload1.SaveAs(filePath);  
  27.         Img.ImageUrl = "Images/" + FileUpload1.FileName;  
  28.         Session["filepath"] = "Images/" + FileUpload1.FileName;  
  29.     }  
  30.     protected void RedFilter_Click(object sender, EventArgs e)  
  31.     {  
  32.         Bitmap temp = (Bitmap)_current;  
  33.         Bitmap bmap = (Bitmap)temp.Clone();  
  34.         Color c;  
  35.         for (int i = 0; i < bmap.Width; i++)  
  36.         {  
  37.             for (int j = 0; j < bmap.Height; j++)  
  38.             {  
  39.                 c = bmap.GetPixel(i, j);  
  40.                 PixelR = c.R;  
  41.                 PixelG = c.G - 255;  
  42.                 PixelB = c.B - 255;  
  43.   
  44.                 PixelR = Math.Max(PixelR, 0);  
  45.                 PixelR = Math.Min(255, PixelR);  
  46.   
  47.                 PixelG = Math.Max(PixelG, 0);  
  48.                 PixelG = Math.Min(255, PixelG);  
  49.   
  50.                 PixelB = Math.Max(PixelB, 0);  
  51.                 PixelB = Math.Min(255, PixelB);  
  52.                 bmap.SetPixel(i, j, Color.FromArgb((byte)PixelR, (byte)PixelG, (byte)PixelB));  
  53.             }  
  54.         }  
  55.         _current = (Bitmap)bmap.Clone();  
  56.         Random rnd = new Random();  
  57.         int a=rnd.Next();  
  58.         _current.Save(Server.MapPath("Images/"+a+".png"));  
  59.         Img.ImageUrl = "Images/" + a + ".png";  
  60.     }  
  61.     protected void GreenFilter_Click(object sender, EventArgs e)  
  62.     {  
  63.         Bitmap temp = (Bitmap)_current;  
  64.         Bitmap bmap = (Bitmap)temp.Clone();  
  65.         Color c;  
  66.         for (int i = 0; i < bmap.Width; i++)  
  67.         {  
  68.             for (int j = 0; j < bmap.Height; j++)  
  69.             {  
  70.                 c = bmap.GetPixel(i, j);  
  71.                 PixelR = c.R - 255;  
  72.                 PixelG = c.G;  
  73.                 PixelB = c.B - 255;  
  74.   
  75.                 PixelR = Math.Max(PixelR, 0);  
  76.                 PixelR = Math.Min(255, PixelR);  
  77.   
  78.                 PixelG = Math.Max(PixelG, 0);  
  79.                 PixelG = Math.Min(255, PixelG);  
  80.   
  81.                 PixelB = Math.Max(PixelB, 0);  
  82.                 PixelB = Math.Min(255, PixelB);  
  83.                 bmap.SetPixel(i, j, Color.FromArgb((byte)PixelR, (byte)PixelG, (byte)PixelB));  
  84.             }  
  85.         }  
  86.         _current = (Bitmap)bmap.Clone();  
  87.         Random rnd = new Random();  
  88.         int a = rnd.Next();  
  89.         _current.Save(Server.MapPath("Images/" + a + ".png"));  
  90.         Img.ImageUrl = "Images/" + a + ".png";  
  91.     }  
  92.   
  93.     protected void BlueFilter_Click(object sender, EventArgs e)  
  94.     {  
  95.         Bitmap temp = (Bitmap)_current;  
  96.         Bitmap bmap = (Bitmap)temp.Clone();  
  97.         Color c;  
  98.         for (int i = 0; i < bmap.Width; i++)  
  99.         {  
  100.             for (int j = 0; j < bmap.Height; j++)  
  101.             {  
  102.                 c = bmap.GetPixel(i, j);  
  103.                 PixelR = c.R - 255;  
  104.                 PixelG = c.G - 255;  
  105.                 PixelB = c.B;  
  106.   
  107.                 PixelR = Math.Max(PixelR, 0);  
  108.                 PixelR = Math.Min(255, PixelR);  
  109.   
  110.                 PixelG = Math.Max(PixelG, 0);  
  111.                 PixelG = Math.Min(255, PixelG);  
  112.   
  113.                 PixelB = Math.Max(PixelB, 0);  
  114.                 PixelB = Math.Min(255, PixelB);  
  115.                 bmap.SetPixel(i, j, Color.FromArgb((byte)PixelR, (byte)PixelG, (byte)PixelB));  
  116.             }  
  117.         }  
  118.         _current = (Bitmap)bmap.Clone();  
  119.         Random rnd = new Random();  
  120.         int a = rnd.Next();  
  121.         _current.Save(Server.MapPath("Images/" + a + ".png"));  
  122.         Img.ImageUrl = "Images/" + a + ".png";  
  123.     }  
  124. }  
Step 14: Now run the project and you will see the following outputs:
  1. Initially when the project is run:


  2. When the Choose File or File Upload Control is clicked:



  3. After selecting the File Click on Upload Button:



  4. When the Red Filter Button is clicked:


  5. When the Green Filter Button is clicked:


  6. When the Blue Filter Button is clicked:



Similar Articles