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);
As you can probably see I have tried using Json library to convert the results of the StreamReader to a System.Drawing.Image
I get the feeling the json library is not able to do what I am trying to do.
Can I convert the StreamReader string "result" and return it as a System.Drawing.Image?
Thanks
–
You can't simply serialize a System.Drawing.Image to Json, and then deserialize the json back to a System.Drawing.Image. Json simply serializes the public properties of an object, and System.Drawing.Image is a wrapper around the GDI2 library.
Instead you should save your System.Drawing.Image to a byte array using some image encoder (jpg or png), and return that byte array from your GetVisitorImage action. On the client side you read all of the bytes (using ReadToEnd on the ResponseStream) and render them, or you still have the option to reconstruct your System.Drawing.Image from your stream - if you really need a System.Drawing.Image.
I am completely unfamiliar with the web API, so I am not entirely sure what the best way is to declare this in your webAPI method.
Here is how I fixed it.
Basically, in the service I converted the image to a base64 string and returned that.
In the client I converted the returned base64 string back to a System.Drawing.Image
This is the new Service controller:
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;
And this is the new client function
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;
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.