Hi
try this example to Change Color of Uploaded Image to Black and White
1- Create new website and add new page and New Folder and Name it Upload and inside this folder add another folder
and name it to Original
2- Open your Web page and add FileUpload and Button and Image control
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”ChangeImageColor.aspx.cs” Inherits=”ChangeImageColor” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Change Color Image to Black and White</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<p>Please upload your picture:
<asp:FileUpload id=”PictureUploadControl” Width=”400″ runat=”server” />
</p>
<p>
<asp:Button runat=”server” id=”AddPictureButton” text=”Upload” onclick=”AddPictureButton_Click” /><br />
</p>
<asp:Image ID=”GrayscaledPhoto” runat=”server” /><br />
</div>
</form>
</body>
</html>
3- In code behind add this code:-
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing; //Bitmap needed this
using System.Drawing.Imaging; //ImageFormat needed this
using System.IO; // .delete needed this
public partial class ChangeImageColor : System.Web.UI.Page
{
public static Bitmap Grayscale(Bitmap bitmap)
{
//Declare myBitmap as a new Bitmap with the same Width & Height
Bitmap myBitmap = new Bitmap(bitmap.Width, bitmap.Height);
for (int i = 0; i < bitmap.Width; i++)
{
for (int x = 0; x < bitmap.Height; x++)
{
//Get the Pixel
Color BitmapColor = bitmap.GetPixel(i, x);
//I want to come back here at some point and understand, then change, the constants
//Declare grayScale as the Grayscale Pixel
int grayScale = (int)((BitmapColor.R * 0.3) + (BitmapColor.G * 0.59) + (BitmapColor.B * 0.11));
//Declare myColor as a Grayscale Color
Color myColor = Color.FromArgb(grayScale, grayScale, grayScale);
//Set the Grayscale Pixel
myBitmap.SetPixel(i, x, myColor);
}
}
return myBitmap;
}
protected void AddPictureButton_Click(object sender, EventArgs e)
{
if (PictureUploadControl.HasFile)
{
PictureUploadControl.SaveAs(Server.MapPath(“~/Upload/Original”) + PictureUploadControl.FileName);
Bitmap oldBitmap = new Bitmap(Server.MapPath(“~/Upload/Original”) + PictureUploadControl.FileName, false);
Bitmap newBitmap = Grayscale(new Bitmap(oldBitmap));
string name = Guid.NewGuid().ToString();
newBitmap.Save(Server.MapPath(“~/Upload/”) + name + “.jpg”, ImageFormat.Jpeg);
oldBitmap.Dispose();
//we will delete the old
// File.Delete(Server.MapPath(“~/Upload/Original”) + PictureUploadControl.FileName);
GrayscaledPhoto.ImageUrl = “Upload/” + name + “.jpg”;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Hope this helps
Good Luck