Question

i have a wcf rest which is recieving an image from andriod device and save into public shared folder.

everything is working well but while saving the image file(actual image size is 15kb) into my shared folder it is saving with 489kb.

Any image file is saving with 489kb only. I found the problem why it is saving like this..

this is my code..

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "UploadImage")]
public string RecieveImage(Stream ImageStream)
{
    try
    {
        byte[] buffer = new byte[500000];
        ImageStream.Read(buffer, 0, 500000);
        FileStream f = new FileStream(@"c:\desktop\wcfUploadImage.jpeg", FileMode.OpenOrCreate);               
        f.Write(buffer, 0, buffer.Length);
        f.Close();
        f.Dispose();
    }
    catch (Exception ex)
    {
        throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
    }

    return "Successsfully recieved.";
}

Because of byte[500000] only i am saving the image with 489kb. i am getting an error if i replaced 500000 with ImageStream.length.

What is the correct way to save the image with actual size?

Was it helpful?

Solution

Stream's Read method returns the number of bytes it has actually read as Int32. Use that value.

Note that if it returns exact number of bytes you asked for (eg. 500000), than it is highly likely that you need to read the stream again: there is still unread data.

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