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:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Brightness.aspx.cs" Inherits="Brightness" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
-
- </div>
- </form>
- </body>
- </html>
Step 7: Now drag and drop the following controls from the tool box:
- One File Upload control
- One Button control For upload
- One TextBox control to provide the value for brightness
- One Button control to set the brightness
After doing all this your aspx page code will be:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Brightness.aspx.cs" Inherits="Brightness" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- Upload an Image:<asp:FileUpload ID="FileUpload1" runat="server" />
- <asp:Button ID="btnUpload" runat="server" Text="Upload"/>
- <br />
- Give The Value Of For the Brightness: <asp:TextBox ID="tbBrightness" runat="server" Width="53px"></asp:TextBox>
- <asp:Button ID="btnBright" runat="server" Text="Set Brightness"/>
- <br />
- <asp:Image ID="Img" runat="server" />
- </div>
- </form>
- </body>
- </html>
Step 8: Now open the code behind of this Web Form:
- Add the "System.Drawing" namespace
- Use a global variable, Bitmap _current.
- Write the following code in the page load event:
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Session["filepath"] != null)
- {
- Img.ImageUrl = Session["filepath"].ToString();
- _current = (Bitmap)Bitmap.FromFile(Server.MapPath(Session["filepath"].ToString()));
- }
- }
- Write the following code in the Upload Button's click event:
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- if (FileUpload1.HasFile)
- {
- string filePath = Server.MapPath("Images/" + FileUpload1.FileName);
- FileUpload1.SaveAs(filePath);
- Img.ImageUrl = "Images/" + FileUpload1.FileName;
- Session["filepath"] = "Images/" + FileUpload1.FileName;
- }
- else
- {
- Response.Write("Please Select The File");
- }
- }
- Write the following code in the Bright Button's click event:
- protected void btnBright_Click(object sender, EventArgs e)
- {
- if (tbBrightness.Text != null && Session["filepath"] != null)
- {
- int brightness = int.Parse(tbBrightness.Text);
- Bitmap temp = (Bitmap)_current;
- Bitmap bmap = (Bitmap)temp.Clone();
- if (brightness < -255) brightness = -255;
- if (brightness > 255) brightness = 255;
- Color col;
- for (int i = 0; i < bmap.Width; i++)
- {
- for (int j = 0; j < bmap.Height; j++)
- {
- col = bmap.GetPixel(i, j);
- int colRed = col.R + brightness;
- int colGreen = col.G + brightness;
- int colBlue = col.B + brightness;
-
- if (colRed < 0) colRed = 1;
- if (colRed > 255) colRed = 255;
-
- if (colGreen < 0) colGreen = 1;
- if (colGreen > 255) colGreen = 255;
-
- if (colBlue < 0) colBlue = 1;
- if (colBlue > 255) colBlue = 255;
-
- bmap.SetPixel(i, j, Color.FromArgb((byte)colRed, (byte)colGreen, (byte)colBlue));
- }
- }
- _current = (Bitmap)bmap.Clone();
- Random rnd = new Random();
- int a = rnd.Next();
- _current.Save(Server.MapPath("Images/" + a + ".png"));
- Img.ImageUrl = "Images/" + a + ".png";
- }
- }
Step 9: Run the project; you will get the following output:
- When the project is initially run:
- After uploading an image:
- After giving Brightness=100:
The following is the complete code:
- Brightness.aspx:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Brightness.aspx.cs" Inherits="Brightness" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- Upload an Image:<asp:FileUpload ID="FileUpload1" runat="server" />
- <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
- <br />
- Give The Value Of For the Brightness: <asp:TextBox ID="tbBrightness" runat="server" Width="53px"></asp:TextBox>
- <asp:Button ID="btnBright" runat="server" Text="Set Brightness" OnClick="btnBright_Click"/>
- <br />
- <asp:Image ID="Img" runat="server" />
- </div>
- </form>
- </body>
- </html>
- Brightness.aspx.cs(Code Behind):
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class Brightness : System.Web.UI.Page
- {
- Bitmap _current;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Session["filepath"] != null)
- {
- Img.ImageUrl = Session["filepath"].ToString();
- _current = (Bitmap)Bitmap.FromFile(Server.MapPath(Session["filepath"].ToString()));
- }
- }
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- if (FileUpload1.HasFile)
- {
- string filePath = Server.MapPath("Images/" + FileUpload1.FileName);
- FileUpload1.SaveAs(filePath);
- Img.ImageUrl = "Images/" + FileUpload1.FileName;
- Session["filepath"] = "Images/" + FileUpload1.FileName;
- }
- else
- {
- Response.Write("Please Select The File");
- }
- }
-
- protected void btnBright_Click(object sender, EventArgs e)
- {
- if (tbBrightness.Text != null && Session["filepath"] != null)
- {
- int brightness = int.Parse(tbBrightness.Text);
- Bitmap temp = (Bitmap)_current;
- Bitmap bmap = (Bitmap)temp.Clone();
- if (brightness < -255) brightness = -255;
- if (brightness > 255) brightness = 255;
- Color col;
- for (int i = 0; i < bmap.Width; i++)
- {
- for (int j = 0; j < bmap.Height; j++)
- {
- col = bmap.GetPixel(i, j);
- int colRed = col.R + brightness;
- int colGreen = col.G + brightness;
- int colBlue = col.B + brightness;
-
- if (colRed < 0) colRed = 1;
- if (colRed > 255) colRed = 255;
-
- if (colGreen < 0) colGreen = 1;
- if (colGreen > 255) colGreen = 255;
-
- if (colBlue < 0) colBlue = 1;
- if (colBlue > 255) colBlue = 255;
-
- bmap.SetPixel(i, j, Color.FromArgb((byte)colRed, (byte)colGreen, (byte)colBlue));
- }
- }
- _current = (Bitmap)bmap.Clone();
- Random rnd = new Random();
- int a = rnd.Next();
- _current.Save(Server.MapPath("Images/" + a + ".png"));
- Img.ImageUrl = "Images/" + a + ".png";
- }
- }
- }
Outputs on various various brightness values: