我從asp.net網頁API嘗試下面的代碼,以輸出圖像,但響應的車身長度始終爲0輸出圖像使用Web API HttpResponseMessage
public HttpResponseMessage GetImage()
{
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StreamContent(new FileStream(@"path to image"));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return response;
}
任何提示嗎?
WORKS:
[HttpGet]
public HttpResponseMessage Resize(string source, int width, int height)
{
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
// Photo.Resize is a static method to resize the image
Image image = Photo.Resize(Image.FromFile(@"d:\path\" + source), width, height);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Jpeg);
httpResponseMessage.Content = new ByteArrayContent(memoryStream.ToArray());
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
httpResponseMessage.StatusCode = HttpStatusCode.OK;
return httpResponseMessage;
}
不太確定,如果我得到你 - 但你可以檢查[**如何寫一個圖像outputStream **](http://stackoverflow.com/questions/5629251/c-sharp-outputting-image-to -response-output-stream-giving-gdi-error) –
我沒有收到錯誤,我什麼也沒收到。 – lolol
你回來了什麼狀態代碼? –