Invalid Byte Array Conversion in WCF Rest Service for Jpeg Image for Signature Pad

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Invalid Byte Array Conversion in WCF Rest Service for Jpeg Image for Signature Pad

venkatesh
Hi all,

We are trying to implement the signature capture in Xamarin Android by using the Xamarin Signature capture component. We need to save the signature image in database by using WCF REST Service(POST Method).

We have converted image(JPEG) into Byte array and sent the same as stream to WCF REST Service.

In WCF REST Service, the stream is received and we tried to convert the Byte array from stream into Image. But it shows conversion failed error as "Parameter is not valid".

Kindly help me to resolve the issue. find below my sample code.

Xamarin android (Client Side) coding part:

async void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (signature.IsBlank)
            {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.SetMessage("No signature to save.");
                alert.SetNeutralButton("Okay", delegate { });
                alert.Create().Show();
            }
            BitmapImage = signature.GetImage(); //Here getting signature pad image
            using (var memstream = new MemoryStream())
            {
                BitmapImage.Compress(Bitmap.CompressFormat.Jpeg, 0, memstream);
                string test = await DataAccess.ImageUpload(task, Application_Server_Url, memstream);
            }
        }
        catch (Exception ex){ }
    }
  //Database class to send server with image
    public async Task<String> ImageuploadFromClient<T>(T item, String ServiceAddress, MemoryStream stream) where T : BusinessLayer.Contracts.IBusinessEntity, new()
    {
        try
        {
            OutputString = string.Empty;
            byte[] fileToSend = new byte[16 * 1024];
            var request = HttpWebRequest.Create(ServiceAddress);
            request.Method = "POST"; request.ContentType = "image/jpeg";
            Stream reqStream = request.GetRequestStream();
            for (int i = 0; i < fileToSend.Length; i++) { fileToSend[i] = (byte)('a' + (i % 26)); }
            reqStream.Write(fileToSend, 0, fileToSend.Length); reqStream.Close();
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                { var content = reader.ReadToEnd(); if (content != "") { OutputString = (string)Newtonsoft.Json.JsonConvert.DeserializeObject(content); } }
            }
        }
        catch (Exception e) { OutputString = "ErrorMsg"; } return OutputString;
    }

Sample WCF REST Service Function:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest
)]
public String TestStream(Stream streamImg)
{
    try
    {
        byte[] ds = ReadFully(streamImg);
        String loginBased = InsertImageInDatabaseServer(ds); //Here the Byte Array saved in database server. But the same byte array to image conversion shows error while retrieving
        // For this reason i have checked to save the image in local machine but shows conversion error.
        MemoryStream memStream = new MemoryStream(ds);
        Image img = Image.FromStream(memStream); // Here it shows error as "Conversion failed"
        img.Save(@"d:\imgtest.jpg");
    }
    catch (Exception ex)
    { }
    return "Success";
}

public static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}
   
Reply | Threaded
Open this post in threaded view
|

Re: Invalid Byte Array Conversion in WCF Rest Service for Jpeg Image for Signature Pad

moljac
Hi


On 20140611 12:30 , venkatesh wrote:
Hi all,

We are trying to implement the signature capture in Xamarin Android by using
the Xamarin Signature capture component. We need to save the signature image
in database by using WCF REST Service(POST Method). 

We have converted image(JPEG) into Byte array and sent the same as stream to
WCF REST Service.
In mono WCF is not fully implemented / stable. It works for simple use cases, but for something more complex you'll probably hit limits.

http://www.mono-project.com/WCF_Development
http://forums.xamarin.com/discussion/370/rest-or-wcf
http://forums.xamarin.com/discussion/4557/how-to-consume-wcf-basic-service-from-xamarin-for-android
http://forums.xamarin.com/discussion/432/wcf-status-servicestack

maybe the cooperation with Microsoft will fix this in the future, but currently 2014-06 it is uncertain zone.

regards

Mel

In WCF REST Service, the stream is received and we tried to convert the Byte
array from stream into Image. But it shows conversion failed error as
"Parameter is not valid".

Kindly help me to resolve the issue. find below my sample code.

*Xamarin android (Client Side) coding part:*



*Sample WCF REST Service Function:*

   
<http://mono-for-android.1047100.n5.nabble.com/file/n5713714/ErrorStack.jpg> 



--
View this message in context: http://mono-for-android.1047100.n5.nabble.com/Invalid-Byte-Array-Conversion-in-WCF-Rest-Service-for-Jpeg-Image-for-Signature-Pad-tp5713714.html
Sent from the Mono for Android mailing list archive at Nabble.com.
_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid


_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid