2

我有一个数据库,其中包含一个名为的表Products,其中包含产品的图片。

要将图像存储在内存中

public byte[] Picture

所以在我的数据库中它是一个 varbinary 类型(自动转换)所以,我想做的是在 pictureBox1.Image 中显示来自数据库的图像。像这样的东西:

 var q_pic = from p in context.Products
                         where p.ID == value // Id of product that i want 
                         select new
                         {
                             p.Picture
                         };

现在

picutreBox1.Image = ??

原因

pictureBox1.Image = q_pic // doeasn't work

我试过这个:

var bytes = from p in context.Products
                        where p.ID == value // Id of product that i want 
                        select p.Picture;
            if (bytes != null)
            {
                using (var ms = new MemoryStream(bytes))
                {
                    using (var image = Image.FromStream(ms))
                    {
                        pictureBox1.Image = (Image)image.Clone();
                    }
                }
            }

得到这个

Error   1   The best overloaded method match for 'System.IO.MemoryStream.MemoryStream(byte[])' has some invalid arguments   

Error   2   Argument 1: cannot convert from 'System.Linq.IQueryable<byte[]>' to 'byte[]'    
4

5 回答 5

1

Reed Copsey 列出的内容将使您了解已检索到 Product 对象上的 byte[]。要将其放入 PictureBox 的 Image 属性中,您需要将字节数组转换为 Image。

 using (var ms = new System.IO.MemoryStream(q_pic)) {
    using(var img = Image.FromStream(ms)) {
       pictureBox1.Image = img;
    }
 }
于 2013-11-01T19:02:51.957 回答
1

您可能想要更多类似的东西:

var q_pic = from p in context.Products
                     where p.ID == value // Id of product that i want 
                     select p.Picture;

// FirstOrDefault will return null if there were no matches
pictureBox1.Image = q_pic.FirstOrDefault();

您原来的主要问题是您使用了匿名类型(不必要地),并试图分配给IEnumerable<T>,而不仅仅是图片本身。从查询中删除解决了第一个new {...问题,并使用从.FirstOrDefaultTIEnumerable<T>


鉴于您的评论,很明显您的查询是返回 a byte[],而不是图像。你需要把它写成:

var first = context.Products.FirstOrDefault(p => p.ID == value);
if (first != null)
{
     using (var ms = new MemoryStream(first.Picture)) 
     {
         using(var image = Image.FromStream(ms)) 
         {
             pictureBox1.Image = (Image)image.Clone();
         }
     }
}
于 2013-11-01T18:56:29.947 回答
0

当您使用 LINQ 时,您会从数据库中获得“System.Data.Linq.Binary”类型。这种类型有一个ToArray()方法,它返回byte[]. 你应该更正你的代码是这样的:

var bytes = from p in context.Products
                        where p.ID == value // Id of product that i want 
                        select p.Picture;
byte[] trueBytes = bytes.ToArray();
if (trueBytes != null)
{
    using (var ms = new MemoryStream(trueBytes))
    {
        using (var image = Image.FromStream(ms))
        {
            pictureBox1.Image = (Image)image.Clone();
        }
    }
}
于 2013-11-04T02:56:30.773 回答
0

只做

byte[] imgdata;


string id = dataGridViewX1.SelectedRows[0].Cells[0].Value.ToString();
var q = db.tbl_aezas.First(c => c.id == id);
imgdata = q.image.ToArray();
System.IO.MemoryStream ms = new System.IO.MemoryStream(imgdata);
pictureBox1.Image = Image.FromStream(ms);
于 2016-07-13T08:24:30.550 回答
0
DataBonzClinicDataContext db = new DataBonzClinicDataContext();

            var tsel = (from u in db.tblsysusers 
                       where (u.pass_id==cbxUser.Text.ToString().Trim()) 
                      select u.pass_imgid).First();            

            if (tsel != null)
            {
               var imgdata = tsel.ToArray();
                System.IO.MemoryStream ms = new System.IO.MemoryStream(imgdata);
                pictureBox1.Image = Image.FromStream(ms);
            }
于 2019-10-26T16:23:51.703 回答