0

I want to get the value of an Image column from my database table.

I used this code before, but now I want to call a function to query it from another table.

MemoryStream ms = new MemoryStream(obj.photo, 0, obj.photo.Length);
ms.Position = 0; // this is important          
pbFarmer.Image = Image.FromStream(ms, true);  

This code throws an error:

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Drawing.Image'. An explicit conversion exists

public Image getphoto(int id)
{
    using (simisdbEntities db = new simisdbEntities())
    {
        // return db.FarmerImages.Where(u => u.id == id).Select(u => u.photo);
        // return db.FarmerImages.SqlQuery("SELECT photo FROM dbo.FarmersImages where id =" + id);

        // return db.FarmerImages.First(a => a.id == id);

        var img = from p in db.FarmerImages 
                  where p.id == id 
                  select Image.FromStream(new MemoryStream(p.photo.ToArray()));
        return img;
    }
}
1
  • Since you already have the id, would this work? var img = db.FarmerImage.Find(id).'propertyname' ... propertyname would be the property of the found entity. Commented Sep 28, 2016 at 3:31

1 Answer 1

1

The problem is that your linq query is returning IQueryable<Image> instead of a single Image object so you can not return it from your method that returns only an Image. You need to somehow get only one value.

You could go with something like this:

using (simisdbEntities db = new simisdbEntities())
{
    IQueryable<Image> img = from p in db.FarmerImages 
                            where p.id == id 
                            select Image.FromStream(new MemoryStream(p.photo.ToArray()));
    return img.FirstOrDefault();
}

But then you'll get a run time exception:

Additional information: LINQ to Entities does not recognize the method 'System.Drawing.Image FromStream(System.IO.Stream)' method, and this method cannot be translated into a store expression.

I'd first get the FarmerImage metadata from the database, handle the possible exceptions - eg. image does not exist - and then return the Image object:

UPDATE: After your comments below, I'd suggest you to also check for the case when you image exists in database, but the photo array is null.

public Image getphoto(int id)
{
    using (var db = new simisdbEntities())
    {
        var imgMetadata = db
                .FarmerImages
                .FirstOrDefault(p => p.id == id);

        //handle the case when image does not exist.
        if (imgMetadata == null)
            throw new Exception("Image not found!");

        //update: check for the case when a FarmerImage exists in database but the photo array is null
        if (image.photo == null)
            throw new Exception("Image not found!");

        //read the image bytes into an Image object.
        var img = Image.FromStream(new MemoryStream(imgMetadata.photo.ToArray()));

        return img;
    }
}

Remember that you could call the method and handle the exceptions - I'd do it this way - or you could return null instead of throwing an exception and the handle the case when the returned image is null.

First case: expecting an exception

public void CallerHanderOrMethod()
{
    try{
        var img = farmerManager.getPhoto(farmerId);
    }
    catch(Exception ex) //consider throwing a more specific exception.
    {
        //load the default silhouette image into the picture box. 
    }
}

Second case: expecting a null image.

public void CallerHanderOrMethod()
{
    var img = farmerManager.getPhoto(farmerId);
    if (img == null) 
    {
        //load the default silhouette image into the picture box. 
    }
}

This should do the work.

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for taking your time to assist me.
Hi Also before i used to fill in the picture box if there was no image like so: pbFarmer.Image = Properties.Resources.male_silhouette; How can i add that in the getphoto function if there is no image found i want to show this default
@ivias, glad to help! You could return null from your method instead of throwing an exception and handle the case where you invoked getphoto() so if the image is null you could load your default silhouette. Besides, you should check in your method not only if the image does not exits but also if mage.photo array is null. I've updated the answer. Take a look!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.