0

I've got an ASP.NET application with a library that generates an Image. It is set up as follows:

In the Web.config I have a declaration in the <httpHandlers> section like so:

<add path="My.image.aspx" verb="GET" type="MyProject.Handlers.MyImageHandler, MyProject" validate="false"/>

This links up the code to generate the image to that path which is then used on my .aspx page like this:

<asp:Image ImageUrl="My.image.aspx" BorderWidth="0" ID="myImage" runat="server"/>

When viewing this page normally, the image is loaded correctly.

However, I need to output this image to a PDF file, but because the PDF's won't interpret the page HTML with that URL correctly (says it cannot find resource, because these files are limited to interpreting only basic HTML/CSS), I wanted to try to load the Image in the code behind and output it to the PDF that way. However, I have no idea how to load this image in the code behind. I tried the following:

System.Drawing.Image img = System.Drawing.Image.FromFile("My.image.aspx");

But had no luck. Is there a proper way to load this Image in the code behind from the httpHandler I have specified?

EDIT: I found a workaround, as I couldn't get the Image to load from the code behind. Instead, I have just used the <asp:Image> control to the load the file on the page and trigger the handler. When the handler creates the System.Drawing.Image file I was able to add it to the Session object for use later on that page. However, I still was unable to find a way to trigger all of that from the code behind without using a front end ASP control.

user1048281
  • 233
  • 1
  • 4
  • 16

1 Answers1

0

you asp:image have and id as myImage

So in the code behind you can use it this way

using (Bitmap theImage = new Bitmap("My.image.aspx"))
{
   using(MemoryStream stream = new MemoryStream())
   {
      theImage .Save(stream , System.Drawing.Imaging.ImageFormat.Png);
      stream .WriteTo(context.Response.OutputStream);
   }
}
HatSoft
  • 11,077
  • 3
  • 28
  • 43
  • 'System.Drawing.Image.FromFile("My.image.aspx")' will not load the image because it says it cannot find the file. The ASP.NET Image control doesn't contain a FromFile property, and finally the image on the ASPX is a different type of image from the System.Web.UI.WebControls library. – user1048281 Jul 20 '12 at 15:43
  • @user1048281 your right i got side tracked i have updated the answer – HatSoft Jul 20 '12 at 15:46
  • Well this works to load the Image. But I want to store the Image file as a System.Drawing.Image type for output. Edit: This is also the same thing I am doing already...just from the code behind. – user1048281 Jul 20 '12 at 16:04
  • @user1048281 see updated answer this will write the image to you outputstream – HatSoft Jul 20 '12 at 16:09
  • Gives the "Parameter is not valid." exception when loading the Bitmap the same exception as when trying this System.Drawing.Image.FromFile("My.image.aspx"); – user1048281 Jul 20 '12 at 16:19
  • You can't load an image from a Handler like that, with the related URL specified in the Web.config. My question is how to do this another way because this way clearly does not work. – user1048281 Jul 20 '12 at 16:32
  • Thanks for the suggestions but I found a workaround, am adding it to the original post. – user1048281 Jul 20 '12 at 16:48