locked
Problems building HttpHandler for Image resources RRS feed

  • Question

  • User-1278464155 posted
    I'm building a class library that holds image resources in different formats (.gif, .jpeg, .png) and a HttpHandler that will send the images to the browser. The class library is linked in my web application and the HttpHandler is linked through the web.config (using the <httphandlers> section).

    Now the problem is that when sending an image to the broswer using my ResourceHandler, I must know what type the image is in my ImageResources class. How can I get the image type (gif/jpeg/png) or should I take a different approach to return my images? Condition is that the images are stored in the class library.

    Thanks for any help.



    Here is my code

    HTTPHANDLER:
    namespace ClassLibrary1
    {
        public class ResourceHandler : IHttpHandler
        {
            public bool IsReusable { get { return true; } }

            public void ProcessRequest(HttpContext context)
            {
                // some error checking removed
                string imageName = context.Request.QueryString["name"];

                Bitmap o = (Bitmap)ImageResources.ResourceManager.GetObject(imageName);
               
                ImageFormat format = ImageFormat.Png;

                context.Response.Clear();
                o.Save(context.Response.OutputStream, format);
            }
        }
    }


    WEB.CONFIG
    <system.web>
         <httpHandlers>
             <add verb="*" path="MyResources.axd" type="ClassLibrary1.ResourceHandler"/>
         </httpHandlers>
    </system.web>

    DEFAULT.ASPX
    <img src="MyResources.axd?name=error_medium" />
    Tuesday, January 10, 2006 6:02 AM

All replies

  • User-1278464155 posted
    I forgot to mention that I'm working with ASP.NET 2.0.
    Tuesday, January 10, 2006 3:13 PM
  • User-528039901 posted

    How can I get the image type (gif/jpeg/png)

    From the text field in the database where you stored it when you uploaded the image. Helps if you also store more information such as resolution (width, height) and stuff like this just in case you need it.

    That, at least, is what we do in the PowerNodes CMS. The database does not JUST have the binary array of the data, but also some information ABOUT the data.

    Monday, January 23, 2006 3:00 AM
  • User-158764254 posted

    I believe you need to include the file extension as part of your imageName in the resource assembly.

    Your calling page would then look more like this:
    <img src="MyResources.axd?name=error_medium.jpg" />

    now in your handler, you can extract the extension from the imageName and map it to the appropiate ContentType for your response stream.

    Monday, January 23, 2006 4:41 PM