CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 1999
    Location
    Galway, Ireland
    Posts
    96

    HttpHandler - dynamic images

    I'm using a CAPTCHA image generator from Code Project and would like to wrap this in to a web control.

    So I was wondering what's the best way to display a dynamic image from a memorystream? Currently I'm setting the img src to a handler which works fine. But the handler needs to be a servable page and can't be wrapped in to a control dll. I'd prefer not to write the image to a temp directory. Is there an alternative or a nifty trick like putting the handler in as a resource in the dll?
    Lounge One-Eleven Presents: Gong! v2.0 - you never knew you needed it until you lived without it

  2. #2
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: HttpHandler - dynamic images

    You can output an image with an .aspx page (or even better, an .ashx page.) Just set the Response.ContentType to the proper mime-type and write the image contents to the output stream.

  3. #3
    Join Date
    Apr 1999
    Location
    Galway, Ireland
    Posts
    96

    Re: HttpHandler - dynamic images

    Yeah, that's what I just said. I'm already using a httphandler (ashx). That's the problem, you can't wrap an ashx page in a web control dll. It has to be a servable page on the actual website.

    The point of my question is how to achieve this functionality within a seperate web control dll.
    Last edited by Chris Green; November 9th, 2007 at 06:42 AM.
    Lounge One-Eleven Presents: Gong! v2.0 - you never knew you needed it until you lived without it

  4. #4
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: HttpHandler - dynamic images

    Ah, I see.

    Try something like this in the RenderContents method:
    Code:
    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.ContentType = "image/jpeg";
    // Do something with response.OutputStream
    response.End();
    It'll basically hijack the current page and output the image instead.

  5. #5
    Join Date
    Apr 1999
    Location
    Galway, Ireland
    Posts
    96

    Re: HttpHandler - dynamic images

    Ahh, but that will just return a blank page with nothing but the image. No, I think here we need some serious voodoo. I'd be surprised if its simple.
    Lounge One-Eleven Presents: Gong! v2.0 - you never knew you needed it until you lived without it

  6. #6
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: HttpHandler - dynamic images

    Yes, of course it will. Now I'm not sure what you want to accomplish. You know that you can't branch a web request right? It can only ever return one file, which in your case will be either the website or the image. Some browsers do support inline data, but that's nothing I would recommend trying because it'd be inefficient and wouldn't work in all mainstream browsers.

    If you want a dialog to pop up instead of the browser loading the image, you can use the Content-Disposition HTTP header with the "attachment" value.

    Edit; example:
    Code:
    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.AddHeader("Content-Disposition", "attachment; filename=\"image.jpg\"");
    response.ContentType = "image/jpeg";
    // Do something with response.OutputStream
    response.End();
    Edit 2

    If you meant you want the web control to be able to output dynamically generated images as part of the page it's on, then you can solve this by adding some obscure QueryString value (?j38dla=1 or something equally random to avoid collision with query values sent by the website) and then when that is set you run my example code. That way you could do something such as the following:
    Code:
    controlImage.ImageUrl = "CurrentPage.aspx?j38dla=1";
    Last edited by andreasblixt; November 9th, 2007 at 09:07 AM.

  7. #7
    Join Date
    Apr 1999
    Location
    Galway, Ireland
    Posts
    96

    Re: HttpHandler - dynamic images

    In Edit2 you're getting there. A webcontrol capable of producing a dynamic image in a Class Library project. As a webcontrol.dll you won't have an aspx or ashx pages to serve. I guess my post was really unclear.
    Lounge One-Eleven Presents: Gong! v2.0 - you never knew you needed it until you lived without it

  8. #8
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: HttpHandler - dynamic images

    My suggestion is a pretty nasty way of doing it if it's a distributable library you are making because the current page will be executed once for each image requested (until the point your control is being rendered, because then the image will be output and the request terminated.)

    I'd probably solve it by creating a HttpHandler .dll file (similar to the way .ashx pages work, but compiled as a DLL and referenced in Web.config as an assembly.) That's how the AJAX web control library handles its dynamic files (ScriptResource.axd for example.)

    I found this article about HttpHandler on Google:
    http://www.devx.com/dotnet/Article/6962/0/page/1

    Edit
    The article mentions adding extensions in IIS that point to the ASP.NET DLL. Instead of doing this, use the .axd extension for your virtual resources as all .axd files will be automatically sent to ASP.NET by default (and the extension is meant to be used for virtual files, unlike .aspx & co.)
    Last edited by andreasblixt; November 9th, 2007 at 09:48 AM.

  9. #9
    Join Date
    Apr 1999
    Location
    Galway, Ireland
    Posts
    96

    Re: HttpHandler - dynamic images

    Now we're getting somewhere! Yeah thats a good idea. From my understanding handlers (vs modules) we're originally setup to handle a certain file type (like .jpg to add a watermark). You could then have your webcontrol (in the same dll) do something like
    <img src="image.capcha?code=123" />

    The handler merely checked for .capcha and returns the image.

    Even better if there would be a way in the webcontrol init to programatically add the handler (vs. manually setting it up in the web.config).


    Probably wouldn't work but you know how you can embed an image which is then retreived by webresource.axd? What if it served up an aspx or ashx file? Probably would blow up
    Lounge One-Eleven Presents: Gong! v2.0 - you never knew you needed it until you lived without it

  10. #10
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: HttpHandler - dynamic images

    The problem with using .capcha as an extension is that IIS wouldn't know where to send the request (unless ASP.NET was set up to catch all non-handled requests.) It would merely return a 404 error unless you manually add the .capcha extension to IIS which means even more work for the user installing your web control. All requests to files ending with .axd are sent to ASP.NET by default however.

    As for adding the handler programmatically to an application, you could make your app open Web.config as an XML file and add the handler section, but I wouldn't recommend it. It's also possible to change the handler in the early events of the application, but I doubt you'll have access from the web control code (it's probably not run before the default ASP.NET PageFactoryHandler has been initialized.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured