从Webservice StreamReader返回System.Drawing.Image

时间:2014-07-09 12:52:47

标签: c#

我编写了一个webservice,其中一个函数需要返回一个System.Drawing.Image

这是我的服务功能:

public class GetVisitorImageController : ApiController
{
    [Route("GetVisitorImage/{id}")]
    [HttpGet]
    public System.Drawing.Image Get(string id)
    {
        string[] authorization = Request.Headers.Authorization.ToString().Split('|');
        string PartnerId = Request.Headers.GetValues("PartnerId").First();
        string DeviceId = Request.Headers.GetValues("DeviceId").First();
        try
        {
            return VisitorFunctions.GetVisitorImage(authorization[0], authorization[1], id, PartnerId, DeviceId);
        }
        catch
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NoContent));
        }
    }
}

这是我的客户端功能:

public static System.Drawing.Image GetVisitorImage(NetworkInfo networkInfo, string partnerId, string deviceId, string visitorId)
{
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(networkInfo.baseUrl + "GetVisitorImage" + "/" + visitorId);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "GET";
    httpWebRequest.Headers.Add("Authorization", "" + networkInfo.userName + "|" + networkInfo.userPassword + "");
    httpWebRequest.Headers.Add("PartnerId", partnerId);
    httpWebRequest.Headers.Add("DeviceId", deviceId);
    httpWebRequest.Proxy = ConfigureProxy(networkInfo);

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
        return JsonConvert.DeserializeObject<System.Drawing.Image>(result);
    }
}

您可能已经看到我尝试使用Json库将StreamReader的结果转换为System.Drawing.Image

我觉得json图书馆无法做我想做的事。

我可以转换StreamReader字符串&#34;结果&#34;并将其作为System.Drawing.Image?

返回

由于

2 个答案:

答案 0 :(得分:1)

您不能简单地将System.Drawing.Image序列化为Json,然后将json反序列化为System.Drawing.Image。 Json只是序列化对象的公共属性,System.Drawing.Image是GDI2库的包装器。

相反,您应该使用某个图像编码器(jpg或png)将System.Drawing.Image保存到字节数组,并从GetVisitorImage操作返回该字节数组。在客户端,您读取所有字节(使用ResponseStream上的ReadToEnd)并渲染它们,或者您仍然可以选择从流中重建System.Drawing.Image - 如果您确实需要System.Drawing.Image。

我对Web API完全不熟悉,所以我不完全确定在webAPI方法中声明这个的最佳方法。

答案 1 :(得分:0)

万一有人最终像我一样被困。

以下是我修复它的方法。

基本上,在服务中我将图像转换为base64字符串并返回该字符串。

在客户端中,我将返回的base64字符串转换回System.Drawing.Image

这是新的服务控制器:

public class GetVisitorImageController : ApiController
{
    [Route("GetVisitorImage/{id}")]
    [HttpGet]
    public string Get(string id)
    {
        string[] authorization = Request.Headers.Authorization.ToString().Split('|');
        string PartnerId = Request.Headers.GetValues("PartnerId").First();
        string DeviceId = Request.Headers.GetValues("DeviceId").First();
        System.Drawing.Image visitorimage = VisitorFunctions.GetVisitorImage(authorization[0], authorization[1], id, PartnerId, DeviceId);

        MemoryStream memStream = new MemoryStream();
        visitorimage.Save(memStream, ImageFormat.Jpeg);
        byte[] imageBytes = memStream.ToArray();
        string base64String = Convert.ToBase64String(imageBytes);
        return base64String;
    }
}

这是新的客户端功能

public static System.Drawing.Image GetVisitorImage(NetworkInfo networkInfo, string partnerId, string deviceId, string visitorId)
    {
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(AcceptAllCertifications);
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(networkInfo.baseUrl + "GetVisitorImage" + "/" + visitorId);
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "GET";
        httpWebRequest.Headers.Add("Authorization", "" + networkInfo.userName + "|" + networkInfo.userPassword + "");
        httpWebRequest.Headers.Add("PartnerId", partnerId);
        httpWebRequest.Headers.Add("DeviceId", deviceId);
        httpWebRequest.Proxy = ConfigureProxy(networkInfo);

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            result = result.Remove(0, 1); // remove first character
            result = result.Remove(result.Length - 1); // remove last character

            byte[] imageBytes = Convert.FromBase64String(result);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }
    }