验证码的显示问题,直接链接图片页面就行,放在image控件就不显示,为什么?

szheto 2006-01-08 06:34:34
<asp:image id=Image1 runat="server" Width='<%# Session("image_w")%>' ImageUrl="ValidateCode.aspx" Height='<%# Session("image_h") %>'></asp:image>
直接链接ValidateCode.aspx就OK呀,但以上代码却不能显示,是什么回事?
...全文
258 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
terence4444 2006-04-21
  • 打赏
  • 举报
回复
mark
qpgsd 2006-04-07
  • 打赏
  • 举报
回复
mark
husttc 2006-03-11
  • 打赏
  • 举报
回复
mark
LoveMango 2006-01-18
  • 打赏
  • 举报
回复
MARK
szheto 2006-01-09
  • 打赏
  • 举报
回复
我终于明白了,原来是Forms验证误事,加上这个就可以了.
<location path="Model/Login/ValidateCode.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>

谢谢大家!
szheto 2006-01-09
  • 打赏
  • 举报
回复
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim My_Stream As MemoryStream = Get_Images(12, 4, "#ffffff") '参数分别为:字体大小,验证码位数,背景色
Show_image(My_Stream) '显示内存图像
My_Stream.Close() '关闭打开的流文件

End Sub

'将字符转换成图像
'参数说明:
' Font_Size 字体大小
' Char_Number 验证码位数
' BackgroundColor 背景色
Function Get_Images(ByVal Font_Size As Integer, ByVal Char_Number As Integer, ByVal BackgroundColor As String) As MemoryStream
Dim image_w As Integer = Int(Font_Size * 1.5) + Font_Size * Char_Number
Dim image_h As Integer = Int(Font_Size / 2)

'******************************************
' 保存图像的 宽度 和 高度,供调用页面使用
'******************************************
Session("image_w") = image_w
Session("image_h") = image_h


Dim Temp_Bitmap As Bitmap '封装GDI+位图
Dim Temp_Graphics As Graphics '封装GDI+绘图面
Dim Color_Back As Color = ColorTranslator.FromHtml(BackgroundColor) '背景颜色

Temp_Bitmap = New Bitmap(image_w, 5 * image_h, PixelFormat.Format32bppRgb) '确定背景大小

Temp_Graphics = Graphics.FromImage(Temp_Bitmap)
Temp_Graphics.FillRectangle(New SolidBrush(Color_Back), New Rectangle(0, 0, image_w, 5 * image_h)) '绘制背景

Dim Sesson_Company As String = "" '为了进行验证比较
Dim n As Integer
For n = 0 To Char_Number - 1
Dim Show_Str As String = getChar() '要显示为图像的字符
Sesson_Company = Sesson_Company & Show_Str
Dim Show_Str_Font_Size As Integer = Int(3 * Rnd() + (Font_Size - 2)) '字体随机大小
Dim Color_Font As Color = ColorTranslator.FromHtml(getColor()) '字体随机颜色
Dim Show_Font_Name As String = getFont() '字体
Dim Show_Str_Font As Font = New Font(Show_Font_Name, Show_Str_Font_Size, FontStyle.Bold) '定义文本格式(字体,字号,粗体)
Temp_Graphics.DrawString(Show_Str, Show_Str_Font, New SolidBrush(Color_Font), Int(Font_Size / 2) + n * Font_Size, Int(image_h * 0.125 * Rnd() + image_h * 0.08)) '绘出字符 '绘字符的Y方向下波动+4
Next

'********************************
' 保存到session便于调用的页面比较
'********************************
Session("imagenumber") = Trim(Sesson_Company)

Font_Size = Font_Size * 4
image_h = Int(2.5 * Font_Size)

Dim Temp_Stream As MemoryStream = New MemoryStream
Temp_Bitmap.Save(Temp_Stream, ImageFormat.Jpeg)
Temp_Graphics.Dispose() '释放资源
Temp_Bitmap.Dispose() '释放资源
Temp_Stream.Close() '关闭打开的流文件
Return Temp_Stream '返回流
End Function


'显示内存图像
Function Show_image(ByVal Show_Stream As MemoryStream)
Response.ClearContent()
Response.ContentType = "Image/Jpeg"
Response.BinaryWrite(Show_Stream.ToArray())
Response.End()
End Function

'获得随机字符 0-9 a-z A-Z
Function getChar() As String
Dim Char_array(5)
Char_array(0) = Chr(Int(10 * Rnd() + 48))
Char_array(1) = Chr(Int(26 * Rnd() + 65))
Char_array(2) = Chr(Int(26 * Rnd() + 65))
Char_array(3) = Chr(Int(26 * Rnd() + 97))
Char_array(4) = Chr(Int(26 * Rnd() + 97))
Return Char_array(Int(5 * Rnd()))
End Function

'获得随机颜色
Function getColor() As String
Dim int_a As Integer
Dim int_b As Integer
Dim int_c As Integer
int_a = Int(180 * Rnd() + 20)
int_b = Int(180 * Rnd() + 20)
int_c = Int(180 * Rnd() + 20)
If int_a > 150 And int_b > 150 And int_c > 150 Then int_a = Int(150 * Rnd() + 20)
Return "#" & Hex(int_a) & Hex(int_b) & Hex(int_c)
End Function

'获得随机字体
Function getFont() As String
Dim font_array(7)
font_array(0) = "Arial Black"
font_array(1) = "BatangChe"
font_array(2) = "Century"
font_array(3) = "Arial Black"
font_array(4) = "Arial Black"
font_array(5) = "Arial Black"
font_array(6) = "Arial Black"
Return font_array(Int(7 * Rnd()))
End Function
  • 打赏
  • 举报
回复
你的图片生成页面是不是这种格式的

public class CreateImage : System.Web.UI.Page
{
public void RenderImage ( )//注意
{
using ( Bitmap image = yourimage )
{
Response.ContentType = "image/gif" ;

image.Save ( Response.OutputStream , ImageFormat.Gif ) ;//注意
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{

}
#endregion
}

然后

<img src="CreateImage.aspx">

这样是可以的 甚至你可以在vs的设计窗口里看到图片的
yzgnick 2006-01-09
  • 打赏
  • 举报
回复
把<asp:image id=Image1 runat="server" Width='<%# Session("image_w")%>' ImageUrl="ValidateCode.aspx" Height='<%# Session("image_h") %>'></asp:image>改成
<asp:image id=Image1 runat="server" Width='<%= Session("image_w")%>' ImageUrl="ValidateCode.aspx" Height='<%= Session("image_h") %>'></asp:image>试试

szheto 2006-01-09
  • 打赏
  • 举报
回复
试过了,都不行
fangzhe 2006-01-08
  • 打赏
  • 举报
回复
asp:image大概是直接读取吧?这样你那个ValidateCode.aspx就不执行了,不执行当然出不来了
老老实实地用img src=吧
sheng9hhd 2006-01-08
  • 打赏
  • 举报
回复
<img src="ValidateCode.aspx">试试

62,047

社区成员

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

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

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

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