converting byte array from sql server table to image source Maui

easymanager easymanagersoft 96 Reputation points
2024-02-23T09:26:16.7233333+00:00

Hi,

I have been struggle for quite àsome time to find a way to retrieve byte array data from sql server and get them as image source for a content view. this is my code :> > ```csharp if (i.ImageCouv !=null)

{
			await Shell.Current.DisplayAlert("EasyManager", "Binaire existant" + i.ImageCouv.Length, "Ok");
}

byte[] imageArray = i.ImageCouv;

MemoryStream memory = new MemoryStream(imageArray);

l.MyImage.Source = ImageSource.FromStream(() => (Stream)memory);

lim.Add(l);

So could some body help me in getting an issue ? I am not really experienced in Net Maui. Lendcap

Community Center Not monitored
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. easymanager easymanagersoft 96 Reputation points
    2024-02-23T09:36:28.9833333+00:00

    Hi,

    I have been struggle for quite àsome time to find a way to retrieve byte array data from sql server and get them as image source for a content view. this is my code :

    try
    {
    	foreach (var i in prod2)
    	{
    		var l = new LieuxImDTO();
    		l.Adresse = i.Adresse;
    		l.Nom = i.Nom;
    		l.DateCreat = i.DateCreat;
    		l.Id = i.Id;
    		l.ImageCouv = i.ImageCouv;
    		l.Operateur = i.Operateur;
    		l.OwnerId = i.OwnerId;
    		l.Reference = i.Reference;
    		l.Type = i.Type;
    
    		if (i.ImageCouv !=null)
    		{
    			await Shell.Current.DisplayAlert("EasyManager", "Binaire existant" + i.ImageCouv.Length, "Ok");
    //This help me to really notice that there are data coming back from the server
    
    		}
    		byte[] imageArray = i.ImageCouv;
    		MemoryStream memory = new MemoryStream(imageArray);
    		
    		
    		l.MyImage.Source = ImageSource.FromStream(() => (Stream)memory);
    		
    		lim.Add(l);
    
    	}
    }
    catch (Exception ex)
    {
    	await Shell.Current.DisplayAlert("EasyManager", ex.Message, "Ok");
    //Here the exception is : Object reference not set to an instance of an object
    	return null;
    }
    			
    

    So could some body help me in getting an issue ? I am not really experienced in Net Maui.

    Lendcap

    0 comments No comments

  2. easymanager easymanagersoft 96 Reputation points
    2024-02-23T10:17:15.19+00:00

    Hi, the issue was in the using of Image rather than ImageSource. It is now oK. But could someone tell me now that i can display an image in the contentview How can i let the user manipulate that image which is an element in a collectionview. Thank's Lendcap

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.