Control Image Contrast Using ASP.NET

Introduction: Contrast is the difference in the distinguishable colors of an object (or its representation in an image or display). In the visual perception of the real world, contrast is determined by the difference in the color and brightness of the object and other objects within the same field of view.
 
Contrast is also the difference between the color or shading of the printed material on a document and the background on which it is printed.
 
Brightness can be in the input range -100 to +100
 
Use the following procedure to create a simple sample application for controlling Contrast.
 
Step 1: First open the Visual Studio.
Step 2: In the menu bar click on "File" > "New Web Site" or press "Shift + Alt + N".
Step 3: Now a dialog box will be shown; select "ASP.NET Empty Web Site" and provide the website name and press "OK".
Step 4: Now open Solution Explorer and right-click on the project and click on "Add" > "Add New Item" or simply press "Ctrl + Shift + A".
Step 5: A dialog will be opened. Select "Web Form" and provide the name of the Web Form (like: Contrast.aspx) and click on the "Add" button. 
 
Note:: Steps 1 to 5 are similar to my previous article: Control Image Brightness Using ASP.NET 
 
Step 6:  Now a Web Form will be front of you; provide the following code for it:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contrast.aspx.cs" Inherits="Contrast" %>   
  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.     
  13.         </div>    
  14.     </form>    
  15. </body>    
  16. </html>
Step 7: Now drag and drop the following controls from the tool box:
  1. One File Upload control
  2. One Button control For upload
  3. One TextBox control to provide the value for contrast
  4. One Button control to set the contrast
After doing all this my aspx page code is:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contrast.aspx.cs" Inherits="Contrast" %>    
  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.             Upload an Image:<asp:FileUpload ID="FileUpload1" runat="server" />    
  13.              <asp:Button ID="btnUpload" runat="server" Text="Upload" />    
  14.             <br />    
  15.             Give The Value Of For the Contrast: <asp:TextBox ID="tbContrast" runat="server" Width="53px"></asp:TextBox>    
  16.              <asp:Button ID="btnContrast" runat="server" Text="Set Contrast" />    
  17.             <br /><br />    
  18.             <asp:Image ID="Img" runat="server" />    
  19.         </div>    
  20.     </form>    
  21. </body>    
  22. </html>    
Step 8: Now open the code behind of this Web Form:
  1. Add the "System.Drawing" namespace.
  2. Use a global variable, Bitmap _current.
  3. Write the following code in the "page_load" event:
    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. }  
  4. Write the following code in the "Upload" Button's click event:
    1. protected void btnUpload_Click(object sender, EventArgs e)  
    2. {  
    3.     if (FileUpload1.HasFile)  
    4.     {  
    5.         string filePath = Server.MapPath("Images/" + FileUpload1.FileName);  
    6.         FileUpload1.SaveAs(filePath);  
    7.         Img.ImageUrl = "Images/" + FileUpload1.FileName;  
    8.         Session["filepath"] = "Images/" + FileUpload1.FileName;  
    9.     }  
    10.     else  
    11.     {  
    12.         Response.Write("Please Select The File");  
    13.     }  
    14. }  
  5. Write the following code in the "Set Contrast" Button's click event:
    1. protected void btnContrast_Click(object sender, EventArgs e)  
    2. {  
    3.     if (tbContrast.Text != null && Session["filepath"] != null)  
    4.     {  
    5.         double contrast = int.Parse(tbContrast.Text);  
    6.         Bitmap temp = (Bitmap)_current;  
    7.         Bitmap bmap = (Bitmap)temp.Clone();  
    8.         if (contrast < -100) contrast = -100;  
    9.         if (contrast > 100) contrast = 100;  
    10.         contrast = (100.0 + contrast) / 100.0;  
    11.         contrast *= contrast;  
    12.         Color col;  
    13.         for (int i = 0; i < bmap.Width; i++)  
    14.         {  
    15.             for (int j = 0; j < bmap.Height; j++)  
    16.             {  
    17.                 col = bmap.GetPixel(i, j);  
    18.                 double pRed = col.R / 255.0;  
    19.                 pRed -= 0.5;  
    20.                 pRed *= contrast;  
    21.                 pRed += 0.5;  
    22.                 pRed *= 255;  
    23.                 if (pRed < 0) pRed = 0;  
    24.                 if (pRed > 255) pRed = 255;  
    25.   
    26.                 double pGreen = col.G / 255.0;  
    27.                 pGreen -= 0.5;  
    28.                 pGreen *= contrast;  
    29.                 pGreen += 0.5;  
    30.                 pGreen *= 255;  
    31.                 if (pGreen < 0) pGreen = 0;  
    32.                 if (pGreen > 255) pGreen = 255;  
    33.   
    34.                 double pBlue = col.B / 255.0;  
    35.                 pBlue -= 0.5;  
    36.                 pBlue *= contrast;  
    37.                 pBlue += 0.5;  
    38.                 pBlue *= 255;  
    39.                 if (pBlue < 0) pBlue = 0;  
    40.                 if (pBlue > 255) pBlue = 255;  
    41.   
    42.                 bmap.SetPixel(i, j,  
    43.         Color.FromArgb((byte)pRed, (byte)pGreen, (byte)pBlue));  
    44.             }  
    45.         }  
    46.         _current = (Bitmap)bmap.Clone();  
    47.         Random rnd = new Random();  
    48.         int a = rnd.Next();  
    49.         _current.Save(Server.MapPath("Images/" + a + ".png"));  
    50.         Img.ImageUrl = "Images/" + a + ".png";  
    51.     }  
    52. }   
Step 9: Run the project and you will get the following output: 
  1. When project is initially run:



  2. After uploading an image:



  3. After giving Contrast=25:


The following is the complete code:
  1. Contrast.aspx:
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Contrast.aspx.cs" Inherits="Contrast" %>  
    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.             Upload an Image:<asp:FileUpload ID="FileUpload1" runat="server" />  
    13.              <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />  
    14.             <br />  
    15.             Give The Value Of For the Contrast: <asp:TextBox ID="tbContrast" runat="server" Width="53px"></asp:TextBox>  
    16.              <asp:Button ID="btnContrast" runat="server" Text="Set Contrast" OnClick="btnContrast_Click" />  
    17.             <br /><br />  
    18.             <asp:Image ID="Img" runat="server" />  
    19.         </div>  
    20.     </form>  
    21. </body>  
    22. </html>
  2. Contrast.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 Contrast : System.Web.UI.Page  
    10. {  
    11.     Bitmap _current;  
    12.     protected void Page_Load(object sender, EventArgs e)  
    13.     {  
    14.         if (Session["filepath"] != null)  
    15.         {  
    16.             Img.ImageUrl = Session["filepath"].ToString();  
    17.             _current = (Bitmap)Bitmap.FromFile(Server.MapPath(Session["filepath"].ToString()));  
    18.         }  
    19.     }  
    20.     protected void btnUpload_Click(object sender, EventArgs e)  
    21.     {  
    22.         if (FileUpload1.HasFile)  
    23.         {  
    24.             string filePath = Server.MapPath("Images/" + FileUpload1.FileName);  
    25.             FileUpload1.SaveAs(filePath);  
    26.             Img.ImageUrl = "Images/" + FileUpload1.FileName;  
    27.             Session["filepath"] = "Images/" + FileUpload1.FileName;  
    28.         }  
    29.         else  
    30.         {  
    31.             Response.Write("Please Select The File");  
    32.         }  
    33.     }  
    34.   
    35.     protected void btnContrast_Click(object sender, EventArgs e)  
    36.     {  
    37.         if (tbContrast.Text != null && Session["filepath"] != null)  
    38.         {  
    39.             double contrast = int.Parse(tbContrast.Text);  
    40.             Bitmap temp = (Bitmap)_current;  
    41.             Bitmap bmap = (Bitmap)temp.Clone();  
    42.             if (contrast < -100) contrast = -100;  
    43.             if (contrast > 100) contrast = 100;  
    44.             contrast = (100.0 + contrast) / 100.0;  
    45.             contrast *= contrast;  
    46.             Color col;  
    47.             for (int i = 0; i < bmap.Width; i++)  
    48.             {  
    49.                 for (int j = 0; j < bmap.Height; j++)  
    50.                 {  
    51.                     col = bmap.GetPixel(i, j);  
    52.                     double pRed = col.R / 255.0;  
    53.                     pRed -= 0.5;  
    54.                     pRed *= contrast;  
    55.                     pRed += 0.5;  
    56.                     pRed *= 255;  
    57.                     if (pRed < 0) pRed = 0;  
    58.                     if (pRed > 255) pRed = 255;  
    59.   
    60.                     double pGreen = col.G / 255.0;  
    61.                     pGreen -= 0.5;  
    62.                     pGreen *= contrast;  
    63.                     pGreen += 0.5;  
    64.                     pGreen *= 255;  
    65.                     if (pGreen < 0) pGreen = 0;  
    66.                     if (pGreen > 255) pGreen = 255;  
    67.   
    68.                     double pBlue = col.B / 255.0;  
    69.                     pBlue -= 0.5;  
    70.                     pBlue *= contrast;  
    71.                     pBlue += 0.5;  
    72.                     pBlue *= 255;  
    73.                     if (pBlue < 0) pBlue = 0;  
    74.                     if (pBlue > 255) pBlue = 255;  
    75.   
    76.                     bmap.SetPixel(i, j,  
    77.         Color.FromArgb((byte)pRed, (byte)pGreen, (byte)pBlue));  
    78.                 }  
    79.             }  
    80.             _current = (Bitmap)bmap.Clone();  
    81.             Random rnd = new Random();  
    82.             int a = rnd.Next();  
    83.             _current.Save(Server.MapPath("Images/" + a + ".png"));  
    84.             Img.ImageUrl = "Images/" + a + ".png";  
    85.         }  
    86.     }  
    87. }  
Outputs for various contrast values:
 
 


Similar Articles