4

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
brijrajsinh
  • 453
  • 3
  • 11

1 Answers1

1

Try to call Debug.Writeline(image1.height) and image1.width. Also add breakpoints and step through the loop. It's hard to tell where the loop is going infinite. To me everything looks good in the loop and it shouldn't go infinite.

Post back the result of Debug.Writeline so we can help or where you have gotten with your debugging process through breakpoints or Debug.WRiteline

Also try running it on a different image and/or confirm the path of the image

like I said your code is good, it's either a path issue which seems not because you are getting the correct width and height or most likely it's an authentication issue. I ran your code using this image Panda.jpg

and I was able to get this image as an output

xyz.jpg

confirm if this is the correct output.

One more thing you can try is change

image1.Save(Server.MapPath(@"~\Image\xyz.jpg"), ImageFormat.Jpeg);

to

image1.Save(AppDomain.CurrentDomain.BaseDirectory + "Image/xyz.jpg", ImageFormat.Jpeg);

my final code

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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_Click" />
                <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>

Default.aspx.cs

using System;
using System.Drawing;
using System.Drawing.Imaging;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(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);
            image1.Save(AppDomain.CurrentDomain.BaseDirectory + "Image/xyz.jpg", ImageFormat.Jpeg);
            NewImage.ImageUrl = @"~/Image/xyz.jpg";
            Label1.Text = "Pixel format: " + image1.PixelFormat.ToString();



        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

}

Project Tree

enter image description here

This works like charm for me. Good luck.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Muhand Jumah
  • 1,726
  • 1
  • 11
  • 26
  • Image hight is: 625 and hight is 450. There is no problem in saving but after save same button click keeps on repeating thats the issue. – brijrajsinh Aug 03 '17 at 11:19
  • Okay so now we eliminated the possibility that something is wrong with the loops. Try commenting all of these lines image1.Save(Server.MapPath(@"~\Image\xyz.jpg"), ImageFormat.Jpeg); NewImage.ImageUrl = @"~/Image/xyz.jpg"; Label1.Text = "Pixel format: " + image1.PixelFormat.ToString(); After commenting them run it and let see if you will get into infinite loop or not – Muhand Jumah Aug 03 '17 at 11:33
  • I have tried all that then i come to know that this line causing issue: image1.Save(Server.MapPath(@"~\Image\xyz.jpg"), ImageFormat.Jpeg); – brijrajsinh Aug 03 '17 at 11:35
  • My next thought is some kind of authentication error. Can you try and set Server.MapPath(@"~\Image\xyz.jpg") in its own variable? then in image1.save call the variable name this way we can tell whether Image.save or Server.MapPath is giving us an infinite loop – Muhand Jumah Aug 03 '17 at 11:40
  • Have you run the code? there is no error in that... – brijrajsinh Aug 03 '17 at 11:46
  • Yes I did and code is running perfect for me. I edited my response and I posted the resulted image, confirm it and also read my edited post. – Muhand Jumah Aug 03 '17 at 12:01
  • Yes, this is correct out put. But didn't it go into infinite loop? – brijrajsinh Aug 03 '17 at 12:04
  • No I didn't get into an infinite loop. Check the last line I added into my edited answer – Muhand Jumah Aug 03 '17 at 12:05
  • ok, try by re-running the code just once. and if you do not get into infinite the please share the code. Thank you so much you effort is appreciated. – brijrajsinh Aug 03 '17 at 12:27
  • Just ran it one more time and it works perfect. I posted my code in the answer, check it out. Your original code worked perfectly as well but in my code I only changed Server.MapPath to AppDomain and works as well. Good luck. – Muhand Jumah Aug 03 '17 at 12:34