Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<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> <p>Here is some code.</p> <p>First create the HttpHandler:</p> <pre><code>&lt;%@ WebHandler Language="C#" Class="ImageHandler" %&gt; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; public class ImageHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.Clear(); if (!String.IsNullOrEmpty(context.Request.QueryString["id"])) { int id = Int32.Parse(context.Request.QueryString["id"]); // Now you have the id, do what you want with it, to get the right image // More than likely, just pass it to the method, that builds the image Image image = GetImage(id); // Of course set this to whatever your format is of the image context.Response.ContentType = "image/jpeg"; // Save the image to the OutputStream image.Save(context.Response.OutputStream, ImageFormat.Jpeg); } else { context.Response.ContentType = "text/html"; context.Response.Write("&lt;p&gt;Need a valid id&lt;/p&gt;"); } } public bool IsReusable { get { return false; } } private Image GetImage(int id) { // Not sure how you are building your MemoryStream // Once you have it, you just use the Image class to // create the image from the stream. MemoryStream stream = new MemoryStream(); return Image.FromStream(stream); } } </code></pre> <p>Next, just call it inside your aspx page where you are using the asp:Image.</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:Image ID="myImage" ImageUrl="~/ImageHandler.ashx?id=1" runat="server" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And that is it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload