Solved

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

Posted on 2010-11-15
4
828 Views
Last Modified: 2012-05-10
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
Comment
Question by:NewtonianB
  • 3
4 Comments
 
LVL 3

Expert Comment

by:xeonol
ID: 34142582
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
 
LVL 3

Expert Comment

by:xeonol
ID: 34142585
Oops, sorry, there is a typo in the contenttype, it should be "image/jpeg".
0
 

Author Comment

by:NewtonianB
ID: 34148593
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
 
LVL 3

Accepted Solution

by:
xeonol earned 500 total points
ID: 34152976
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now