从数据库检索屏幕截图时出现问题
我有一堆屏幕截图和一些屏幕截图元数据,我试图在 ASP.NET MVC 3 Web 应用程序中显示,我试图从数据库中检索数据,但出现此错误:
LINQ to Entities 无法识别方法“System.Drawing.Image” ByteArrayToImage(Byte[])'方法,并且该方法无法翻译 到存储表达式中。
这是我的代码:
var screenshotData = (from screenshots in db.screenshots
where screenshots.projects_ID == projectID
select new ImageInformation
{
ID = screenshots.id,
Language = screenshots.language,
Screenshot = Utility.ByteArrayToImage(screenshots.screen_shot),
ProjectID = screenshots.projects_ID
});
foreach (ImageInformation info in screenshotData)
{
this.Add(info);
}
ImageInformation 只是一个简单的类,其中包含存储的信息的定义(ID、语言、屏幕截图、项目ID)。
这是我的 ByteArrayToImage 函数:
public static Image ByteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
谁能告诉我为什么在运行此代码时收到此错误?
谢谢。
I've got a bunch of screenshots and some screenshot meta data I'm trying to display in an ASP.NET MVC 3 web application, I'm trying to retrieve the data from my databse but I get this error:
LINQ to Entities does not recognize the method 'System.Drawing.Image
ByteArrayToImage(Byte[])' method, and this method cannot be translated
into a store expression.
Here's my code:
var screenshotData = (from screenshots in db.screenshots
where screenshots.projects_ID == projectID
select new ImageInformation
{
ID = screenshots.id,
Language = screenshots.language,
Screenshot = Utility.ByteArrayToImage(screenshots.screen_shot),
ProjectID = screenshots.projects_ID
});
foreach (ImageInformation info in screenshotData)
{
this.Add(info);
}
ImageInformation is just a simple class that contains the defintion the information stored (ID, Language, Screenshot, ProjectID).
Here's my ByteArrayToImage function:
public static Image ByteArrayToImage(byte[] byteArrayIn)
{
using (MemoryStream ms = new MemoryStream(byteArrayIn))
{
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
Can anybody tell me why I receive this error when this code runs?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是因为,使用 LINQ-to-Entities,代码被转换为服务器端查询,但在这种情况下它无法做到这一点。我认为您不能将这样的客户端代码直接与 L2E 混合。
我怀疑在从数据库检索数据后,您将必须进行从字节到图像的转换作为一个独特的步骤。
I think it's because, with LINQ-to-Entities, the code is turned into server-side query and it can't do that in this case. I don't think you can mix client-side code like this directly with L2E.
I would suspect you will have to do the conversion from byte->image after you've retrieved the data from the database as a distinct step.
您无法在 LINQ to Entities 查询中执行该功能...一个选项:
1)在您正在实例化的对象(ImageInformation)上有一个 byte[] 属性,并将其中的数据与另一个属性一起复制以读取图像来自此 ImageInformation 对象。
You can't do the function in a LINQ to Entities query... one option:
1) have a byte[] property on the object you are instantiating (ImageInformation) and copy the data in there along with another propery to read the image from this ImageInformation object.