asp: BoundColumn> columns> asp: datagrid> font> form> body> html>
<% @ Page language = "c #" codebehind = "bindimage.aspx.cs" autoeventwireup = "false" inherits = "showimage.bindimage"%> P> Using system;
Using system.data;
Using system.drawing;
Using system.web; using system.data.sqlclient;
Namespace showImage {///
/// Bindimg's summary description. ///
Public class bindImage: system.web.ui.page
{
Protected system.Web.ui.WebControls.DataGrid mydatagrid;
Private Void Page_Load (Object Sender, System.EventArgs E)
{
/ / Place the user code here to initialize the page IF (! Page.ispostback)
{
SqlConnection conn = new sqlconnection (@ "server = shoutor / mydb; database = northwind; uid = sa; pwd = shoutor");
Try
{
Cn.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: This call is necessary for the ASP.NET Web Form Designer. //
InitializationComponent ();
Base.onit (E);
}
// // // Designer supports the required method - do not use the code editor to modify the // / this method. ///
Private vidinitiRizeComponent ()
{
This.Load = New System.EventHandler (this.page_load);
}
#ndregion
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----------------------
<% @ WebHandler Language = "C #" class = "showimage.getimage" codebehind = "getimage.ashx.cs"%>
//------------------------------- GetImage.ashx.cs ----------- -----------------------
Using system.data; using system.data.sqlclient; using system.drawing; using system.drawing.imaging; using system.io; namespace showimage {/////
/// Getimg's summary description. ///
Public Class GetImage: IHTTPHANDLER
{
Public void processRequest (httpcontext context)
{
String id = (string) context.request ["id"];
IF (id! = null)
{
MemoryStream Stream = new memorystream ();
SqlConnection conn = new sqlconnection (@ "server =; dataBase =; uid =; pwd =");
Bitmap BM = NULL;
Image image = NULL;
Try
{
Cn.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 generated thumbnail 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 ();}}} public bool isreusable {get {return true;}}}}
ProcessRequest uses the easy-to-use image.getthumbnailimage method of the nachelf class library to narrow the bitmap to 48 pixels, while maintaining the aspect ratio of the image. Similar techniques can be used to create a DataGrid displayed from other database images. The basic idea is to use the template column to output a tab that references an HTTP handler and contains information that uniquely identifies the records in the query string. After that, the HTTP handle uses ADO.NET to get the image data bit and use GDI (Image Device Interface ) to build image.