2013-06-19 30 views
4

这是我的表在数据库:如何显示数据库中的图像在asp:与linq图像?

enter image description here

而且我读的数据库,如:

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(); 

是有什么错吗?

+0

您正在使用哪种格式的图片的感谢?另外,'image'在SQL Server中是不推荐使用的类型 - 您可能想使用'varbinary'代替。 –

+0

通常为图片的格式是'jpg' – hamed

+0

我不得不说这是一个完美的人:你尝试过什么?说真的,如果你给我们看,这个帖子会受益匪浅。 – ChiefTwoPencils

回答

4

的ASP.NET Image控制松散表示在HTML的<img>标签。因此,只能通过将URL设置为要嵌入页面的图像内容,才能将图像转换为HTML文档。

<img src="images/picture.png" /> 

这意味着你需要一种机制来采取HTTP请求要求的图像资源,并返回包含该图像的二进制数据的响应。

用的ASP.NET Web API这将成为一个简单的操作来实现:

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; 
} 

如果您不能使用网络API,你必须实现一个HTTP处理程序做同样的工作。

在你的ASP.NET页面,您必须将属性ImageUrl设置配置为您的控制器/处理器的地址,包括用户名作为URL的一部分。

0

如果thisuser.picture有你的形象一条合适的路径,你可以尝试这样的事:

Image im = new Image(); 
im.ID = "myImg"; 
im.ImageUrl = thisuser.picture.toString(); 
this.Controls.Add(im); 

我假设你正在使用web表单(我不认为这有差别) 。

此外,这可能需要刷新页面。 'this'指的是Page对象(在我的情况下)。

+0

就拿在餐桌信息仔细一看,这不是一个路径,它@hamed斑点 – IvanL

4
<asp:Image ImageUrl='<%# GetImage(Eval("IMG_DATA")) %>' /> 

上面我们说,我们要采取列[IMG_DATA]的值,并将其传递到页面的方法的getImage,它应该返回一个有效的图像ASPX引擎。所以,让我们实现我们的页面类中的方法:

public string GetImage(object img) 
{ 
    return "data:image/jpg;base64," + Convert.ToBase64String((byte[])img); 
} 
+0

是它不工作? – Manish

+0

伟大与在简单和纯粹databound soloution :) –

0

直接使用ASP.NET Image控件(或至少不是我马上意识到),你不能渲染。快速的在我头顶:

解决方案1: 您需要一些HttpHandler为您的图像。在你的asp:image标签中,你需要一个ImageUrl,它将被这个特定的HttpHandler拾取。您可以使用正确的MIME类型和标题将数据库中的图像内容写为字节。

解决方案2: 看看到URL中嵌入数据,也许你可以写出你的形象是这样,并将其添加到ASP:图像的ImageUrl这样的:https://en.wikipedia.org/wiki/Data_URI_scheme

4

你需要创建通用处理程序 - 让我们把它叫做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" /> 

几点注意事项:

你应该考虑的图像缓存

,你应该从图像SQL类型VARBINARY开关(http://msdn.microsoft.com/en-us/library/ms187993.aspx

你应该存储图像的MIME类型的地方(如果我t为JPEG,PNG,BMP) - 优选地在相同的表,并且服务器在处理程序

    正确的mimetype -
0

创建ashx的页面。它使用的财产以后这样

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); 
     } 
} 
  1. 坐上图像方法

    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; 
    } 
    

    这个方法返回返回形象UR ID。

  2. 你的aspx页面上

    ..

    Image1.ImageUrl = "somthing.ashx?ID="+userImageID; 
    

我希望它会工作。因为我发现它在我自己的工作。你

+0

谢谢@hamed为您的编辑。 – Binod