C# 将带有实体框架的数据库中的图像加载到gridview中
我已经用实体框架从标准Northwind数据库创建了一个模型。 现在我想用类别列表填充Gridview,但在该类别中,实体(表)也是一幅图。gridview中填充了Id、描述和CategoryName,但我没有在gridview中得到图片,这是二进制数据 有人知道这个问题的解决方案吗C# 将带有实体框架的数据库中的图像加载到gridview中,c#,asp.net,entity-framework,gridview,webforms,C#,Asp.net,Entity Framework,Gridview,Webforms,我已经用实体框架从标准Northwind数据库创建了一个模型。 现在我想用类别列表填充Gridview,但在该类别中,实体(表)也是一幅图。gridview中填充了Id、描述和CategoryName,但我没有在gridview中得到图片,这是二进制数据 有人知道这个问题的解决方案吗 谢谢。你可以这样做 您必须添加新的通用处理程序------右键单击解决方案资源管理器并添加新的通用处理程序,并将其命名为“ImageHandler.ashx” 注意:这是关于如何从数据库加载图像并在gridview
谢谢。你可以这样做 您必须添加新的通用处理程序------右键单击解决方案资源管理器并添加新的通用处理程序,并将其命名为“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;
}
}
}
使用制度;
使用System.Web;
使用System.IO;
使用系统、绘图、成像;
使用System.Collections.Generic;
使用System.Linq;
公共类ImageHandler:IHttpHandler{
公共void ProcessRequest(HttpContext上下文)
{
HttpRequest req=context.Request;
//字符串categoryID=“1”;
string categoryID=req.QueryString[“categoryID”].ToString();
//获取有关指定类别的信息
NorthwindDataContext db=新的NorthwindDataContext();
var category=来自数据库类别中的c
其中Convert.ToInt32(c.CategoryID)=Convert.ToInt32(CategoryID)
选择c.图片;
int len=category.First().Length;
//输出二进制数据
//但是首先我们需要去掉OLE头
头部长度=78;
int strippedImageLength=len-OleHeaderLength;
字节[]imagdata=新字节[strippedImageLength];
Copy(category.First().ToArray(),OleHeaderLength,imagdata,0,strippedImageLength);
如果((imagdata)!=null)
{
MemoryStream m=新的MemoryStream(imagdata);
System.Drawing.Image Image=System.Drawing.Image.FromStream(m);
image.Save(context.Response.OutputStream,ImageFormat.Jpeg);
}
}
公共布尔可重用{
得到{
返回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>
我希望它能帮助您……它正在做您要求它做的事情,您很可能没有得到实际的图像,或者您没有告诉gridview渲染图像。你在使用WPF还是Winforms?有没有试过谷歌?关于GridView上的图像有很多文章。EF作为加载字节数组的一种方式只是一个细节。这是一个重复:如果使用EF加载一个具有byte[]类型成员的类实际上没有太大区别。是的,我使用了谷歌。大多数示例都不使用EF,因为我有一个具有4个属性的实体类别,我不知道如何将二进制数据转换为图像文件。顺便说一句,这是一个asp.net webforms。一个带有自动生成collumns的gridview,后面的代码中有一个数据绑定。