How to bind a MemoryStream to asp:image control?

62,719

Solution 1

A handler can accept a url parameter like any other request. So instead of linking your <asp:image/> to image.ashx you'd set it to image.ashx?ImageID=[Your image ID here].

Solution 2

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.

Here is some code.

First create the HttpHandler:

<%@ WebHandler Language="C#" Class="ImageHandler" %>

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("<p>Need a valid id</p>");
        }
    }

    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);
   }
}

Next, just call it inside your aspx page where you are using the asp:Image.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Image ID="myImage" ImageUrl="~/ImageHandler.ashx?id=1" runat="server" />
        </div>
    </form>
</body>
</html>

And that is it.

Solution 3

I am assuming you need to generate dynamic images from asp.net You might be in luck http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449

Hanselman blogged about it recently http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx

Solution 4

@Will and Ben Griswald: instead of "image.aspx" use "image.ashx".

It's more light-weight than a full ASP.Net Page, and it's specifically designed to handle content-types other than text/html.

Solution 5

You can use Telerik's BinaryImage control for ASP.net.

More info in here: http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx

Share:
62,719
György Balássy
Author by

György Balássy

Wait for it.

Updated on April 08, 2020

Comments

  • György Balássy
    György Balássy about 4 years

    Is there a way to bind a MemoryStream to asp:image control?

  • Imran Rizvi
    Imran Rizvi over 12 years
    Is there any way to do this without HttpHandler, I don't have write to create handler in my project'