I do not know how to convert images to xamarin.forms.image

0
I am developing a xamarin.forms application and am having difficulty understanding how to fill in the parameter to convert an image to array of bytes, I searched a lot for some solution, found this method below to convert to array of bytes.

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

The image is loaded in xaml:

 <Image x:Name="fotosOcorrencia" HorizontalOptions="Center" Source="{Binding Foto}"  HeightRequest="180"/>

But I do not know how to fill in the parameters, I did not find one or the other to do that. I'm trying to do this:

 arrayByteImagem = ConvertImageInArrayByte(fotosOcorrencia, CultureInfo.CurrentCulture);

I'm very grateful that someone can help me, I know it's not difficult, but I can not find the solution.

    
asked by anonymous 15.10.2018 / 00:22

3 answers

0

I found another way to convert image to byte array that solved my problem. But even if someone knows how to use the method I showed, I appreciate it. This was the solution

public static byte[] GetPhoto(string filePath)
    {
        FileStream stream = new FileStream(
            filePath, FileMode.Open, FileAccess.Read);
        BinaryReader reader = new BinaryReader(stream);

        byte[] photo = reader.ReadBytes((int)stream.Length);

        reader.Close();
        stream.Close();

        return photo;
    }
    
16.10.2018 / 05:24
0

Let's look at this method. There are several flaws in it, I'll explain one by one.

  • The method parameters culture , parameter and targetType have never been used.

  • As you want to convert an image to a byte array, the method's main entry should be an image. Why is a object , described in the parameter value ?

  • The method output should be an array of bytes, but it also returns a object .

  • In the following line, this type of conversion does not exist .

    byte[] imageAsBytes = (byte[])value; // você não pode converter um object
                                         // para byte[] diretamente
    
  • The conversion would be wrong if you used the method as a byte[] because it returns a ImageSource :

    ImageSource retSource = null;
    ...
    return retSource;
    
  • Object is the basis of all types present in the .NET Framework, it can inherit any type, even those you do not want to pass as a parameter.

    Let's sort this out, you want to convert an image to byte [], right? So let's do it right.

    // um método que dele sai um array de bytes e entra uma imagem
    public byte[] ConvertImageInArrayByte(System.Drawing.Image value)
    {
       // aqui criamos uma memória temporária para salvar a imagem nela
       using (var ms = new MemoryStream())
       {
          // salvamos uma cópia da nossa imagem na nossa memória virtual
          value.Save(ms, ImageFormat.Jpeg);
          // retornamos os bytes da memória virtual
          return ms.ToArray();
       }
    }
    

    Since the memory stream was created in a using block, it is automatically discarded after it is used.

        
    16.10.2018 / 05:59
    0

    You're right, I found it strange even to have to pass an object type to turn into a byte array and return is an object. I've seen someone using this method, I think he should be using this method in the DataBinding of xaml. This solution that you gave me works even in .Net (System.Drawing.Image), but it looks like it has no way to convert when we are in Xamarin because it is a xamarin.forms.image image and gives me an error saying it has no way I make Drawing.image in Forms.image. But in that time I also found another solution galera. Now by file. I took advantage of it and also made it into Base64.

    var memoryStream = new MemoryStream();
    file.GetStream().CopyTo(memoryStream);
    byte[] arrayByteImagem = memoryStream.ToArray();
    string temp_inBase64 = Convert.ToBase64String(arrayByteImagem);
    
        
    17.10.2018 / 19:29