GridView显示图片流 本帖最后由 gaochenfeng2008 于 2009-10-09 16:16:06 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先把二进制的代码转换成 图片格式这里有个简单的列子 OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath(@"~\App_Data\DataBase.aspx")); try { con.Open(); OleDbCommand com = new OleDbCommand("select * from Picture where Pic_ID=" + Request["Pic_ID"] + "", con); OleDbDataReader dr = com.ExecuteReader(); dr.Read(); MemoryStream ms = new MemoryStream((Byte[])dr["Pic"]); Bitmap image = new Bitmap(ms); dr.Close(); //Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); // Image1.ImageUrl = "~/display9.jpg"; } catch (Exception error) { //Label1.Text = "处理失败!原因为:" + error.ToString(); } finally { con.Close(); } 一般是在.ashx里输出图片信息.ShowPic.ashx, 在GridView的<TemplateField>内<img src="ShowPic.ashx?Key='<%# Eval("主关键字段名") %>" alt="" /><%@ WebHandler Language="C#" Class="ShowPic" %>using System;using System.Web;using System.IO;using System.Data.SqlClient;public class ShowPic : IHttpHandler { public void ProcessRequest (HttpContext context) { int 参数值 = Convert.ToInt32(context.Request.QueryString["Key"]); using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["连接字符串名"].ConnectionString; SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select 图片字段名 from 表名 where 主关键字段名=@参数"; cmd.Parameters.AddWithValue("@参数", 参数值); conn.Open(); object result = cmd.ExecuteScalar(); byte[] byts = (byte[])result; context.Response.ContentType = "image/jpeg"; context.Response.BinaryWrite(byts); } } public bool IsReusable { get { return false; } }} hosts文件的设定,和iis的配置 两个My97DatePicker控件 组合条件查询 如何将字符串转化为日期,然后比较日期的大小 散分,不再扯淡了。 VS2003安装失败求解 Xcopy如何把有空白解决方案加类库的文件全复制出来 .net平台配置好了IIS服务器,.net FrameWork1.1也安装好了,为什么只能建立工程,打开别人的工程错误呢 想做一登陆前等待界面(因.NET第一次访问要等好一会儿) 如何把form中的webcontral全部文本框找到 送分的!关于网站根目录 如果两个表结构相似,要不要合成一个表?谢谢。在线等结贴。
这里有个简单的列子
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath(@"~\App_Data\DataBase.aspx"));
try
{
con.Open();
OleDbCommand com = new OleDbCommand("select * from Picture where Pic_ID=" + Request["Pic_ID"] + "", con);
OleDbDataReader dr = com.ExecuteReader();
dr.Read();
MemoryStream ms = new MemoryStream((Byte[])dr["Pic"]);
Bitmap image = new Bitmap(ms);
dr.Close();
//Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
// Image1.ImageUrl = "~/display9.jpg";
}
catch (Exception error)
{
//Label1.Text = "处理失败!原因为:" + error.ToString();
}
finally
{
con.Close();
}
using System.Web;
using System.IO;
using System.Data.SqlClient;public class ShowPic : IHttpHandler {
public void ProcessRequest (HttpContext context) {
int 参数值 = Convert.ToInt32(context.Request.QueryString["Key"]); using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["连接字符串名"].ConnectionString;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select 图片字段名 from 表名 where 主关键字段名=@参数";
cmd.Parameters.AddWithValue("@参数", 参数值); conn.Open(); object result = cmd.ExecuteScalar(); byte[] byts = (byte[])result;
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(byts);
}
}
public bool IsReusable {
get {
return false;
}
}
}