Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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..

C#
[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.


Please tell me the correct way to save the image with actual size

Thanks in advance
Posted
Updated 15-Sep-13 22:49pm
v2
Comments
[no name] 16-Sep-13 6:13am    
I think,
saved image format is jpeg. Its takes extra kb's. So once change image format and see!.
Thank u.
_Satya_ 16-Sep-13 8:03am    
No dude.. if i change that 500000 to 100000 then it is taking 10kb only. The problem was solved my self.
here i have wrote my code....

public string RecieveImage(Stream ImageStream)
FileStream fileToupload = new FileStream(@"C:\desktop\wcfUploadImage.jpeg", FileMode.OpenOrCreate);
byte[] bytearray = new byte[5242880];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = ImageStream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);

fileToupload.Write(bytearray, 0, totalBytesRead);
fileToupload.Close();
fileToupload.Dispose();
}

1 solution

here i have solved my self

C#
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "UploadImage")]
public string RecieveImage(Stream ImageStream)
FileStream fileToupload = new FileStream(@"C:\desktop\wcfUploadImage.jpeg", FileMode.OpenOrCreate);
                byte[] bytearray = new byte[5242880];
                int bytesRead, totalBytesRead = 0;
                do
                {
                    bytesRead = ImageStream.Read(bytearray, 0, bytearray.Length);
                    totalBytesRead += bytesRead;
                } while (bytesRead > 0);

                fileToupload.Write(bytearray, 0, totalBytesRead);
                fileToupload.Close();
                fileToupload.Dispose();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900