Asp.net 如何在asp中显示数据库中的图像:使用linq显示图像?
这是数据库中的我的表: 我读数据库的时候是这样的:Asp.net 如何在asp中显示数据库中的图像:使用linq显示图像?,asp.net,linq,Asp.net,Linq,这是数据库中的我的表: 我读数据库的时候是这样的: DataClassesDataContext db = new DataClassesDataContext(); usertable thisuser = db.usertables.First(p => p.username == User.Identity.Name); 因此,thisuser.picture是图像的句柄。但是如何在页面上的asp:image控件中显示它呢 编辑 我用以下代码保存图片: DataClassesDa
DataClassesDataContext db = new DataClassesDataContext();
usertable thisuser = db.usertables.First(p => p.username == User.Identity.Name);
因此,thisuser.picture
是图像的句柄。但是如何在页面上的asp:image控件中显示它呢
编辑
我用以下代码保存图片:
DataClassesDataContext db = new DataClassesDataContext();
usertable thisuser = db.usertables.First(p => p.username == User.Identity.Name);
byte[] filebyte = FileUpload1.FileBytes;
System.Data.Linq.Binary fileBinary = new System.Data.Linq.Binary(filebyte);
thisuser.picture = fileBinary;
db.SubmitChanges();
有什么问题吗?如果thisuser.picture有正确的图像路径,您可以尝试以下方法:
Image im = new Image();
im.ID = "myImg";
im.ImageUrl = thisuser.picture.toString();
this.Controls.Add(im);
我假设您正在使用web表单(我不认为这有什么区别)
此外,这可能需要刷新页面。“this”指的是页面对象(在我的例子中)。您不能使用ASP.NET图像控件直接呈现它(至少我现在不知道)。我的头顶上快速地响起: 解决方案1: 您的图像需要一些HttpHandler。在asp:image标记中,您需要一个ImageUrl,该URL将由此特定的HttpHandler拾取。在那里,您可以使用正确的MIME类型和标题将数据库中的图像内容写入字节 解决方案2:
看看如何在URL中嵌入数据,也许你可以像这样写出你的图像并将其添加到asp:image ImageUrl中,就像这样:你需要创建通用处理程序-让我们称之为ImageHandler.ashx,它将位于你的web应用程序的根目录中
public class ImageHandler : IHttpHandler {
public void ProcessRequest(HttpContext context) {
// here is the tricky part - you don't store image type anywhere (you should)
// assuming jpeg
context.Response.ContentType = "image/jpeg";
DataClassesDataContext db = new DataClassesDataContext();
if (HttpContext.Current.User != null && HttpContext.Current.User.Identity != null) {
var thisuser = db.usertables.First(p => p.username == HttpContext.Current.User.Identity.Name);
if (thisuser != null) {
byte[] buffer = thisuser.picture.ToArray();
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
}
}
public bool IsReusable {
get { return false; }
}
}
然后在页面中,添加:
<asp:Image runat="server" ImageUrl="~/ImageHandler.ashx" />
)
您应该将图像mimetype存储在某个位置(如果是jpeg、png、bmp)-最好是在同一个表中,并在处理程序中提供正确的mimetype
-
ASP.NET
图像
控件松散地表示HTML中的
标记。因此,只有通过将URL设置为要嵌入页面的图像内容,才能将图像获取到HTML文档中
如果不能使用WebAPI,则必须实现HTTP处理程序来完成相同的工作
在ASP.NET页面中,必须将属性ImageUrl
设置为为控制器/处理程序配置的地址,包括作为URL一部分的用户名。创建ashx页面。在上面用这样的东西
public HttpResponseMessage GetImage(string username)
{
DataClassesDataContext db = new DataClassesDataContext();
usertable thisuser = db.usertables.FirstOrDefault(
p => p.username == username);
if (thisuser == null)
{
return new HttpResponseMessage(HttpStatusCode.NotFound));
}
// Create a stream to return to the user.
var stream = new MemoryStream(thisuser.picture.ToArray());
// Compose a response containing the image and return to the user.
var result = new HttpResponseMessage();
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("image/jpeg");
return result;
}
关于获取图像的方法
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
Stream strm = new MemoryStream(GetImage(ID));
long length = strm.Length;
byte[] buffer = new byte[length];
int byteSeq = strm.Read(buffer, 0, 2048);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 2048);
}
}
此方法返回图像作为ur ID
在您的aspx页面上
public static byte[] GetImage(string ImageId)
{
byte[] img = null;
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "USP_SELECT_GLOBAL_METHOD";
cmd.Parameters.AddWithValue("@EVENT", 5);
cmd.Parameters.AddWithValue("@CODE", ImageId);
cmd.Connection = DL_CCommon.Connection();
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read())
{
img = (byte[])dr[0];
}
dr.Close();
return img;
}
我希望它能起作用。正如我发现它在我自己身上起作用一样。谢谢仔细查看表格信息,它不是路径,而是一个blob您使用的图像格式是什么?另外,image
在SQL Server中是一种不推荐使用的类型-您可能需要使用varbinary
来代替。通常图像的格式是jpg
我不得不说这是一种完美的格式:您尝试了什么?。说真的,如果你给我们看的话,这篇文章会受益匪浅。非常适合使用简单且纯数据绑定的soloution:)
public HttpResponseMessage GetImage(string username)
{
DataClassesDataContext db = new DataClassesDataContext();
usertable thisuser = db.usertables.FirstOrDefault(
p => p.username == username);
if (thisuser == null)
{
return new HttpResponseMessage(HttpStatusCode.NotFound));
}
// Create a stream to return to the user.
var stream = new MemoryStream(thisuser.picture.ToArray());
// Compose a response containing the image and return to the user.
var result = new HttpResponseMessage();
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("image/jpeg");
return result;
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
Stream strm = new MemoryStream(GetImage(ID));
long length = strm.Length;
byte[] buffer = new byte[length];
int byteSeq = strm.Read(buffer, 0, 2048);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 2048);
}
}
public static byte[] GetImage(string ImageId)
{
byte[] img = null;
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "USP_SELECT_GLOBAL_METHOD";
cmd.Parameters.AddWithValue("@EVENT", 5);
cmd.Parameters.AddWithValue("@CODE", ImageId);
cmd.Connection = DL_CCommon.Connection();
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read())
{
img = (byte[])dr[0];
}
dr.Close();
return img;
}
Image1.ImageUrl = "somthing.ashx?ID="+userImageID;