将具有实体框架的数据库中的图像加载到gridview中

时间:2011-10-28 16:14:51

标签: c# asp.net entity-framework gridview webforms

我使用实体框架从标准Northwind数据库创建了一个模型。 现在我想用一个类别列表填充Gridview,但在该类别中,实体(表)是一个collumn Picture。 gridview充满了Id,Description和CategoryName,但我没有得到gridview collumn中的图片,这是二进制数据。

任何人都知道这方面的解决方案吗?

感谢。

1 个答案:

答案 0 :(得分:0)

你可以这样做......

你必须添加新的通用处理程序-------右键单击解决方案资源管理器并添加新的通用处理程序并将其命名为“ImageHandler.ashx”

注意:这只是关于如何从数据库加载图像并在gridview中显示

的示例

这是imagehandler.ashx中的代码

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

using System;
using System.Web;
using System.IO;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Linq;

public class ImageHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context)
    {
        HttpRequest req = context.Request;          
        // string categoryID = "1";          
        string categoryID = req.QueryString["CategoryID"].ToString();          
        // Get information about the specified category          
        NorthwindDataContext db = new NorthwindDataContext();          
        var category = from c in db.Categories                         
                       where Convert.ToInt32(c.CategoryID) == Convert.ToInt32(categoryID)
                       select c.Picture;          
        int len = category.First().Length;          
        // Output the binary data          
        // But first we need to strip out the OLE header          
        int OleHeaderLength = 78;          
        int strippedImageLength = len - OleHeaderLength;          
        byte[] imagdata = new byte[strippedImageLength];          
        Array.Copy(category.First().ToArray(), OleHeaderLength, imagdata, 0, strippedImageLength);          
        if ((imagdata) != null)          
        {              
            MemoryStream m = new MemoryStream(imagdata);              
            System.Drawing.Image image = System.Drawing.Image.FromStream(m);              
            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);          
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

并在Default.aspx页面中添加新的Gridview控件并使用SQLDatasource控件绑定它

<div>

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AutoGenerateColumns="False" DataKeyNames="CategoryID"
        DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333"
        GridLines="None">
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
                InsertVisible="False" ReadOnly="True" SortExpression="CategoryID" />
            <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"
                SortExpression="CategoryName" />
            <asp:BoundField DataField="Description" HeaderText="Description"
                SortExpression="Description" />
            <asp:TemplateField HeaderText="Picture" SortExpression="Picture">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Picture") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%#"ImageHandler.ashx?CategoryID="+ Eval("CategoryID")  %>'/>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT * FROM [Categories]"></asp:SqlDataSource>

</div>

我希望它会帮助你......