I need to return an image that is stored on disk from the web service.
In my controller, I perform some search operations and send the file. Here is my code.
public HttpResponseMessage Get([FromUri]ShowImageRequest req) { // ....................... // ....................... // load image file var imgStream = new MemoryStream(); using (Image image = Image.FromFile(fullImagePath)) { image.Save(imgStream, ImageFormat.Jpeg); } imgStream.Seek(0, SeekOrigin.Begin); // it does not work without this var res = new HttpResponseMessage(HttpStatusCode.OK); res.Content = new StreamContent(imgStream); res.Content.Headers.ContentType = new ediaTypeHeaderValue("image/jpeg"); return res; }
If I do not add this line, I see in the length of the body of the response of the feedback 0
imgStream.Seek(0, SeekOrigin.Begin);
Otherwise, it works. What am I missing and why do I need it?
source share