将图像从Android上传到WCF C#/。净

时间:2013-07-02 15:47:42

标签: c# android .net web-services wcf-rest

早上好,我需要将存储卡中的图像从单元发送到我在wcf / C#中按顺序进行的网络服务,不知道C#是公司的要求,我甚至可以为WCF发送Strem但是它很难转换并转换成Bitmap或其他东西。

关注我在wcf中制作帖子图片的android代码:

/**
 * 
 * Método responsável por enviar imagem para o servidor
 *
 * @param String caminho
 * @author Douglas Costa <douglas.cst90@gmail.com.br>
 * @since 01/07/2013 18:27:49
 * @version 1.0
 */
public static void upload(String caminho){

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.0.205:8070/Service/uploadImagem");
    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    File file = new File(caminho);
    //This is the new shit to deal with MIME
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("image", new FileBody(file, "image/jpeg"));
    httppost.setEntity(entity);

    try {
        String responseString = httpclient.execute(httppost, responseHandler);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这是我在C#中的WCF方法的帖子,它接收来自图像的流:

/// Método POST que recebe um Stream do Android
    /// </summary>
    /// <param name="imagem"></param>
    /// <returns></returns>
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "uploadImagem")]
    public Bitmap uploadImagem(Stream imagem)
    {

        try
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = imagem.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                buffer = ms.ToArray();
            }

            using (MemoryStream mStream = new MemoryStream())
            {
                mStream.Write(buffer, 0, buffer.Length);

                Bitmap bm = new Bitmap(mStream);
                return bm;
            }
        }
        catch (Exception)
        {
            return null;
        }
    }

已经尝试了几种转换流的方法,也不知道我发送的方式是否正确,但我有一个类似的方法,但在JAVA webservice中有效。

感谢有人可以帮我解决C#。

对不起英文错误,谢谢。