We can
Change Color of Image Dynamically in dot net. Using bitmap image we can change
Image dimension of image Like Height, Width, Pixel, Color etc. Here We will
Create Change Color of Uploaded Image in dot net So Dynamically we will Invert
color of Image and save in server folder or else in Client Side here save in
Server Folder to download or save or Store in Folder. So let’s start to create
this functionality in Web Application.
Step 1: Design your Webpage like below.
<div><asp:Panel ID="UploadPanel"
runat="server">
<asp:FileUpload ID="FileUpload1"
runat="server"
/>
<asp:Button ID="btnUpload"
runat="server"
Text="Upload"
OnClick="btnUpload_Click"
/>
<br
/>
</asp:Panel>
<asp:Panel ID="InvertPanel"
runat="server">
<asp:Button ID="btnInvert"
runat="server"
Text="Invert"
OnClick="btnInvert_Click"
Height="28px"
Width="68px"
/>
<br
/>
<asp:Image ID="Img" runat="server"
Height="219px"
Width="325px"
/>
</asp:Panel>
</div>
Step 2: Now generate button click event and
Code for Upload File in Server Folder.
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;
UploadPanel.Visible = false;
InvertPanel.Visible = true;
}
else
{
Response.Write("Please Select The File");
}
}
Step 3: Now Maintain Uploaded File in Session
so we can Close or Kill Session when we done Image Invert Color. Now Check in
Page Load () event for Current Session and Check for File Existing or Not and
Check for Panel.
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()));
}
if
(!IsPostBack)
{
UploadPanel.Visible = true;
InvertPanel.Visible = false;
}
}
Step 3: Now Generate Invert button Click event
so when we click that time we can invert Image color.
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/InvrtedImage/" + a + ".png"));
Img.ImageUrl = "images/InvrtedImage/" + a + ".png";
}
}
Here we are using bitmap Image for Inverting
image Color and Store in Folder. Run your Webpage and See Output.
Now Select File and Click on Upload.
Now Click Invert button to invert color.
Here we are Storing both File in Server Folder
as you can see here.
Thanks for comments.....