Make Your Icons Spiffy With Image Overlays

In Windows, it is very common to see an icon that has an image overlayed on top of it to designate something special. For example, when you create a shortcut to another location, you get that nice white box with the curvy arrow that says to you “this is just a shortcut, not an actual document.” On the web, this type of thing can also be very useful in your UI, but achiving the effect with Javascript or layers can be a pain. In our CMS UI, we needed a way to designate which documents in lists were merely shortcuts or overlay different status info on top of icons. If you run into a case like this, here is some code to get you started (note, you probably want to add caching, etc. to this):

Cool Http Handler:
1using System;
2using System.IO;
3using System.Web;
4using System.Drawing;
7
8namespace SomethingHere
9{
10 
11 public class ImageOverlayRequestHandler : IHttpHandler
12 {
13  public ImageOverlayRequestHandler()
14  {
15  }
16
17  void WriteOverlayedImage(Stream s, string imageUrl, string overlayImageUrl)
18  {
19   string imagePath = HttpContext.Current.Server.MapPath(imageUrl);
20   string overlayImagePath = HttpContext.Current.Server.MapPath(overlayImageUrl);
21
22   using(System.Drawing.Image image = System.Drawing.Bitmap.FromFile(imagePath))
23   {
24    using(Bitmap tmpBitmap = new Bitmap(image.Width, image.Height))
25    {
26     using(Graphics g = Graphics.FromImage(tmpBitmap))
27     {
28      g.DrawImage(image, 0, 0);
29      using(System.Drawing.Image overlayImage = System.Drawing.Image.FromFile(overlayImagePath))
30      {
31       g.DrawImage(overlayImage, image.Width - overlayImage.Width, image.Height - overlayImage.Height);
32      }
33      tmpBitmap.Save(s, System.Drawing.Imaging.ImageFormat.Png);      
34     }
35    }
36   }    
37  }
38  #region IHttpHandler Members   ...  #endregion
62 }
63}
64

And a web control to make it easy for you:

1using System;
2using System.Web;
3using System.Drawing;
5using System.Web.UI.WebControls;
6
7namespace YourWebControlNamespace
8{
9 public class OverlayImage : System.Web.UI.WebControls.Image
10 {
11  public OverlayImage()
12  {
13  }
14
15  string _overlayImageUrl;
16  public string OverlayImageUrl
17  {
18   get
19   {
20    return _overlayImageUrl;
21   }
22   set
23   {
24    _overlayImageUrl = value;
25   }
26  }
36  protected override void Render(System.Web.UI.HtmlTextWriter writer)
37  {
38   string _orgUrl = ImageUrl;
39
40   if(OverlayImageUrl != null && OverlayImageUrl != "")
41   {
42    ImageUrl = "GetOverlayedImage.ashx?Image1="+HttpUtility.UrlEncode(ImageUrl)+"&Image2="+HttpUtility.UrlEncode(OverlayImageUrl);
43   }
44
45   base.Render (writer);
46
47   ImageUrl = _orgUrl;
48  }
49
50 }
51}

No Comments