首页 新闻 会员 周边 捐助

我将图片转成字节数组存到SQL SERVER数据库中,类型image,如何用DataList显示图片

0
悬赏园豆:5 [待解决问题]

在SQL SERVER数据库中存放图片的字段是"Image“,类型为image,其存储图片的形式为二进制数据,我想在ASP.NET界面的DataList控件中显示图片,应该如何编写代码?请高手指点……

逸风Michelle的主页 逸风Michelle | 初学一级 | 园豆:180
提问于:2010-08-22 16:13
< >
分享
所有回答(4)
0

可以从数据库中读取出来,显示方式像验证码一样。如:<img src="showimg.aspx?id=12" />,去数据库读去ID为12的图片,并输出这张图片显示在img标签里。具体方法可以参考:

http://www.cnblogs.com/chenkai/archive/2010/02/03/1662542.html

Astar | 园豆:40805 (高人七级) | 2010-08-22 16:27
0
<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();
}

 

不过建议你还是存储图片路径地址比较好点

jowo | 园豆:2834 (老鸟四级) | 2010-08-22 20:41
我按此办法进行编写代码,带仍会出现错误,请帮忙找出问题的所在……具体代码如下: protected void Page_Load(object sender, EventArgs e) { //在此处放置用户代码以初始化页面 if (!IsPostBack) { int imgId=int.Parse(Request.QueryString["Id"]);//imgId为图片的Id //建立数据库连接 SqlConnection con = new SqlConnection("server=PC- PC\\SQL2005;database=UUU1;User Id=sa;Password=sasa"); //数据库操作语句 string strSql = "select * from CateringEntertainment where Id=@Id"; SqlCommand com = new SqlCommand(strSql, con); com.Parameters.Add(new SqlParameter("Id",SqlDbType.Int,4)); com.Parameters["Id"].Value = imgId; con.Open();//打开数据库连接 SqlDataReader dr = com.ExecuteReader();//读出数据 dr.Read();//读一行 //输出图片文件二进制数据 Response.BinaryWrite((byte[])dr["Image"]); dr.Close(); con.Close(); } } this.Image1.ImageUrl = "Image.aspx?Id=xxx";
支持(0) 反对(0) 逸风Michelle | 园豆:180 (初学一级) | 2010-08-24 10:28
不好意思,由于我无法复制截图,代码有点乱,请见谅
支持(0) 反对(0) 逸风Michelle | 园豆:180 (初学一级) | 2010-08-24 10:30
0

获取图片,最好不要放在一个 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;
}
}
}

 

HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-08-23 08:23
0

楼上都很热心,楼主,你该结贴。。。

顾晓北 | 园豆:10893 (专家六级) | 2010-12-24 14:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册