I developed a βsimpleβ test file:
[HttpPost]
[Route("GetImage")]
public HttpResponseMessage GetImage()
{
try
{
int bmpX = 50;
int bmpY = 50;
DrawingVisual drawingVisual = new DrawingVisual();
var dc = drawingVisual.RenderOpen();
dc.DrawRectangle(Brushes.Green, new Pen(Brushes.Green, 3), new System.Windows.Rect(0, 0, bmpX, bmpY));
dc.DrawLine(new Pen(Brushes.Red, 2.0), new System.Windows.Point(0, 0), new System.Windows.Point(bmpX, bmpY));
dc.DrawLine(new Pen(Brushes.Red, 2.0), new System.Windows.Point(0, bmpY), new System.Windows.Point(bmpX, 0));
dc.Close();
var bmp = new RenderTargetBitmap(bmpX, bmpY, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
MemoryStream memoryStream = new MemoryStream();
encoder.Save(memoryStream);
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
httpResponseMessage.Content = new ByteArrayContent(memoryStream.ToArray());
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
httpResponseMessage.StatusCode = HttpStatusCode.OK;
return httpResponseMessage;
}
catch (Exception ex)
{
var message = Request.CreateResponse(HttpStatusCode.Created, ex.ToString());
return message;
}
}
This is a function inside my ASP.Net WebAPI project, as part of the controller (ValuesController, as it happens, so adding this to the project by default should be easy enough to reproduce the problem). If I host this on Azure and run it locally and then call both from Fiddler, the local one works as expected and returns a red X on a green background, but Azure returns a blank image.
If I use GDI (System.Drawing.Bitmap), then it works fine, even from Azure. Unfortunately, this is not a solution; I am transferring an already written application from the desktop to the Azure API service, and the PresentationCore code is extensive and takes too long to convert to GDI.
: - Azure/Web API/PresentationCore, , .