可以从数据库中读取出来,显示方式像验证码一样。如:<img src="showimg.aspx?id=12" />,去数据库读去ID为12的图片,并输出这张图片显示在img标签里。具体方法可以参考:
http://www.cnblogs.com/chenkai/archive/2010/02/03/1662542.html
<img src= "getImage.aspx?id=xxx "/>
在getImage.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
int imgId = int.Parse(Request.QueryString["imgId"]);//imgId为图片的id
//建立数据库连接
string connString = "server=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088";
SqlConnection conn = new SqlConnection(connString);
//数据库操作语句
string selString = "select * from image where image_id = @image_id";
SqlCommand comm = new SqlCommand(selString,conn);
comm.Parameters.Add(new SqlParameter("@image_id",SqlDbType.Int,4));
comm.Parameters["@image_id"].Value = imgId;
conn.Open();//打开数据库连接
SqlDataReader dr = comm.ExecuteReader();//读出数据
dr.Read();//读一行
//设定输出文件的类型
Response.ContentType = (string)dr["image_content_type"];//上传时,存储的图片类型
//输出图片文件二进制数据
Response.OutputStream.Write((byte[])dr["image_data"],0,(int)dr["image_size"]);
Response.End();
dr.Close();
conn.Close();
}
不过建议你还是存储图片路径地址比较好点
获取图片,最好不要放在一个 page 里面, 这样做效率不高,因为要经历一个页面生命周期,可以自己写个 Handler :
代码如下:
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
public class AjaxImageHandler : IHttpHandler,System.Web.SessionState.IRequiresSessionState
{
private String GetCode(Int32 length)
{
String[] source = {"2","3","4","5","6","7","8","9",
"a","b","c","d","e","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","J","K","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"};
String code = "";
Random rd = new Random();
for (Int32 i = 0; i < length; i++)
{
code += source[rd.Next(0, source.Length)];
}
return code;
}
public void ProcessRequest(HttpContext context)
{
string validationCode = this.GetCode( ConfigHelper.ValidationCodeLength);
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((double)(validationCode.Length * 17)), 27);//画布
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
g.Clear(Color.White); //图片背景色
/**/
///画图片的背景噪音线
for (int i = 0; i < 5; 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", 14, (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.White, Color.White, 1.2f, true);
/**/
///生成不同颜色字符的图片
for (Int32 i = 0; i < validationCode.Length; i++)
{
Brush b = new System.Drawing.SolidBrush(Color.FromArgb(random.Next(160), random.Next(160), random.Next(160)));
Int32 ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.DrawString(validationCode.Substring(i, 1), font, b, 2 + (i * 14), ii);
}
/**/
///画图片的前景噪音点
for (int i = 0; i < 20; 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);
context.Session["validationCode"] = validationCode;
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
}
catch //(Exception e)
{
}
finally
{
g.Dispose();
image.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
楼上都很热心,楼主,你该结贴。。。