David
David

Reputation: 1709

Trouble to send a picture in a wcf rest service

I'm writting a WCF Rest service. He would be use to receive and save a picture. I take and send the picture with a windows phone 7 client. I can send the picture who is parse on a stream (in the service side) but the picture won't be save because of a "badparameter".

Code use to take the picture :

private void TakePictureAction()
    {
        //Show the camera
        _camera.Show();
        //Append when camera task finished.
        _camera.Completed += delegate(object sender, PhotoResult e)
                                {
                                    _photoImage = new BitmapImage();
                                    _photoImage.SetSource(e.ChosenPhoto);
                                };
    }

Code use to send the picture (with rest sharp):

var client = new RestClient("http://127.0.0.1:81/Service.svc");
var request = new RestRequest("/uploadImage", Method.POST);
request.AddBody(_photoImage);
try
{
    client.ExecuteAsync(request, response =>
                                 {
                                    //Do stuff
                                 });
}
catch (Exception ex)
{
    MessageBox.Show("error");
}

And code on the serveur side :

public void UploadImage(Stream fileStream)
{
    if (fileStream != null)
    {
        string filePath = @"c:\";
        using (FileStream fileToUpload = new FileStream(filePath + "ok.bmp", FileMode.Create))
        {
            byte[] byteArray = new byte[10000];
            int bytesRead = 0;
            do
            {
                bytesRead = fileStream.Read(byteArray, 0, byteArray.Length);
                if (bytesRead > 0)
                {
                    fileToUpload.Write(byteArray, 0, bytesRead);
                }
            } while (bytesRead > 0);
            using (MemoryStream ms = new MemoryStream(byteArray))
            {
                Bitmap img = (Bitmap)Image.FromStream(ms);
                img.Save(@"c:\img.bmp");
            }
        }
    }
}

My Service method

 [OperationContract]
        [WebInvoke(UriTemplate = "/uploadImage", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
        void UploadImage(Stream img);

Where am I wrong ?

Upvotes: 0

Views: 1198

Answers (3)

David
David

Reputation: 1709

The problem comes from my web service. I had to maximize the size for the message (to be able to send the complet picture.). I did so by using this stackoverflow post

Upvotes: 0

Pedro Lamas
Pedro Lamas

Reputation: 7243

Windows Phone saves pictures in Jpeg format, so the problem here is most likely because you are saving the file with a ".bmp" (bitmap) extension on the server, thus the "bad format" error you are getting!

Upvotes: 0

Rajesh
Rajesh

Reputation: 7876

My Service method is defined as below:

[WebInvoke]
string PostImage(Stream stream);

Implementation of service method as shown below:

public string PostImage(Stream stream)
        {
            var ms = new MemoryStream();
            stream.CopyTo(ms);
            var streamBytes = ms.ToArray();            
            FileStream f = new FileStream("C:\\temp\\sample.jpg", FileMode.OpenOrCreate);
            f.Write(streamBytes, 0, streamBytes.Length);
            f.Close();
            ms.Close();
            stream.Close();
            return "Recieved the image on server";
        }

Now from my client i upload a picture as shown :

Image image = Image.FromFile("C:\\Sample.png"); // NOTE : Replace your photo location from the camera here
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] imageArray = ms.ToArray();
ms.Close();

var client = new RestClient();            
client.BaseUrl = "http://localhost/Service/Service1.svc";
var request = new RestRequest(method) { DateFormat = DataFormat.Xml.ToString(), Resource = "/PostImage" };
request.AddFile("stream", image, "Array.png");
var response = client.Execute(request);
string responseString;
if (response.StatusCode == HttpStatusCode.OK)
{
     responseString = HttpUtility.HtmlDecode(response.Content);
}

Upvotes: 1

Related Questions