Search code examples
c#androidasp.netarraysmemorystream

Sending image with byte array convertion, from java to c#


I am trying to send a .jpg file which is on my android device to my server computer.

To do this, I am converting the picture into a byte array by a java android application, and sending it as an argument to my server computer. I`m doing this by a web service call.

The first function is edited:

public static byte[] ImageConvertion(){

    File inputFile = new File("/storage/emulated/0/IFSpictures/icon-si_strapclamp.jpg");
    byte[] data;

    try{
        FileInputStream input = new FileInputStream(inputFile);
        ByteArrayOutputStream output = new ByteArrayOutputStream ();

        byte[] buffer = new byte[65535];

        int l;

        while ((l = input.read(buffer)) > 0)
            output.write (buffer, 0, l);

        input.close();
        output.close();

        data = output.toByteArray();
        return data;


    } catch (IOException e) {
        System.err.println(e);
        data=null;
    }
    return data;

}

My web-service is written in ASP.NET (C#) language, and there is a function that takes the byte array as an argument and converts it back into an image on server computer.

[WebMethod]
public void ByteArrayToPicture(byte[] imageData)
{
    using (var ms = new MemoryStream(imageData))
    {
        Image image = Image.FromStream(ms);
        image.Save(@"C:\newImage.jpg");
    }
}

However, I couldn`t do it because of the web-service side. I have debugged it that and it seems that the problem is because of the Image.FromStream() function.

I definitely don`t have any problems with passing the arguments. I think either, the language conflict or the conversion image to byte and vice-verse may be leading the problem. Does anyone has any idea or see something wrong?

I muchly appropriate any help.

Thanks.


Solution

  • sorry for my incomplete question, however I want to give some tips whoever is trying to do the same thing.

    If anyone is trying to send an image to a server and both side has different platforms, then do not convert the image into byte array!

    The reason is, in my case the image which is converted into byte array on Java differs from the byte array on C#. Therefore according to my research it is not possible to gather the image on the server side. The byte array created on Java wont have the right format on C#.

    Hence anyone wants data transferring from one language to another, use Base64 encoding. Convert the image into Base64 string on one side and send it as string to the other language. Since Base64 format is same on every language there wont be any problem to reproduce it.

    I sold the problem with the following codes:

    Bitmap ourbitmap = BitmapFactory.decodeStream(imageStream, null, options);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    ourbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);   
    byte[] b = baos.toByteArray(); 
    test = Base64.encodeToString(b, Base64.DEFAULT); 
    

    This is the code where I get the image and convert it into Base64 string on Java android application,

    byte[] imageBytes = Convert.FromBase64String(Base64ImageData); 
    MemoryStream ms = new MemoryStream(imageBytes, 0,
    imageBytes.Length);
    
    ms.Write(imageBytes, 0, imageBytes.Length);
    Image image = Image.FromStream(ms, true);
    image.Save(@"D:\tmpImage.jpg");
    

    The code above takes the Base64 type string and converts back into an image. This is written in C#.