我有包含pictureBox表单的winforms,我想使用LINQ从DB检索img到该控件。
这是编译时的错误,
CS1061:IQueryable不包含Image的定义,也没有扩展方法可以找到接受IQueryable类型的第一个参数的图像(你是否缺少using指令或汇编引用?)
private void pictureBox1_Click(object sender, EventArgs e)
{
// Get as single image from the database
var q = from image in context.Products
where image.Pro_ID == 1
select image;
// Convert the byte[] to an System.Drawing.Image
img.Image = ByteArrayToImage(q.Image.ToArray());
}
private byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
using (MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
}
答案 0 :(得分:0)
您的问题是inputView
接受图片为varieble但您使用了这个:
ImageToByteArray
使用此函数将数组作为varieble。
从sql获取q后(如果q是字节数组)
ByteArrayToImage(q.Image.ToArray());
和方法
var q = (from x in context.Products
where x.Pro_ID == 1
select x.Image).FirstOrDefault();
Image image1 = byteArrayToImage((byte[])q);
不要忘记使用名称空间System.Drawing.Image