单击按钮进入无限循环 (ASP.Net)
Button click go into infinite loop (ASP.Net)
我正在从特定路径拍摄图像并在其上应用颜色,然后我想保存图像并在单击按钮时将其显示回来。但是那个点击事件进入了无限循环。
它不会导致循环一段时间,但如果你 运行 投射不止一个
time and place debugger 然后你会发现它正在循环。
下面是我的代码:
aspx代码:
<%@ 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#代码:
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);
}
}
}
}
哪里不对请大家帮帮我
尝试调用 Debug.Writeline(image1.height) 和 image1.width。还要添加断点并单步执行循环。很难说循环将无限延伸到哪里。对我来说,循环中的一切看起来都很好,它不应该是无限的。
Post 返回 Debug.Writeline 的结果,以便我们可以帮助您或通过断点或 Debug.WRiteline
帮助您完成调试过程
也尝试 运行 在不同的图像上 and/or 确认图像的路径
就像我说的你的代码很好,这要么是路径问题,但似乎不是因为你得到了正确的宽度和高度,或者很可能是身份验证问题。我 运行 你的代码使用这张图片
并且我能够将此图像作为输出
确认这是否是正确的输出。
您可以尝试的另一件事是改变
image1.Save(Server.MapPath(@"~\Image\xyz.jpg"), ImageFormat.Jpeg);
至
image1.Save(AppDomain.CurrentDomain.BaseDirectory + "Image/xyz.jpg", ImageFormat.Jpeg);
我的最终代码
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);
}
}
}
项目树
这对我来说就像魅力一样。祝你好运。
我正在从特定路径拍摄图像并在其上应用颜色,然后我想保存图像并在单击按钮时将其显示回来。但是那个点击事件进入了无限循环。
它不会导致循环一段时间,但如果你 运行 投射不止一个 time and place debugger 然后你会发现它正在循环。
下面是我的代码:
aspx代码:
<%@ 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#代码:
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);
}
}
}
}
哪里不对请大家帮帮我
尝试调用 Debug.Writeline(image1.height) 和 image1.width。还要添加断点并单步执行循环。很难说循环将无限延伸到哪里。对我来说,循环中的一切看起来都很好,它不应该是无限的。
Post 返回 Debug.Writeline 的结果,以便我们可以帮助您或通过断点或 Debug.WRiteline
帮助您完成调试过程也尝试 运行 在不同的图像上 and/or 确认图像的路径
就像我说的你的代码很好,这要么是路径问题,但似乎不是因为你得到了正确的宽度和高度,或者很可能是身份验证问题。我 运行 你的代码使用这张图片
并且我能够将此图像作为输出
确认这是否是正确的输出。
您可以尝试的另一件事是改变
image1.Save(Server.MapPath(@"~\Image\xyz.jpg"), ImageFormat.Jpeg);
至
image1.Save(AppDomain.CurrentDomain.BaseDirectory + "Image/xyz.jpg", ImageFormat.Jpeg);
我的最终代码
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);
}
}
}
项目树
这对我来说就像魅力一样。祝你好运。