I don’t know how to use the method of converting images in Xamarin.forms.

Asked

Viewed 370 times

0

I’m developing an app Xamarin.Forms and I’m having difficulty understanding how to fill the parameter to convert an image into byte array, I searched a lot looking for some solution, I found this method to convert to byte array.

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 the shampoo:

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

But I’m not knowing how to fill the parameters, I couldn’t find a place or a way to do it. 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’t find the solution.

4 answers

0

Guys I found another way to convert image in byte array that solved my problem. But still, if anyone can explain to me how to use the method I’ve shown, I appreciate it. That 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;
    }
  • This can all be reduced to one System.IO.File.ReadAllBytes(filepath);.

0

Let’s analyze this method. There are several defects in it, I will explain one by one.

  1. The parameters of the method culture, parameter and targetType were never used.

  2. Since you want to convert an image to an array of bytes, the main input of the method should be an image. Why is it an object, described in the parameter value?

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

  4. In the following line, this type of conversion there is no.

    byte[] imageAsBytes = (byte[])value; // você não pode converter um object
                                         // para byte[] diretamente
    
  5. 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 . NET Framework, it can inherit any type, up to the ones you do not want to pass as parameter.


Let’s fix it, you want to convert an image to byte[], right? So let’s do it the right way.

// 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();
   }
}

How the memory stream was created in a block using, it is automatically discarded after its use.

0

You are right, I found it strange even to have to pass an object type to transform into byte array being that the return is object. I saw someone using this method, I think he should be using this method in the Databinding of the shampoo. This solution you gave me actually works on. Net (System.Drawing.Image), but it seems that there is no way to convert when we are in Xamarin because it is an image Xamarin.forms.image and gives me an error saying that there is no way I can transform Drawing.image into Forms.image. But in that time I also found another solution guys. Now by the file. I also turned it into Base64.

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

0

Good morning, guys. I’m trying to convert image to bytes and bytes for image.. My book registration , will need to register the image of the book cover in sqlite.

// 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(); } 
}

The problem that this solution does not work directly in the shared project, only works in the native Android project.

Then with this, I created a service dependecy and tried to pass the image as parameter to be solved in the android project and consequently return the byte[]’s... But it is an error, because I am passing as parameter a Xamarinforms.Image and Android expects to receive System.Drawing.Image. What to do in this case?

Convert a Xamarin.Forms.Image object to System.Drawing.Image?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.