Introduction
Inverting an image's color is a filtering mechanism by replacing a color with the opposite color. For example if the color in the component is 00 then the opposite we get is FF (255-0). like:
The invert color for RGB is CMY.
Color |
Inverted Color |
Red |
Cyan |
Green |
Magenta |
Blue |
Yellow |
Let's step-by-step make a simple application for inverting image color that is as the following:
Step 1: First open Visual Studio.
Step 2: In the menu bar click on the "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 a name (Like: "Invert") 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 in front of you, select "Web Form" and provide the name of the Web Form (Like: InvertImageColor.aspx) and click on the "Add" button.
Step 6: Now a Web Form will be in front of you and that code will be as the following:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="InvertImageColor.aspx.cs" Inherits="InvertImageColor" %>
-
- <!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 toolbox:
- One File Upload control.
- One Button control For upload.
- One Button control to invert image color.
- One Image Control.
After doing all this, my InvertImageColoe.aspx page code is:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="InvertImageColor.aspx.cs" Inherits="InvertImageColor" %>
-
- <!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 />
- <asp:Button ID="btnInvert" runat="server" Text="Invert Image Color" />
- <br />
- <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 btnUpload_Click(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("<script>alert('Please Select The File')</script>");
- }
- }
- Write the following code in the "Invert Image Color" Button's click event:
- protected void btnInvert_Click(object sender, EventArgs e)
- {
- if (Session["filepath"] != null)
- {
- Bitmap temp = (Bitmap)_current;
- Bitmap bmap = (Bitmap)temp.Clone();
- Color col;
- for (int i = 0; i < bmap.Width; i++)
- {
- for (int j = 0; j < bmap.Height; j++)
- {
- col = bmap.GetPixel(i, j);
- bmap.SetPixel(i, j,
- Color.FromArgb(255 - col.R, 255 - col.G, 255 - col.B));
- }
- }
- _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 and you will get the following output:
- When the project is initially run:
- After uploading an image:
- After clicking on the "Invert Image Color" button:
Complete Code
- Code for InvertImageColor.aspx:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="InvertImageColor.aspx.cs" Inherits="InvertImageColor" %>
-
- <!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 />
- <asp:Button ID="btnInvert" runat="server" Text="Invert Image Color" OnClick="btnInvert_Click" />
- <br />
- <br />
- <asp:Image ID="Img" runat="server" />
- </div>
- </form>
- </body>
- </html>
- Code for InvertImageColor.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 InvertImageColor : 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 btnInvert_Click(object sender, EventArgs e)
- {
- if (Session["filepath"] != null)
- {
- Bitmap temp = (Bitmap)_current;
- Bitmap bmap = (Bitmap)temp.Clone();
- Color col;
- for (int i = 0; i < bmap.Width; i++)
- {
- for (int j = 0; j < bmap.Height; j++)
- {
- col = bmap.GetPixel(i, j);
- bmap.SetPixel(i, j,
- Color.FromArgb(255 - col.R, 255 - col.G, 255 - col.B));
- }
- }
- _current = (Bitmap)bmap.Clone();
- Random rnd = new Random();
- int a = rnd.Next();
- _current.Save(Server.MapPath("Images/" + a + ".png"));
- Img.ImageUrl = "Images/" + a + ".png";
- }
- }
- }