1

早上好,我需要将存储卡中的图像从单位发送到我在 wcf / C# 中做的 web 服务,不知道公司对 C# 有多少要求,我什至可以发送一个Strem for WCF,但在转换和转换为位图或其他东西时遇到了麻烦。

按照我在 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#。

抱歉英文错误,谢谢。

4