Control Image Brightness Using ASP .NET

Introduction: Lighter or darker images are sometimes needed, it's a personal choice. Sometimes printing needs a lighter image than does the video screen. There is the known problem called "dot gain", meaning the printed dot's ink soaks into the paper and spreads out, resulting in a larger darker dot than was there originally.
 
Brightness is an attribute of visual perception in which a source appears to be radiating or reflecting light. In other words, brightness is the perception elicited by the luminance of a visual target.
 
For the brightness the input range can be -255 to +255.
 
Use the following procedure to create a simple sample application to control brightness.

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 and click on the "Add" button.
 
 
 
Step 6: Now one Web Form will be front of you; provide the following code for it:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Brightness.aspx.cs" Inherits="Brightness" %>  
  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 brightness
  4. One Button control to set the brightness
After doing all this your aspx page code will be:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Brightness.aspx.cs" Inherits="Brightness" %>  
  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 Brightness: <asp:TextBox ID="tbBrightness" runat="server" Width="53px"></asp:TextBox>  
  16.              <asp:Button ID="btnBright" runat="server" Text="Set Brightness"/>  
  17.             <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 Bright Button's click event:
    1. protected void btnBright_Click(object sender, EventArgs e)  
    2. {  
    3.     if (tbBrightness.Text != null && Session["filepath"] != null)  
    4.     {  
    5.         int brightness = int.Parse(tbBrightness.Text);  
    6.         Bitmap temp = (Bitmap)_current;  
    7.         Bitmap bmap = (Bitmap)temp.Clone();  
    8.         if (brightness < -255) brightness = -255;  
    9.         if (brightness > 255) brightness = 255;  
    10.         Color col;  
    11.         for (int i = 0; i < bmap.Width; i++)  
    12.         {  
    13.             for (int j = 0; j < bmap.Height; j++)  
    14.             {  
    15.                 col = bmap.GetPixel(i, j);  
    16.                 int colRed = col.R + brightness;  
    17.                 int colGreen = col.G + brightness;  
    18.                 int colBlue = col.B + brightness;  
    19.   
    20.                 if (colRed < 0) colRed = 1;  
    21.                 if (colRed > 255) colRed = 255;  
    22.   
    23.                 if (colGreen < 0) colGreen = 1;  
    24.                 if (colGreen > 255) colGreen = 255;  
    25.   
    26.                 if (colBlue < 0) colBlue = 1;  
    27.                 if (colBlue > 255) colBlue = 255;  
    28.   
    29.                 bmap.SetPixel(i, j, Color.FromArgb((byte)colRed, (byte)colGreen, (byte)colBlue));  
    30.             }  
    31.         }  
    32.         _current = (Bitmap)bmap.Clone();  
    33.         Random rnd = new Random();  
    34.         int a = rnd.Next();  
    35.         _current.Save(Server.MapPath("Images/" + a + ".png"));  
    36.         Img.ImageUrl = "Images/" + a + ".png";  
    37.     }  
    38. }
Step 9: Run the project; you will get the following output:
  1. When the project is initially run:



  2. After uploading an image:



  3. After giving Brightness=100:


The following is the complete code:
  1. Brightness.aspx:
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Brightness.aspx.cs" Inherits="Brightness" %>  
    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 Brightness: <asp:TextBox ID="tbBrightness" runat="server" Width="53px"></asp:TextBox>  
    16.              <asp:Button ID="btnBright" runat="server" Text="Set Brightness" OnClick="btnBright_Click"/>  
    17.             <br />  
    18.             <asp:Image ID="Img" runat="server" />  
    19.         </div>  
    20.     </form>  
    21. </body>  
    22. </html>  
  2. Brightness.aspx.cs(Code Behind):
    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 Brightness : 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 btnBright_Click(object sender, EventArgs e)  
    36.     {  
    37.         if (tbBrightness.Text != null && Session["filepath"] != null)  
    38.         {  
    39.             int brightness = int.Parse(tbBrightness.Text);  
    40.             Bitmap temp = (Bitmap)_current;  
    41.             Bitmap bmap = (Bitmap)temp.Clone();  
    42.             if (brightness < -255) brightness = -255;  
    43.             if (brightness > 255) brightness = 255;  
    44.             Color col;  
    45.             for (int i = 0; i < bmap.Width; i++)  
    46.             {  
    47.                 for (int j = 0; j < bmap.Height; j++)  
    48.                 {  
    49.                     col = bmap.GetPixel(i, j);  
    50.                     int colRed = col.R + brightness;  
    51.                     int colGreen = col.G + brightness;  
    52.                     int colBlue = col.B + brightness;  
    53.   
    54.                     if (colRed < 0) colRed = 1;  
    55.                     if (colRed > 255) colRed = 255;  
    56.   
    57.                     if (colGreen < 0) colGreen = 1;  
    58.                     if (colGreen > 255) colGreen = 255;  
    59.   
    60.                     if (colBlue < 0) colBlue = 1;  
    61.                     if (colBlue > 255) colBlue = 255;  
    62.   
    63.                     bmap.SetPixel(i, j, Color.FromArgb((byte)colRed, (byte)colGreen, (byte)colBlue));  
    64.                 }  
    65.             }  
    66.             _current = (Bitmap)bmap.Clone();  
    67.             Random rnd = new Random();  
    68.             int a = rnd.Next();  
    69.             _current.Save(Server.MapPath("Images/" + a + ".png"));  
    70.             Img.ImageUrl = "Images/" + a + ".png";  
    71.         }  
    72.     }  
    73. }
Outputs on various brightness values: