DataGrid shows that the use of images from the database

  Foreword 

  How do we create a DataGrid out that obtained from the database image? 

  This is a frequently asked question on the DataGrid control, and other can easily be through a combination of you already know about the content of the template are a little handle on the handling HTTP (HTTP Handler) knowledge to answer. 

  Below NorthWind use of the database to the Employees table in a DataGrid to display images in the database. 

  Code 

  — BindImg.aspx 

  <% @ Page language = "c #" Codebehind = "BindImg.aspx.cs" AutoEventWireup = "false" Inherits = "ShowImg.BindImg"%> 
<HTML>
<HEAD>
  <title> BindImg </ title> 
  </ HEAD> 
<body>
  <form Id="Form1" method="post" runat="server"> 
  <FONT Face="宋体"> 
  <asp:DataGrid Id="MyDataGrid" runat="server" AutoGenerateColumns="False" Width="632px"> 
  <AlternatingItemStyle BackColor="Beige"> </ AlternatingItemStyle> 
  <HeaderStyle HorizontalAlign="Center"> </ HeaderStyle> 
<Columns>
  <asp:TemplateColumn HeaderText="Photo"> 
<ItemTemplate>
  <Img src ='<%# "GetImg.ashx? ID =" + DataBinder.Eval (Container.DataItem, "EmployeeID ")%>'> 
  </ ItemTemplate> 
  </ Asp: TemplateColumn> 
  <asp:BoundColumn DataField="LastName" HeaderText="Last Name"> </ asp: BoundColumn> 
  <asp:BoundColumn DataField="FirstName" HeaderText="First Name"> </ asp: BoundColumn> 
  <asp:BoundColumn DataField="title" HeaderText="Title"> </ asp: BoundColumn> 
  </ Columns> 
  </ Asp: DataGrid> </ FONT> 
  </ Form> 
  </ Body> 
  </ HTML> 

  — BindImg.aspx.cs 

  Using System; 
  Using System.Data; 
  Using System.Drawing; 
  Using System.Web; 

  Using System.Data.SqlClient; 

  Namespace ShowImg 
  ( 
  / / / <summary> 
  / / / BindImg explanatory memorandum. 
  / / / </ Summary> 
  Public class BindImg: System.Web.UI.Page 
  ( 
  Protected System.Web.UI.WebControls.DataGrid MyDataGrid; 

  Private void Page_Load (object sender, System.EventArgs e) 
  ( 
  / / User code placed here to initialize pages 
  If (! Page.IsPostBack) 
  ( 
  SqlConnection conn = new SqlConnection (@ "Server = shoutor \ mydb; database = northwind; uid = sa; Pwd = shoutor"); 
  Try 
  ( 
  Conn.Open (); 
  SqlCommand cmd = new SqlCommand ( "select employeeID, lastname, firstname, title from employees," conn); 
  SqlDataReader reader = cmd.ExecuteReader (); 
  MyDataGrid.DataSource = reader; 
  MyDataGrid.DataBind (); 
  ) 
  Finally 
  ( 
  Conn.Close (); 
  ) 
  ) 
  ) 
  # Region Web form designer generated code 
  Override protected void OnInit (EventArgs e) 
  ( 
  / / 
  / / CODEGEN: The call is ASP.NET Web Forms designer necessary. 
  / / 
  InitializeComponent (); 
  Base.OnInit (e); 
  ) 

  / / / <summary> 
  / / / Designer support of the approach - Do not use the code editor Laws 
  / / / The contents of this method. 
  / / / </ Summary> 
  Private void InitializeComponent () 
  ( 
  This.Load + = new System.EventHandler (this.Page_Load); 
  ) 
  # Endregion 
  ) 
  ) 

  — GetImg.ashx 

  <% @ WebHandler Language = "C #" Class = "ShowImg.GetImg"%> 

  — GetImg.aspx.cs 

  Using System; 
  Using System.Web; 
  Using System.Data; 
  Using System.Data.SqlClient; 
  Using System.Drawing; 
  Using System.Drawing.Imaging; 
  Using System.IO; 

  Namespace ShowImg 
  ( 
  / / / <summary> 
  / / / GetImg explanatory memorandum. 
  / / / </ Summary> 
  Public class GetImg: IHttpHandler 
  ( 
  Public void ProcessRequest (HttpContext context) 
  ( 
  String id = (string) context.Request [ "id"]; 
  If (id! = Null) 
  ( 
  MemoryStream stream = new MemoryStream (); 
  SqlConnection conn = new SqlConnection (@ "Server = shoutor \ mydb; database = northwind; uid = sa; Pwd = shoutor"); 
  Bitmap bm = null; 
  Image image = null; 
  Try 
  ( 
  Conn.Open (); 
  SqlCommand cmd = new SqlCommand ( "select photo from employees where employeeid = '" + id +"'", conn); 
  Byte [] blob = (byte []) cmd.ExecuteScalar (); 
  Stream.Write (blob, 78, blob.Length-78); 
  Bm = new Bitmap (stream); 

  Int width = 48; 
  Int height = (int) (width * ((double) bm.Height / (double) bm.Width)); 
  / / GetThumbnailImage generate thumbnails 
  Image = bm.GetThumbnailImage (width, height, null, IntPtr.Zero); 

  Context.Response.ContentType = "image / jpeg"; 

  Image.Save (context.Response.OutputStream, ImageFormat.Jpeg); 
  ) 
  Finally 
  ( 
  If (image! = Null) 
  Image.Dispose (); 
  If (bm! = Null) 
  Bm.Dispose (); 
  Stream.Close (); 
  Conn.Close (); 
  ) 
  ) 
  ) 

  / / Implementation of interface (System.Web.IHttpHandler.IsReusable) 
  Public bool IsReusable 
  ( 
  Get 
  ( 
  Return true; 
  ) 
  ) 
  ) 
  ) 

  Aggregate 

  As an extra added that the ProcessRequest also used the air-class library of easy-to-use approach to the Image.GetThumbnailImage bitmap reduced to the width of 48 pixels, while maintaining the image of the aspect ratio.    Similar technology can be used to create a database of images from other shows that the DataGrid.    The basic idea is to use a template are used to output a handle processing of HTTP <img> labels, and query strings unique identifier included in the picture of the recorded information.    After processing HTTP handle use ADO.NET to access image data bits, and the use of GDI + (Image Device Interface +) to build images. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “DataGrid shows that the use of images from the database”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.