Using Embedded Images in ASP.Net V2

Thanks to Yves and Peter for the advise I utilized your advise to create a new version my embedded Image example. I did switch to an ashx file, I didn't even know about this file type until now. I'm no longer passing in the assembly as a parameter but I didn't want to put the assembly in the config file either, so I figured that I would just test for the image by the passed in name in all the assemblies loaded into that AppDomain. There shouldn't be to many right? But you do have to make sure that embedded image has a unique name, that shouldn't be to hard though because you can prefix it by your namespace.
<% @ webhandler language="C#" class="ImageHandler" %> 

public class ImageHandler : System.Web.IHttpHandler 
{ 
  public void ProcessRequest(System.Web.HttpContext context) 
  { 
    string image = string.Empty;
    System.IO.Stream stream = null;
      
    try
    {
      context.Response.Clear();

      // Get Image Name from request
      image = context.Request["img"].Replace("/"".");   
  
      // Find image in loaded assemblies
      foreach(System.Reflection.Assembly asm in System.AppDomain.CurrentDomain.GetAssemblies())
      {
        stream = asm.GetManifestResourceStream(image);
        if(stream != null)
          break;
      }
      
      // Throw exception if image is not found
      if(stream == null)
        throw new System.Exception("Image not found in loaded assemblies");
            
      // Create an Image object from the stream
      System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
      
      // Save the Image stream to the output stream
      img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
    }
    catch(System.Exception ex)
    {
      context.Response.Write("Exception: <BR>image = " + image + "<BR>" +
        ex.Message + "<BR>" + ex.StackTrace.Replace("\n""<BR>"));  
    }
    finally
    {     
      context.Response.End(); 
    }; 
  } 
   
  public bool IsReusable 
  { 
    get { return true; } 
  } 
}

So what do you guys think about revision 2?

2 Comments

  • I don't think you're supposed to be posting ANYTHING about V@ if you happen to have alpha bits. I believe you signed an NDA that requires you to not talk about it. I would HIGHLY recommend, for your sake, that you make sure you are clear to post stuff before you actually do.

  • Sorry but I think you are mistaken this has nothing todo with ASP.Net V2 I don't know anything about that it is my second attempt to use embedded images. All my code is still using ASP.Net V1. :)

Comments have been disabled for this content.