Introduction: A gray scale image is an image in which the value of each pixel is a single sample, that is, it carries only intensity information. The gray scale image is also known as a black-and-white Image that is composed exclusively of shades of gray, varying from black at the weakest intensity to white at the strongest.
Use the following procedure to create a simple application for converting a color image to a gray scale image. ( Note: steps 1 to 5 are similar to my previous article: Control Image Brightness Using ASP.NET.)
Step 1: First open 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 (like: "GrayScaleConverter") 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: "GrayScaleConverter.aspx") and click on the "Add" button.
Step 6: Now a Web Form will be front of you and that code will be as in the following:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GrayScaleConverter.aspx.cs" Inherits="GrayScaleConverter" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. </div>
  11. </form>
  12. </body>
  13. </html>
Step 7: Now drag and drop the following controls from the tool box:
  1. One File Upload control
  2. One Button control for uploading
  3. One Button control to convert a color image to a gray scale image
After doing all this, my GrayScaleConverter.aspx page code is:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GrayScaleConverter.aspx.cs" Inherits="GrayScaleConverter" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. Upload an Image:<asp:FileUpload ID="FileUpload1" runat="server" />
  11. <asp:Button ID="btnUpload" runat="server" Text="Upload" />
  12. <br />
  13. <asp:Button ID="btnGrayScale" runat="server" Text="Click Me To Conevert Gray Scale" />
  14. <br /><br />
  15. <asp:Image ID="Img" runat="server" />
  16. </div>
  17. </form>
  18. </body>
  19. </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 for 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 "Gray Scale Converter" Button's click event:
    1. protected void btnGrayScale_Click(object sender, EventArgs e)
    2. {
    3. if (Session["filepath"] != null)
    4. {
    5. Bitmap temp = (Bitmap)_current;
    6. Bitmap bmap = (Bitmap)temp.Clone();
    7. Color col;
    8. for (int i = 0; i < bmap.Width; i++)
    9. {
    10. for (int j = 0; j < bmap.Height; j++)
    11. {
    12. col = bmap.GetPixel(i, j);
    13. byte gray = (byte)(.299 * col.R + .587 * col.G + .114 * col.B);
    14. bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
    15. }
    16. }
    17. _current = (Bitmap)bmap.Clone();
    18. Random rnd = new Random();
    19. int a = rnd.Next();
    20. _current.Save(Server.MapPath("Images/" + a + ".png"));
    21. Img.ImageUrl = "Images/" + a + ".png";
    22. }
    23. }
Step 9: Run the project and you will get the following output:
  1. When the project is initially run:

  2. After uploading an image:


  3. After clicking on the covert gray scale button:

The following is the complete code:
  1. Code for GrayScaleConverter.aspx:
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GrayScaleConverter.aspx.cs" Inherits="GrayScaleConverter" %>
    2. <!DOCTYPE html>
    3. <html xmlns="http://www.w3.org/1999/xhtml">
    4. <head runat="server">
    5. <title></title>
    6. </head>
    7. <body>
    8. <form id="form1" runat="server">
    9. <div>
    10. Upload an Image:<asp:FileUpload ID="FileUpload1" runat="server" />
    11. <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
    12. <br />
    13. <asp:Button ID="btnGrayScale" runat="server" Text="Click Me To Conevert Gray Scale" OnClick="btnGrayScale_Click"/>
    14. <br /><br />
    15. <asp:Image ID="Img" runat="server" />
    16. </div>
    17. </form>
    18. </body>
    19. </html>

  2. Code for GrayScaleConverter.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. public partial class GrayScaleConverter : System.Web.UI.Page
    9. {
    10. Bitmap _current;
    11. protected void Page_Load(object sender, EventArgs e)
    12. {
    13. if (Session["filepath"] != null)
    14. {
    15. Img.ImageUrl = Session["filepath"].ToString();
    16. _current = (Bitmap)Bitmap.FromFile(Server.MapPath(Session["filepath"].ToString()));
    17. }
    18. }
    19. protected void btnUpload_Click(object sender, EventArgs e)
    20. {
    21. if (FileUpload1.HasFile)
    22. {
    23. string filePath = Server.MapPath("Images/" + FileUpload1.FileName);
    24. FileUpload1.SaveAs(filePath);
    25. Img.ImageUrl = "Images/" + FileUpload1.FileName;
    26. Session["filepath"] = "Images/" + FileUpload1.FileName;
    27. }
    28. else
    29. {
    30. Response.Write("Please Select The File");
    31. }
    32. }
    33. protected void btnGrayScale_Click(object sender, EventArgs e)
    34. {
    35. if (Session["filepath"] != null)
    36. {
    37. Bitmap temp = (Bitmap)_current;
    38. Bitmap bmap = (Bitmap)temp.Clone();
    39. Color col;
    40. for (int i = 0; i < bmap.Width; i++)
    41. {
    42. for (int j = 0; j < bmap.Height; j++)
    43. {
    44. col = bmap.GetPixel(i, j);
    45. byte gray = (byte)(.299 * col.R + .587 * col.G + .114 * col.B);
    46. bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
    47. }
    48. }
    49. _current = (Bitmap)bmap.Clone();
    50. Random rnd = new Random();
    51. int a = rnd.Next();
    52. _current.Save(Server.MapPath("Images/" + a + ".png"));
    53. Img.ImageUrl = "Images/" + a + ".png";
    54. }
    55. }
    56. }