下面又是一篇转译自外国网站的控件开发文章:
文章主要介绍了一个用来显示图片的自定义控件,当然在DATAGRID或其它的列表控件中显示图片有很多方式,但以下这种方式让你的代码看起来更清淅,执行效率会更高.它不用在调用另一个网页,对每一个图片进行数据库查询处,它只是在绑定过程中就处理了来自数据库的数据.
以下是控件源码:
using System;
using System.Collections.Specialized;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Reflection ;
using System.ComponentModel;
![](/Images/OutliningIndicators/None.gif)
namespace PAB.WebControls
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public enum ImageType
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Gif,
Jpeg
}
![](/Images/OutliningIndicators/InBlock.gif)
public enum Persistence
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Cache,
Session
}
[Designer("PAB.WebControls.ImageControlDesigner"),
ToolboxDataAttribute("<{0}:ImageControl Runat=\"server\"></{0}:ImageControl>")]
public class ImageControl : Control
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
protected string ImageUrl;
private ImageType imageType;
[Description("Image Type")]
[Category("Data")]
[DefaultValue("Gif")]
[Browsable(true)]
public ImageType ImageType
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return imageType;
}
![](/Images/OutliningIndicators/InBlock.gif)
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
imageType=value;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private Persistence persistenceType;
![](/Images/OutliningIndicators/InBlock.gif)
[Description("Cache or Session Persistence")]
[Category("Data")]
[DefaultValue("Cache")]
[Browsable(true)]
public Persistence PersistenceType
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return persistenceType;
}
![](/Images/OutliningIndicators/InBlock.gif)
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
persistenceType=value;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private Bitmap _bitmap;
[Browsable(false)]
public Bitmap Bitmap
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(this.PersistenceType==Persistence.Session )
return (Bitmap)Context.Session[String.Concat(CreateUniqueIDString(), "Bitmap")];
else
return (Bitmap)Context.Cache[String.Concat(CreateUniqueIDString(), "Bitmap")];
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(this.PersistenceType==Persistence.Session)
Context.Session[String.Concat(CreateUniqueIDString(), "Bitmap")] = value;
else
Context.Cache[String.Concat(CreateUniqueIDString(), "Bitmap")] = value;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private string CreateUniqueIDString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string idStr=String.Empty;
string tmpId=String.Empty;
if( this.PersistenceType ==Persistence.Session )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
idStr = "__" +Context.Session.SessionID.ToString() +"_";
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(Context.Cache["idStr"]==null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmpId=Guid.NewGuid().ToString();
Context.Cache["idStr"]=tmpId;
}
idStr= "__"+Context.Cache["idStr"].ToString() +"_";
}
![](/Images/OutliningIndicators/InBlock.gif)
idStr = String.Concat(idStr, UniqueID);
idStr = String.Concat(idStr, "_");
idStr = String.Concat(idStr, Page.ToString());
idStr = String.Concat(idStr, "_");
return idStr;
}
![](/Images/OutliningIndicators/InBlock.gif)
private void ImageControl_Init(EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpRequest httpRequest = Context.Request;
HttpResponse httpResponse = Context.Response;
if (httpRequest.Params[String.Concat("ImageControl_", UniqueID)] != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
httpResponse.Clear();
if(this.ImageType==ImageType.Gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
httpResponse.ContentType = "Image/Gif";
Bitmap.Save(httpResponse.OutputStream,ImageFormat.Gif );
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
httpResponse.ContentType = "Image/Jpeg";
Bitmap.Save(httpResponse.OutputStream, ImageFormat.Jpeg);
}
httpResponse.End();
}
string str = httpRequest.Url.ToString();
if (str.IndexOf("?") == -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ImageUrl = String.Concat(str, "?ImageControl_", UniqueID, "=1");
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ImageUrl = String.Concat(str, "&ImageControl_", UniqueID, "=1");
}
}
![](/Images/OutliningIndicators/InBlock.gif)
protected override void OnInit(EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ImageControl_Init(e);
}
protected override void Render(HtmlTextWriter output)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
output.Write("<img id={0} src={1}>", this.UniqueID,ImageUrl );
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public class ImageControlDesigner: System.Web.UI.Design.ControlDesigner
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public ImageControlDesigner()
{}
public override string GetDesignTimeHtml()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetEmptyDesignTimeHtml ();
}
![](/Images/OutliningIndicators/InBlock.gif)
protected override string GetEmptyDesignTimeHtml()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return CreatePlaceHolderDesignTimeHtml( "<div>[Image is set at runtime. Place
control inside Table TD or DIV for absolute positioning.]</div>");
}
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
下面就是具体的应用了:
下面是写到绑定DG控件的HTML代码:
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 352px; POSITION: absolute; TOP: 216px"
runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="name" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate>
<cc1:ImageControl id="ImageControl1" Runat="server"></cc1:ImageControl>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
我们从样例数据库Northwind 中得到以下数据表Employees:
private void Page_Load(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
SqlConnection cn;
cn = new SqlConnection
("DATABASE=northwind;SERVER=localhost;UID=sa;");
String cmdText = "SELECT top 4 lastname as name, photo as image FROM Employees";
SqlDataAdapter da = new SqlDataAdapter(cmdText,cn);
DataSet ds=new DataSet();
da.Fill(ds);
DataTable tbl = ds.Tables[0];
DataGrid1.DataSource=tbl;
DataGrid1.DataBind();
}
![](/Images/OutliningIndicators/None.gif)
在该DG控件的绑定代码中我们写下列代码:
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
MemoryStream ms=null;
PAB.WebControls.ImageControl ctrl=null;
byte[] b=null;
if ( e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
b = (byte[])(DataBinder.Eval(e.Item.DataItem, "image"));
ms = new MemoryStream();
// 78 is the size of the OLE header for Northwind images, need to strip it off
int offset = 78;
ms.Write(b, offset, b.Length-offset);
ctrl = (PAB.WebControls.ImageControl)e.Item.FindControl("ImageControl1");
ctrl.Bitmap=(Bitmap)System.Drawing.Image.FromStream(ms);
}
}
![](/Images/OutliningIndicators/None.gif)
We need a MemoryStream for this operation. If the item being databound is a regular data display row, we cast the Item.DataItem "image" DataRowView Column to a byte array. In this particular case, Images in the Northwind Employees table were designed from MS Access, which expects a 78 byte OLE header, which we'll strip off. We write the rest of the array into our MemoryStream. Finally, we cast the e.Item.FindControl("ImageControl1") to an instance of my PAB.WebControls.ImageControl, assign the Image to it's Bitmap property, and -- voila! here's what we get:
在这个操作过程当中,我们需要MemoryStream流,如果被绑定的列是普通列的话,我们要把这个列转化为"IMAGE"格式的列.在MS ACCESS数据库中,有前78字节的OLE HEADER,这是自动加上去的,我们要把它去掉,把余下的写入到我们的MemoryStream流当中.
原文章出处和执行结果:请参见!