Using Generic Handler and Ultrawebgrid To display Images

 It's early in the morning. Morning to you all.

Now, i want to discuss about displaying images on our grid. Ok , you've got application that store the data in binary on Your DB . and then you need to display it on your grid row by row.

ultrawebgrid,Grid can have template column that host asp control and of course asp:image

1.add this on your webgrid

<igtbl:TemplatedColumn BaseColumnName="ImagePreview" Key="ImagePreview" IsBound="True">
                                                         <Header Caption="Imagephoto">
                                                         <RowLayoutColumnInfo OriginX="1" />
                                                         </Header>
                                                         <Footer>
                                                         <RowLayoutColumnInfo OriginX="1" />
                                                         </Footer>
                                                         <CellTemplate>
                                                         <asp:Image runat="server" ID="image1" ImageUrl="<%# Container.Value %>" />
                                                         </CellTemplate>
                                                        </igtbl:TemplatedColumn>

2.Add a httphandler(generic handler) for streaming, ashx is very nice for custom streaming, and aspx is good at streaming html.

 <%@ WebHandler Language="C#" Class="imgstrm" %>

using System;
using System.Web;
using System.Linq;

public class imgstrm : System.Web.IHttpHandler {
    

    public void ProcessRequest (HttpContext context) {
        string id=context.Request.QueryString["id"];
        StreamImage(id, context.Response);
    }
 
    private void StreamImage(string id, HttpResponse response)
    {
        using (MTSDataContext a = new MTSDataContext())
        {
            Collateral theonewesearch = (from temp in a.Collaterals
                                         where temp.ID == id.Trim()
                                         select temp).First();

          using(System.IO.MemoryStream stream = new System.IO.MemoryStream(theonewesearch.Image.ToArray()))
          {

              System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
              byte[] data = theonewesearch.Image.ToArray();
              response.ContentType = "image/jpeg";
              System.Drawing.Bitmap fullbitmap = new System.Drawing.Bitmap(image);
              fullbitmap.Save(response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);     
              response.Flush();
              response.End();
        }
        }
    }
    public bool IsReusable {
        get {
            return true;
        }
    }

}

add on your Data source , change the select query

3. SELECT [ID], [Type], [Description], [Priority],'imgstrm.ashx?id=' + CAST([ID] AS Varchar(16)) as  [ImagePreview]  FROM [Collateral]

and Wualah, and some magic word sim sa labim press F5 , it appears

 

 

Share this post: | | | |
Published Tuesday, April 22, 2008 8:35 AM by cipto
Filed under:

Comments

No Comments