Reputation: 183
I have a database with a table named Products
which contains pictures of the products.
To store an image in memory one does
public byte[] Picture
So in my database it's a varbinary type (automaticly converted) So, what i want to do is to display in pictureBox1.Image my image from database. Something like this:
var q_pic = from p in context.Products
where p.ID == value // Id of product that i want
select new
{
p.Picture
};
and now
picutreBox1.Image = ??
cause
pictureBox1.Image = q_pic // doeasn't work
I have tried this:
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();
}
}
}
and getting this
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[]'
Upvotes: 2
Views: 3763
Reputation: 1
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);
}
Upvotes: 0
Reputation: 1
just make
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);
Upvotes: 0
Reputation: 1299
When you use LINQ, you get 'System.Data.Linq.Binary' type from database. This type has a ToArray()
method, which returns byte[]
. To you should correct you code like this:
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();
}
}
}
Upvotes: 0
Reputation: 564791
You probably want something more like:
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();
The main issues in your original were you were using an anonymous type (unnecessarily), and trying to assign to an IEnumerable<T>
, not just the picture itself. Removing the new {...
from the query solves the first issue, and using FirstOrDefault
extracts a single T
(the first) from the IEnumerable<T>
.
Given your comment, it's obvious that your query is return a byte[]
, not an image. You would need to write it as:
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();
}
}
}
Upvotes: 1
Reputation: 1714
What Reed Copsey lists will get you to the point where you have retrieved the byte[] on the Product object. To put it in the Image property on the PictureBox you will need to convert the byte array to an Image.
using (var ms = new System.IO.MemoryStream(q_pic)) {
using(var img = Image.FromStream(ms)) {
pictureBox1.Image = img;
}
}
Upvotes: 1