任何上传图像文件都以相同的大小保存 [英] Any upload image file is saving with same size

查看:100
本文介绍了任何上传图像文件都以相同的大小保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个wcf休息,它从andriod设备接收图像并保存到公共共享文件夹。



一切正常但保存图像文件时(实际图像大小为15kb)进入我的共享文件夹,它以489kb保存。



任何图像文件仅以489kb保存。我发现了这个问题为什么会这样保存..



这是我的代码..



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.





请告诉我以实际尺寸保存图像的正确方法



先谢谢



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

Thanks in advance

推荐答案

这里我已经解决了我的问题



here i have solved my self

[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();
}


这篇关于任何上传图像文件都以相同的大小保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆