在DataGrid中显示使用内存流的图片(转译)
2005-10-15 15:27
567 查看
下面又是一篇转译自外国网站的控件开发文章:
文章主要介绍了一个用来显示图片的自定义控件,当然在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;
namespace PAB.WebControls
下面就是具体的应用了:
下面是写到绑定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>
我们从样例数据库Northwind 中得到以下数据表Employees:
private void Page_Load(object sender, System.EventArgs e)
在该DG控件的绑定代码中我们写下列代码:
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
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流当中.
原文章出处和执行结果:请参见!
文章主要介绍了一个用来显示图片的自定义控件,当然在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;
namespace PAB.WebControls
下面就是具体的应用了:
下面是写到绑定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>
我们从样例数据库Northwind 中得到以下数据表Employees:
private void Page_Load(object sender, System.EventArgs e)
在该DG控件的绑定代码中我们写下列代码:
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
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流当中.
原文章出处和执行结果:请参见!
相关文章推荐
- 在DataGrid中显示使用内存流的图片(转译) (转)
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 【Android Training - Graphics】高效地显示Bitmap图片(Lesson 4 - 优化Bitmap的内存使用)
- ImageView使用和从内存读取图片显示
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- IOS使用scrollview显示大量图片,实现缓存,节省内存
- 解决Picasso占用内存,使用Picasso在加载超过2M左右的图片时完全不能显示图片问题(不同的手机可能不同)
- VC6中使用内存DC加载并显示JPG图片的注意事项
- jquery表格datagrid单元格显示图片及分页使用
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 使用ViwePager显示图片时如何防止内存泄露。
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- VC6中使用内存DC加载并显示JPG图片的注意事项
- Android手机开发:ImageView使用和从内存读取图片显示
- WPF中Datagrid其中一列使用图片显示
- 图片的循环显示(图像的使用,线程)
- 生成二维码图片并且使用BASE64编码显示到前端页面
- 使用TCPMP解码显示JPEG图片WINCE5.0
- 在VC中使用CImage转换图片格式以及显示
- HTML5 之 FileReader 的使用 (二) (网页上图片拖拽并且预显示可在这里学到) [转载]