Tim Scott

All things for a good .NET geek

  Home  |   Contact  |   Syndication    |   Login
  36 Posts | 0 Stories | 4 Comments | 33 Trackbacks

News


Archives

Post Categories

Image Galleries

Coding

TechEd

JezB asked a question on microsoft.public.dotnet.framework.windowsforms about how to draw a nice soft shadow behind a rectangular object.  I suggested that one way would be to use image slicing and scaling, as per the “old HTML image table” trick.:

Make a blurred rectangle in your favorite graphics program. I used Photoshop:

Now slice the image into 9 parts, keeping the corners as small as possible but still large enough to contain the entire "corner" part of the image. In this enlarged sample, the blue lines indicate where we are going to slice the image:

Now save those 9 parts as image files or embedded resources in your project. At runtime, use Graphics.DrawImage to strech the images as needed. You'll need to stretch (or shrink the edge parts) to match the size of the shadow rectangle that you are trying to make. Using the same parts, you can make different shadow rectangles. The borders are turned on so you can see how it goes together:

We can make it larger just by streching the edges and middle, leaving the corners the same size:

With borders on, so you can see where we stretch it:
This example was shown with HTML, but the same principles would apply when using Graphics.DrawImage. But after all of that, I like the method in Bob Powell's GDI+ faq much better.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Wednesday, October 26, 2005 12:18 PM