Question

Im building a C#, WPF and Android application, mostly for training, but either way, the issue that I have is the follow:

When I execute the followed code WPF app, it saves the desire file from the disk to the desktop, but the problem comes when I try to do the same from the Android application, I do get connected to Console application, but the byte[] apparently are not the same format, maybe I miss something, or I'm going something wrong.


Convert Image to byte array function: C#

 private static byte[] ImageToByteArraybyMemoryStream(Image image) {
        MemoryStream ms = new MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        return ms.ToArray();
    }

Save Image to Disk method C#

public static void ByteArrayToImageFilebyMemoryStream(byte[] imageByte) {
   try {

        MemoryStream ms = new MemoryStream(imageByte);
        Image image = Image.FromStream(ms);
        image.Save(@"C:\Users\jycr753\Desktop\imageTest1.png");
   }
}

Convert to byte[] and send from Android Application.

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
 bitmapImage.compress(Bitmap.CompressFormat.PNG, 70, stream);
 byteArray = stream.toByteArray();
 File fl = new File( imagePath);
 FileInputStream fi = new FileInputStream( fl );
 BufferedInputStream bi = new BufferedInputStream( fi );
 bi.read( byteArray, 0, byteArray.length );
 OutputStream outs = sock.getOutputStream();
 outs.write( byteArray, 0, byteArray.length );
 outs.flush();

Image from Console Application enter image description here


Debugger in Save file method screen shot

enter image description here

Manifest Permitions

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

byte code Is there any issue if the C# application expect a ASCII encoding?


Any help of point me in the right direction it will be great, if there is the need to put more code, just ask :)

thanks community..

Was it helpful?

Solution

It is not visible from the code provided how exactly does the C# open the other end of the socket stream and how does it read.

Very usual problem with reading socket streams is that you don't know when all data was received.

Network layer may signal there is some data multiple times. Without some protocol including marks of the meaning data packet started and data packet ended it is not possible to determine when whole PNG arrived, unless you use some all-or-nothing protocol - e.g. HTTP GET

Also the exact binary protocol must be agreed by both sides, e.g. little or big endian

Need more data to provide better answer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top