从sqlServer的image字段获取并显示图像示例(ASPNET2.0)
来源:互联网 发布:王治郅nba生涯数据 编辑:程序博客网 时间:2024/05/16 17:30
第一种方法:
要两个页,第一个页面放Image控件(A.aspx);另一个页面用 Response.BinaryWrite((byte[])users.PhotoData)显示image类型图片(B.aspx);
A.aspx:
<asp:Image ID="ManagerPhoto" runat="server" Width="50px" />
A.aspx.cs:
ManagerPhoto.ImageUrl = "B.aspx?ID=" + ID.ToString();
B.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Users users = _org.GetUsersInfomationByID(Convert.ToInt32(Request.QueryString["ID"]));
Response.BinaryWrite((byte[])users.PhotoData);
}
A.aspx:
<asp:Image ID="ManagerPhoto" runat="server" Width="50px" />
A.aspx.cs:
ManagerPhoto.ImageUrl = "B.aspx?ID=" + ID.ToString();
B.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Users users = _org.GetUsersInfomationByID(Convert.ToInt32(Request.QueryString["ID"]));
Response.BinaryWrite((byte[])users.PhotoData);
}
第二种方法:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Configuration;
public class Handler : IHttpHandler {
public bool IsReusable {
get {
return true;
}
}
public bool IsReusable {
get {
return true;
}
}
public void ProcessRequest(HttpContext context) {
// Set up the response settings
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
// Set up the response settings
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
Stream stream = null;
if (context.Request.QueryString["employeeID"] != "") {
stream = GetPhoto(context.Request.QueryString["employeeID"]);
}
stream = GetPhoto(context.Request.QueryString["employeeID"]);
}
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0) {
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}
byte[] buffer = new byte[buffersize];
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 photoId) {
SqlConnection myConnection = new SqlConnection("server=192.168.3.17;database=mypeople;user id='sa';password='");
SqlCommand myCommand = new SqlCommand
("SELECT [照片] FROM [people] WHERE [职工编号]=@PhotoID",
myConnection);
myCommand.CommandType = CommandType.Text;
myCommand.Parameters.Add(new SqlParameter("@PhotoID", photoId));
myConnection.Open();
object result = myCommand.ExecuteScalar();
try {
return new MemoryStream((byte[])result);
}
catch (ArgumentNullException e) {
return null;
}
finally {
myConnection.Close();
}
}
}
SqlConnection myConnection = new SqlConnection("server=192.168.3.17;database=mypeople;user id='sa';password='");
SqlCommand myCommand = new SqlCommand
("SELECT [照片] FROM [people] WHERE [职工编号]=@PhotoID",
myConnection);
myCommand.CommandType = CommandType.Text;
myCommand.Parameters.Add(new SqlParameter("@PhotoID", photoId));
myConnection.Open();
object result = myCommand.ExecuteScalar();
try {
return new MemoryStream((byte[])result);
}
catch (ArgumentNullException e) {
return null;
}
finally {
myConnection.Close();
}
}
}
/* ----------------------引用方法---------------------------------------
<asp:Image ID="imgPhoto" ForeColor =Red runat="server"
ImageUrl='<%# "Handler2.ashx?employeeID=" + Eval("职工编号") %>'
AlternateText="该员工无照片" Height="248px" Width="188px" />
-----------------------------------------------------------------------*/
<asp:Image ID="imgPhoto" ForeColor =Red runat="server"
ImageUrl='<%# "Handler2.ashx?employeeID=" + Eval("职工编号") %>'
AlternateText="该员工无照片" Height="248px" Width="188px" />
-----------------------------------------------------------------------*/
- 从sqlServer的image字段获取并显示图像示例(ASPNET2.0)
- ASPNET2.0的Cookie实现
- AspNet2.0GRIDView设置显示条数
- sqlserver image字段默认值
- 获取SqlServer表的字段
- 从数据库中读出图片并显示的示例代码
- 从数据库中读出图片并显示的示例代码
- 存储图片到数据库的image字段中,并在Winform中显示图片
- 存储图片到数据库的image字段中,并在Winform中显示图片
- 从数据库中取出IMAGE字段并返回DataSet
- mybatis-generator获取sqlServer字段注释并修改生成的实体类属性的注释
- ASPNET2.0中读写Cookie的方法!
- ASPNET2.0中读写Cookie的方法
- java操作sqlserver image字段
- Hibernate操作SQLServer的image和text字段的问题
- 从assets获取 image/获取文件并读取数据
- 如何操作sqlserver 数据库的image字段[C#]
- 怎样向SQLServer插入带有Image字段的记录
- 关于SIGBUS的总结
- CPL2005冬季锦标赛开幕 RocketBoy参赛Quake4
- JavaScript获取下拉列表选中项的值和文本
- 开博
- CDC类及其子类---CClientDC、CWindowDC和CPaintDC
- 从sqlServer的image字段获取并显示图像示例(ASPNET2.0)
- TCP头校验和计算算法详解
- Word 2003审阅功能与SDL Trados 2007 Workbench发生冲突
- 揭秘jbpm流程引擎内核设计思想及构架(经典之作,收藏之)
- using namespace std
- LINUX修改为从WIN默认启动
- 指针和数组名的区别 char *str 和 char str[]的不同之处
- 求一个整型数组第二大的数
- 开通了博客