在项目开发过程中,经常需要从数据库获取二进制图片并显示出来,当存在多个的时候,采用Repeater控件。代码如下:


    <asp:Image runat="server" ImageAlign="Middle" ImageUrl='<%# Eval("FILEID","ShowImageList.ashx?fileid={0}")%>' />
    
<div><%# Eval("FILENAME")%></div>
</ItemTemplate>
</asp:Repeater>

上面是正确的写法,但是先前我在绑定url的时候是用下面的方式

/>

怎么也解析不出来,查了半天也没有发现这个细节,还是建议多使用 Format String。

或者采用下面的方式也可以

 

至于从数据库获取图片并显示,大部分都是用ashx获取图片,如上述代码所示,处理代码如下。

 ProcessRequest (HttpContext context) 
{
    string sFileID = context.Request["fileid"].ToString();
    MemoryStream stream 
= new MemoryStream();
    
    BusinessInvokeResult result 
= FileService.GetFileData(sFileID, string.Empty);
    
if (result.IsSuccessful)
    {
        
byte[] buffer = (byte[])result.ResultValue;
        stream.Write(buffer, 
0, buffer.Length);
        Bitmap bitMap 
= new Bitmap(stream);
        context.Response.ContentType 
= "image/Jpeg";
        bitMap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

 

 

相关文章: