使用GridView绑定图片的方法,不需要在服务器端生成图片
2007-07-12 15:18
519 查看
很多人开始有这个疑问,GridView控件中的ImageField没有DataField属性,那么如何才能绑定到SQL Server中的Image Field?自从DynamicImage控件从beta2中消失后,这就成了个问题。但是,ASP.NET2.0随之也给我们带来了另外一种解决方案,那就是方便地利用HttpHandler(.ashx)动态显示数据库中的图片,这点在VS2005中提供了PersonalWebSite等模版中已经给出方案:通过ashx动态获取数据库中的某条图片数据,然后在GridView等控件的自定义模版中安置一个Image控件,并设置Image控件的ImageUrl属性为类似 XXX.ashx?photoId=1 即可显示图片。
<%@ WebHandler Language="C#" Class="GetPicture" %>
using System;
using System.Web;
using System.IO;
using System.Data.SqlClient;
public class GetPicture : IHttpHandler ...{
public void ProcessRequest (HttpContext context) ...{
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
Stream stream = null;
if (context.Request.QueryString["PhotoID"] != null &&
context.Request.QueryString["PhotoID"] != "")
...{
string photoId = context.Request.QueryString["PhotoID"];
stream = GetPhoto(photoId);
}
else
return;
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
if (stream == null || stream.Length < 1)
return;
int count = stream.Read(buffer, 0, buffersize);
while (count > 0)...{
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}
public Stream GetPhoto(string fileTypeID)...{
SqlConnection myConnection = new SqlConnection("Integrated Security=SSPI;Initial Catalog=WebWindows;Data Source=guosong");
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "GetFileTypeIcon";
myCommand.CommandType = System.Data.CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@fileTypeID", fileTypeID));
myConnection.Open();
object result = myCommand.ExecuteScalar();
try...{
return new MemoryStream((byte[])result);
}
catch (ArgumentNullException e)...{
return null;
}
finally...{
myConnection.Close();
}
}
public bool IsReusable ...{
get ...{
return false;
}
}
}
<asp:TemplateField HeaderText="ICON">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetPicture.ashx?PhotoID=" + Eval("FileTypeID") %>' />
</ItemTemplate>
</asp:TemplateField>
<%@ WebHandler Language="C#" Class="GetPicture" %>
using System;
using System.Web;
using System.IO;
using System.Data.SqlClient;
public class GetPicture : IHttpHandler ...{
public void ProcessRequest (HttpContext context) ...{
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
Stream stream = null;
if (context.Request.QueryString["PhotoID"] != null &&
context.Request.QueryString["PhotoID"] != "")
...{
string photoId = context.Request.QueryString["PhotoID"];
stream = GetPhoto(photoId);
}
else
return;
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
if (stream == null || stream.Length < 1)
return;
int count = stream.Read(buffer, 0, buffersize);
while (count > 0)...{
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}
public Stream GetPhoto(string fileTypeID)...{
SqlConnection myConnection = new SqlConnection("Integrated Security=SSPI;Initial Catalog=WebWindows;Data Source=guosong");
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "GetFileTypeIcon";
myCommand.CommandType = System.Data.CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@fileTypeID", fileTypeID));
myConnection.Open();
object result = myCommand.ExecuteScalar();
try...{
return new MemoryStream((byte[])result);
}
catch (ArgumentNullException e)...{
return null;
}
finally...{
myConnection.Close();
}
}
public bool IsReusable ...{
get ...{
return false;
}
}
}
<asp:TemplateField HeaderText="ICON">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetPicture.ashx?PhotoID=" + Eval("FileTypeID") %>' />
</ItemTemplate>
</asp:TemplateField>
相关文章推荐
- android 针对于GridView中的getView方法的bug,使用本地缓存来处理图片显示的问题
- 生成黑白画笔,android使用系统浏览器打开网页,将Bitmap图片存储在本地JPG文件的方法
- 使用PHP生成图片的缩略图的方法
- PHP文件生成的图片无法使用CDN缓存的解决方法
- 使用POI生成Excel文件及使用POI在EXCEL文件中插入图片的方法
- 使用RoundedBitmapDrawable生成圆角图片的方法
- PHP文件生成的图片无法使用CDN缓存的解决方法
- 使用jquery on()方法为动态生成的元素绑定事件
- ASP.NET技巧:使用Gridview绑定数据库中的图片
- Android开发之使用GridView展示图片的方法
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- PHP使用内置函数生成图片的方法详解
- 织梦使用百度Ueditor编辑器上传图片自动生成绝对路径实现方法(测试可用)
- 解决PHP后端生成的图片无法使用CDN缓存的方法
- Tips/Tricks#2:使用Gridview绑定数据库中的图片
- 使用POI生成Excel文件及使用POI在EXCEL文件中插入图片的方法
- GridView使用DataBind方法绑定数据时的分页问题[转]
- 《GridView使用DataBind方法绑定数据时的分页问题》
- 使用Gridview绑定数据库中的图片