How to send and receive image by json?

Asked

Viewed 2,506 times

1

i made an app in Xamarin that the person will have to sign and I need to send this image to the server If possible leave the code to perform such task

  • One of the possible strategies is to use Base64 and turn the image bytes into text. Then the server needs to know this and remove the Base64 transformation.

  • How I accomplish such a task, the biggest problem is to disconnect

  • https://answall.com/q/20683/64969

  • Possible duplicate of How to decode an image in Base64

2 answers

1

Basically, assuming you have the image stored locally, or simply have the byte array of it:

byte[] bytes = System.IO.File.ReadAllBytes(FileName);
String strImage = Convert.ToBase64String(b);

Then you send it to the server as a string parameter.

On the server, for you to do the reverse process:

byte[] bytes = Convert.FromBase64String(strDaImagemRecebida);

Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
    image = Image.FromStream(ms);
}

1


What I have is a stream I need to send to the server then it was like this

 Atendimento_ViewModel.Assinatura = await Pad.GetImageStreamAsync(SignatureImageFormat.Png) as  MemoryStream;
app local
byte[] imageBytes = Assinatura.ToArray();
pedido.Assinatura = Convert.ToBase64String(imageBytes);

Server

byte[] bytes = Convert.FromBase64String(pedido.Assinatura);
MemoryStream stream = new MemoryStream(bytes);
Image Image1 = Image.GetInstance(System.Drawing.Image.FromStream(stream),System.Drawing.Imaging.ImageFormat.Png);

the above code is to take the written signature and be sent to the server, beware if the intention is to send the image in a pdf the same should be . pdf

Browser other questions tagged

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