Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create ASP.Net Handler to return Image as Bytes

Tags:

c#

asp.net

I am looking at creating a handler to return images based on id passed through, I haven't created my own one before and when I created it it mentions that it has to be registered with IIS. This project is distributed to a lot of clients, will I have to change each one's IIS or is there some way round this or an alternative to a handler?

EDIT: In response to below, this is what I have created (but not yet tested), so will I need to change anything in IIS or web.config for this?

public class Photos : IHttpHandler
{
    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        //write your handler implementation here.
        var img = Image.FromFile(@"C:\Projects\etc\logo.jpg");
        context.Response.ContentType = "image/jpeg";
        img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    }

    #endregion
}
like image 410
user1166905 Avatar asked Jan 30 '14 15:01

user1166905


1 Answers

You can create a class that inherits IHttpHandler and within that grab the id (from the querystring or similar), process the request and return the binary data. Shouldn't have to register it with IIS...

public class MyHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        //Get Id from somewhere

        //Get binary data

        context.Response.ContentType = "application/octet-stream";
        context.Response.BinaryWrite(bytes);
    }
}
like image 138
Dave Zych Avatar answered Sep 22 '22 00:09

Dave Zych