asp.net2.0 中 为什么不显示图片

maroon 2008-10-31 11:40:11
如题,DrawBar.aspx是显示ZedGraph控件的条状图片,我用img来显示,可就是显示不出来,为什么呀?
...全文
743 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mjjzg 2008-11-01
  • 打赏
  • 举报
回复
DrawBar.aspx??是图片吗
w161134025 2008-11-01
  • 打赏
  • 举报
回复
你首先要保证图片在你的aspx页面输出能正常显示吗,如果正常那么src指向他是不会显示不出来的,
检查下是不是图片在.aspx的页面输出有问题
踏平扶桑 2008-11-01
  • 打赏
  • 举报
回复
不显示还是显示X?
如果不显示你就在浏览器里单独打开那个用来生成验证码的 aspx看能否执行
如果显示X检查一下路径或者生成的代码是否正确,是否输出图片。
zorro911 2008-11-01
  • 打赏
  • 举报
回复
验证码是吧,我用这种形式没有问题
<img id="chkCode" alt="" src="YZNumber.aspx"/>
首先看你路径对对,然后看看你输出图像的那个页面有没有问题,或许你的页面本身就有错误,它怎么可能输出图像呢。
直接让你验证码的页面在浏览器里浏览下,看看能否输出图片。
duchong417 2008-11-01
  • 打赏
  • 举报
回复
首先DrawBar.aspx不是个普通的页面,这个页面要输出的是image格式的,还有有Response.BinaryWrite()输出。
「已注销」 2008-11-01
  • 打赏
  • 举报
回复
测试下DrawBar.aspx是否能输出图片,并且有Response.Write()输出;
记住,输出一定要在page的Load()事件中!
evjen 2008-11-01
  • 打赏
  • 举报
回复
都是牛人
学学...
sunxw18 2008-11-01
  • 打赏
  • 举报
回复
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";你这个与生成的图片格式不对!
acol 2008-11-01
  • 打赏
  • 举报
回复
你运行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();
}




5Br 2008-11-01
  • 打赏
  • 举报
回复
这个还真不知道呢
加强学习了
编程有钱人了 2008-11-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wennxxin 的回复:]
.NET高手已经不屑回答这个问题了
可惜咱不是高手

我就回答吧
首先
这个是基本的HTML语法
你连 <img src="DrawBar.aspx" />里面应该放什么东西都没弄明白

IMG是显示图片
你放的是DrawBar.aspx
而SRC=""应该放的内容应该是图片的地址
你放的是一个ASPX的页面
当然不会显示
[/Quote]

如果这个页面是输出图像的流的这样是可以实现的
例如:这个页面就是用生成验证码的,用个<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();
}


5Br 2008-11-01
  • 打赏
  • 举报
回复
.NET高手已经不屑回答这个问题了
可惜咱不是高手

我就回答吧
首先
这个是基本的HTML语法
你连<img src="DrawBar.aspx" />里面应该放什么东西都没弄明白

IMG是显示图片
你放的是DrawBar.aspx
而SRC=""应该放的内容应该是图片的地址
你放的是一个ASPX的页面
当然不会显示

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧