将图像从Android上传到WCF C#/.NET

本文关键字:WCF NET 图像 Android | 更新日期: 2023-09-27 18:20:54

早上好,我需要将存储卡中的图像从单元发送到我在wcf/C#中做的Web服务,我不知道该公司需要多少C#,我甚至可以发送一个用于wcf的Strem,但它在转换和转换为位图或其他方面遇到了困难。

按照我的安卓代码,使张贴图像在wcf:

/**
 * 
 * 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 Web服务中工作。

如果有人能帮我学习C#,我将不胜感激。

抱歉英语有错误,谢谢。

将图像从Android上传到WCF C#/.NET

遵循以下3条,这些是否可以帮助您。。

  • http://www.jarredcapellman.com/2012/2/8/getting-a-picture-from-android-to-a-wcf-service
  • 将图像从Android发布到WCF Rest Service
  • Android上传图像到WCF服务";参数无效"