Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

i try to display image,not getting right.can some1 find out, what's wrong in my codding ?

$
0
0

hello i m new to xamarin
i m trying to fetch images from sql server database into xamarin project.
i explain u below the steps i followed to fetch the data from web server.....

i stored image into sql server database field name is Img and datatype is varbinary(max).
i created webapi to fetch the data form sql database

i have used byte[] property for image in class (class Name is Products) as below

public int pro_Id {get;set;}
public String ProductName {get;set;}
public String Description {get;set;}
public byte[] img {get;set;}

i am reading img field using sqldatareader (Reader.Read()) as below

while (Reader.Read()) in MasterContorller
{
product.Pro_id = Convert.ToInt32(Reader["Pro_id"]);
product.ProductName = Reader["ProductName"].ToString();
product.Description = Reader["Description"].ToString();
Products.img = (byte[]).Reader("img");
}

lets talk about what the steps i followed in my xamarin project to fetch the data

i used url (i.e - http:/xyz.com/api/master/getproducts)to retrieve the data from web server using json converter.
it fetches me data (productname,Description) in listview... but not retrieving the image

below the class (Product) i created in xamarin project....

public int pro_Id {get;set;}
public String ProductName {get;set;}
public String Description {get;set;}
public byte[] img {get;set;}

i bind them as below in xaml

<ContentPage.Resources>
    <ResourceDictionary>
        <conv:ByteArrayToImageSourceConverter x:Key="bic" />
    </ResourceDictionary>
</ContentPage.Resources>

it retrieves ProductName and Description well it leaves the space for image but not displaying it.

this is the code below i use to convert byte into image

public class ByteArrayToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{

        ImageSource retSource = null;
        if (value != null)
        {
            byte[] imageAsBytes = (byte[])value;
            retSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
        }
        return retSource;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

i m struggling a lot with this.... i don't know where i m making the mistake
i tried all possible ways to find out the issue but unable to do so...
if anyone can help me find out the actual issue...
thanks


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>