I am taking a image from a particular path and apply color on it then i want to save the image and display it back on button click. but that click event go into infinite loop.
It do not cause loop some time but if you run project more than one time and place debugger then you will find that it is looping.
Below is my code:
aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Convert.aspx.cs" Inherits="ConvertImage.Convert" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click1" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
<div>
<asp:Image ID="OldImage" ImageUrl="~/Image/Panda.jpg" runat="server" />
<asp:Image ID="NewImage" runat="server" />
</div>
</div>
</form>
</body>
</html>
c# code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using static System.Net.Mime.MediaTypeNames;
namespace ConvertImage
{
public partial class Convert : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
CompareImage();
}
private void CompareImage()
{
Bitmap image1;
try
{
image1 = new Bitmap(Server.MapPath(@"~\Image\Panda.jpg"), true);
OldImage.ImageUrl = "~/Image/Panda.jpg";
int x, y;
for (x = 0; x < image1.Width; x++)
{
for (y = 0; y < image1.Height; y++)
{
if (y < 150)
{
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 25, 60);
image1.SetPixel(x, y, newColor);
}
else if (y >= 150 && y < 300)
{
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 200, 100);
image1.SetPixel(x, y, newColor);
}
else
{
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.R, 100, 210);
image1.SetPixel(x, y, newColor);
}
}
}
image1.Save(Server.MapPath(@"~\Image\xyz.jpg"), ImageFormat.Jpeg);
NewImage.ImageUrl = @"~/Image/xyz.jpg";
Label1.Text = "Pixel format: " + image1.PixelFormat.ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
Please help me where i am wrong.