asp.net2.0 中 <img src="DrawBar.aspx" />为什么不显示图片 如题,DrawBar.aspx是显示ZedGraph控件的条状图片,我用img来显示,可就是显示不出来,为什么呀? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 .NET高手已经不屑回答这个问题了可惜咱不是高手我就回答吧首先这个是基本的HTML语法你连<img src="DrawBar.aspx" />里面应该放什么东西都没弄明白IMG是显示图片你放的是DrawBar.aspx而SRC=""应该放的内容应该是图片的地址你放的是一个ASPX的页面当然不会显示 如果这个页面是输出图像的流的这样是可以实现的例如:这个页面就是用生成验证码的,用个<img src 指向这个页面是可以的DrawBar.aspx.cs public void CheckCodes(string CheckCode) { Random rand = new Random(); int iwidth = (int)(CheckCode.Length * 15); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22); Graphics g = Graphics.FromImage(image); g.Clear(Color.White); //画图片的背景噪音线20条 for (int i = 0; i < 20; i++) { int x1 = rand.Next(image.Width); int x2 = rand.Next(image.Width); int y1 = rand.Next(image.Height); int y2 = rand.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2); } //定义颜色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple, Color.YellowGreen }; //定义字体 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体", "新宋体" }; //随机输出噪点 for (int i = 0; i < 50; i++) { int x = rand.Next(image.Width); int y = rand.Next(image.Height); g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); } //输出不同字体和颜色的验证码字符 for (int i = 0; i < CheckCode.Length; i++) { int cindex = rand.Next(7); int findex = rand.Next(6); Font f = new System.Drawing.Font(font[findex], 12, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(c[cindex]); int ii = 4; if ((i + 1) % 2 == 0) { ii = 2; } g.DrawString(CheckCode.Substring(i, 1), f, b, 3 + (i * 12), ii); } //画一个边框 g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); //输出到浏览器 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); HttpContext.Current.Response.ClearContent(); //Response.ClearContent(); HttpContext.Current.Response.ContentType = "image/Png"; HttpContext.Current.Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } 你运行DrawBar.aspx页面的时候程序有图片生成没,如果没那就是你代码有问题了,如果有生成图片可能就是你路径的问题了。你是想做验证码把,我把我的给你 /// <summary> /// 生成验证码图片 /// </summary> public static void CreateCheckCodeImage() { #region string checkCode = GenerateCheckCode(); if (checkCode == null || checkCode.Trim() == String.Empty) return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音线 for (int i = 0; i < 25; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(checkCode, font, brush, 2, 2); //画图片的前景噪音点 for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ContentType = "image/Gif"; HttpContext.Current.Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } #endregion } /// <summary> /// 判断用户输入的验证码是否正确 /// </summary> /// <param name="code">验证码</param> /// <returns>正确返回TRUE,错误返回FALSH</returns> public static bool checkCode(string code) //code即为用户输入的验证码 { #region if (System.Web.HttpContext.Current.Request.Cookies["CheckCode"] == null) return false; else if (System.Web.HttpContext.Current.Request.Cookies["CheckCode"].Value.ToString().ToLower() != code.ToString().ToLower()) return false; else if (System.Web.HttpContext.Current.Request.Cookies["CheckCode"].Value.ToString().ToLower() == code.ToString().ToLower()) return true; else return false; #endregion }上面你写个类里面下面你写在DrawBar.aspx.cs文件的page_load里生成验证码就好了,你再在你要用的页面里的<img src='DrawBar.aspx' onclick="this.src+='?';"/>点击图片可刷新验证码 注意路径,存放目录 try { if (Request.QueryString["refrush"] == "1") { Response.Redirect(Request.Url.ToString()); } else { Security.CreateCheckCodeImage(); } } catch { Security.CreateCheckCodeImage(); } System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); HttpContext.Current.Response.ContentType = "image/Png";你这个与生成的图片格式不对! 测试下DrawBar.aspx是否能输出图片,并且有Response.Write()输出;记住,输出一定要在page的Load()事件中! 首先DrawBar.aspx不是个普通的页面,这个页面要输出的是image格式的,还有有Response.BinaryWrite()输出。 验证码是吧,我用这种形式没有问题<img id="chkCode" alt="" src="YZNumber.aspx"/>首先看你路径对对,然后看看你输出图像的那个页面有没有问题,或许你的页面本身就有错误,它怎么可能输出图像呢。直接让你验证码的页面在浏览器里浏览下,看看能否输出图片。 不显示还是显示X?如果不显示你就在浏览器里单独打开那个用来生成验证码的 aspx看能否执行如果显示X检查一下路径或者生成的代码是否正确,是否输出图片。 你首先要保证图片在你的aspx页面输出能正常显示吗,如果正常那么src指向他是不会显示不出来的,检查下是不是图片在.aspx的页面输出有问题 如何实现这个单词分割 ListBox数据绑定,新人求解 jatoolsPrinter打印问题 请教一个关于ellipsis的问题! 高兴:成功解决win2003server 下开发asp.net问题散分 有关freetextbox的源码如何调试的问题,谢谢 如何同步复制id parameters参数化SQL语句放注入 一个很少人会关注到的问题,牛人进 为什么Request不行呢? Forms验证,总是需要频繁的登陆,不知是什么原因? findcontrol()方法找不到控件,怎么办?
可惜咱不是高手我就回答吧
首先
这个是基本的HTML语法
你连<img src="DrawBar.aspx" />里面应该放什么东西都没弄明白IMG是显示图片
你放的是DrawBar.aspx
而SRC=""应该放的内容应该是图片的地址
你放的是一个ASPX的页面
当然不会显示
如果这个页面是输出图像的流的这样是可以实现的
例如:这个页面就是用生成验证码的,用个<img src 指向这个页面是可以的DrawBar.aspx.cs public void CheckCodes(string CheckCode)
{
Random rand = new Random();
int iwidth = (int)(CheckCode.Length * 15);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
//画图片的背景噪音线20条
for (int i = 0; i < 20; i++)
{
int x1 = rand.Next(image.Width);
int x2 = rand.Next(image.Width);
int y1 = rand.Next(image.Height);
int y2 = rand.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);
}
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple, Color.YellowGreen };
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体", "新宋体" }; //随机输出噪点
for (int i = 0; i < 50; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
} //输出不同字体和颜色的验证码字符
for (int i = 0; i < CheckCode.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(6); Font f = new System.Drawing.Font(font[findex], 12, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.DrawString(CheckCode.Substring(i, 1), f, b, 3 + (i * 12), ii);
}
//画一个边框 g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); //输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
//Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Png";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
/// <summary>
/// 生成验证码图片
/// </summary>
public static void CreateCheckCodeImage()
{
#region
string checkCode = GenerateCheckCode();
if (checkCode == null || checkCode.Trim() == String.Empty)
return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image); try
{
//生成随机生成器
Random random = new Random(); //清空图片背景色
g.Clear(Color.White); //画图片的背景噪音线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
} Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2); //画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next()));
} //画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
#endregion
}
/// <summary>
/// 判断用户输入的验证码是否正确
/// </summary>
/// <param name="code">验证码</param>
/// <returns>正确返回TRUE,错误返回FALSH</returns>
public static bool checkCode(string code) //code即为用户输入的验证码
{
#region
if (System.Web.HttpContext.Current.Request.Cookies["CheckCode"] == null)
return false;
else if (System.Web.HttpContext.Current.Request.Cookies["CheckCode"].Value.ToString().ToLower() != code.ToString().ToLower())
return false;
else if (System.Web.HttpContext.Current.Request.Cookies["CheckCode"].Value.ToString().ToLower() == code.ToString().ToLower())
return true;
else
return false;
#endregion
}
上面你写个类里面下面你写在DrawBar.aspx.cs文件的page_load里生成验证码就好了,你再在你要用的页面里的<img src='DrawBar.aspx' onclick="this.src+='?';"/>点击图片可刷新验证码 注意路径,存放目录
try
{
if (Request.QueryString["refrush"] == "1")
{
Response.Redirect(Request.Url.ToString());
}
else {
Security.CreateCheckCodeImage();
}
}
catch
{
Security.CreateCheckCodeImage();
}
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray()); HttpContext.Current.Response.ContentType = "image/Png";你这个与生成的图片格式不对!
记住,输出一定要在page的Load()事件中!
<img id="chkCode" alt="" src="YZNumber.aspx"/>
首先看你路径对对,然后看看你输出图像的那个页面有没有问题,或许你的页面本身就有错误,它怎么可能输出图像呢。
直接让你验证码的页面在浏览器里浏览下,看看能否输出图片。
如果不显示你就在浏览器里单独打开那个用来生成验证码的 aspx看能否执行
如果显示X检查一下路径或者生成的代码是否正确,是否输出图片。
检查下是不是图片在.aspx的页面输出有问题