[英]CS1061: Retrieve image from DB to PictureBox control
我有包含pictureBox窗體的winform,我想使用LINQ從數據庫中檢索到該控件的img。
這是編譯時的錯誤,
CS1061:IQueryable不包含Image的定義,並且找不到找到接受IQueryable類型的第一個參數的擴展方法Image(是否缺少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();
}
}
您的問題是ImageToByteArray
接受ImageToByteArray
的圖像,但是您使用了以下方法:
ByteArrayToImage(q.Image.ToArray());
該函數將此函數與可變數組一起使用。
從sql獲取q之后(如果q是字節數組)
var q = (from x in context.Products
where x.Pro_ID == 1
select x.Image).FirstOrDefault();
Image image1 = byteArrayToImage((byte[])q);
和方法
public Image byteArrayToImage(byte[] byteArrayIn)
{
using (var ms = new MemoryStream(byteArrayIn))
{
return Image.FromStream(ms);
}
}
不要忘記使用名稱空間System.Drawing.Image
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.