Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 867
  • Last Modified:

c# .net, memorystream in code to image with request

I have an HTTP request to my c# application that generates a memorystream, I want this request to return that memorystream as an image just as if I were to go on lets say google.com/myimage.jpeg I want the image to be shown.

How do I return it exactly?
0
NewtonianB
Asked:
NewtonianB
  • 3
1 Solution
 
xeonolCommented:
I assume that the MemoryStream is named ms and the image type is JPEG, then you can return it simply with
 
Response.ContentType = "image/png"
Response.BinaryWrite(ms.ToArray)
Response.End()

Open in new window

0
 
xeonolCommented:
Oops, sorry, there is a typo in the contenttype, it should be "image/jpeg".
0
 
NewtonianBAuthor Commented:
On response.end I get
"[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}"
0
 
xeonolCommented:
Oh, the cause of the exception is here: http://support.microsoft.com/kb/312629/EN-US/

As it suggests, we should call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution.

Sorry for the mistake.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now