<p>Is there a way to bind a MemoryStream to asp:image control?</p>

[{'answer_id': 46801, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 5, 'selected': False, 'text': '<p>Best bet is to create an HttpHandler that would return the image.  Then bind the ImageUrl property on the asp:Image to the url of the HttpHandler.</p>\n\n<p>Here is some code.</p>\n\n<p>First create the HttpHandler:</p>\n\n<pre><code>&lt;%@ WebHandler Language="C#" Class="ImageHandler" %&gt;\n\nusing System.Drawing;\nusing System.Drawing.Imaging;\nusing System.IO;\nusing System.Web;\n\npublic class ImageHandler : IHttpHandler\n{    \n    public void ProcessRequest (HttpContext context)\n    {\n        context.Response.Clear();\n\n        if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))\n        {\n            int id = Int32.Parse(context.Request.QueryString["id"]);\n\n            // Now you have the id, do what you want with it, to get the right image\n            // More than likely, just pass it to the method, that builds the image\n            Image image = GetImage(id);\n\n            // Of course set this to whatever your format is of the image\n            context.Response.ContentType = "image/jpeg";\n            // Save the image to the OutputStream\n            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);\n        }\n        else\n        {\n            context.Response.ContentType = "text/html";\n            context.Response.Write("&lt;p&gt;Need a valid id&lt;/p&gt;");\n        }\n    }\n\n    public bool IsReusable\n    {\n        get\n        {\n            return false;\n        }\n   }\n\n   private Image GetImage(int id)\n   {\n       // Not sure how you are building your MemoryStream\n       // Once you have it, you just use the Image class to \n       // create the image from the stream.\n       MemoryStream stream = new MemoryStream();\n       return Image.FromStream(stream);\n   }\n}\n</code></pre>\n\n<p>Next, just call it inside your aspx page where you are using the asp:Image.</p>\n\n<pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;\n\n&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;\n\n&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;\n&lt;head runat="server"&gt;\n    &lt;title&gt;&lt;/title&gt;\n&lt;/head&gt;\n&lt;body&gt;\n    &lt;form id="form1" runat="server"&gt;\n        &lt;div&gt;\n            &lt;asp:Image ID="myImage" ImageUrl="~/ImageHandler.ashx?id=1" runat="server" /&gt;\n        &lt;/div&gt;\n    &lt;/form&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>And that is it.</p>\n'}, {'answer_id': 46803, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 2, 'selected': False, 'text': '<p>I am assuming you need to generate dynamic images from asp.net\nYou might be in luck \n<a href="http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449" rel="nofollow noreferrer">http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449</a></p>\n\n<p>Hanselman blogged about it recently \n<a href="http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx" rel="nofollow noreferrer">http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx</a></p>\n'}, {'answer_id': 46808, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 1, 'selected': False, 'text': '<p>While Databinding a MemoryStream to a Image is not possible, it could be possible to use a Label/GenericControl, some Code and the <a href="http://en.wikipedia.org/wiki/Data_URI_scheme" rel="nofollow noreferrer">data URI scheme</a> to embed Images in Pages, but there are severe issues with that approach:</p>\n\n<blockquote>\n  <p>Disadvantages</p>\n  \n  <ul>\n  <li>Embedded content must be extracted and decoded before changes may be made, then re-encoded and re-embedded afterwards.</li>\n  <li>Cookies are not supported.</li>\n  <li>Information that is embedded more than once is redownloaded as part of the containing file, and thus does not benefit from the browser\'s cache.</li>\n  <li>Browsers may limit URI lengths, creating an effective maximum data size. For example, URIs in previous versions of Opera had limits of 4kB, and 32kB for IE8 Beta 1[citation needed]</li>\n  <li>Data is included as a simple stream, and many processing environments (such as web browsers) may not support using containers (such as multipart/alternative or message/rfc822) to provide greater complexity such as metadata, data compression, or content negotiation.</li>\n  <li>Microsoft\'s Internet Explorer, through version 7 (some 70% of the market as of 2008 Q2), lacks support.</li>\n  </ul>\n</blockquote>\n\n<p>The better Approach is to use a separate "Image.aspx" Page which takes and outputs your MemoryStream, kinda like I did in my Photo Album software that i\'ve created when I started learning ASP.net:</p>\n\n<p><a href="http://souvenance.svn.sourceforge.net/viewvc/souvenance/trunk/Souvenance.Web/Image.aspx.cs?revision=100&amp;view=markup" rel="nofollow noreferrer">(Don\'t laugh, that was my first attempt at ASP.net :-)</a></p>\n\n<p>Edit: Agreed on ASHX, the code above is just to show one sample implementation. When I come around to update the Photo Album, it will use ASHX for that.</p>\n'}, {'answer_id': 46811, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': '<p>@Will and Ben Griswald:  instead of "image.aspx" use "image.ashx".</p>\n\n<p>It\'s more light-weight than a full ASP.Net Page, and it\'s specifically designed to handle content-types other than text/html.</p>\n'}, {'answer_id': 46819, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Nope.  </p>\n\n<p>But you can create a special page to stream that image out.  First, you set the URL of the image to the page that performs the streaming, including some url parameters that let you know where to get the image:</p>\n\n<pre><code>&lt;img src="GetImage.aspx?filename=foo" ... /&gt;\n</code></pre>\n\n<p>in GetImage.aspx, you get the filename (or whatever) from the URL, load the image in your MemoryStream, and then write the content of that memory stream directly to the HttpResponse:</p>\n\n<pre><code>    response.Expires = 0;\n    response.Buffer = false;\n    response.Clear();\n    response.ClearHeaders();\n    response.ClearContent();\n    response.ContentType = "image/jpeg";\n    response.BinaryWrite(stream);\n    response.Flush();\n    response.Close();\n</code></pre>\n'}, {'answer_id': 46894, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 4, 'selected': True, 'text': "<p>A handler can accept a url parameter like any other request. So instead of linking your <code>&lt;asp:image/&gt;</code> to <code>image.ashx</code> you'd set it to <code>image.ashx?ImageID=[Your image ID here]</code>.</p>\n"}, {'answer_id': 13606996, 'author': 'Farshid', 'author_id': 516512, 'author_profile': 'https://Stackoverflow.com/users/516512', 'pm_score': 1, 'selected': False, 'text': '<p>You can use Telerik\'s BinaryImage control for ASP.net. </p>\n\n<p>More info in here:\n<a href="http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx" rel="nofollow">http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx</a></p>\n'}, {'answer_id': 14313352, 'author': 'Ricardo Conte', 'author_id': 337764, 'author_profile': 'https://Stackoverflow.com/users/337764', 'pm_score': 0, 'selected': False, 'text': '<p>For me it was necessary to add "buffer="false" to the @Page. Otherwise I would keep getting the same picture all the time...</p>\n'}]
['https://Stackoverflow.com/questions/46788', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4215/']