Home > Tips > Strange behavior of DropShadowEffect

Strange behavior of DropShadowEffect

Yesterday, I spent the whole morning wrestling with WPF because an object I was re-implementing was appearing too blurry when compared to the existing native version. This was a basic label object, containing a picture and some text. Both the picture and the text were quite fuzzy, and don’t get me wrong, I am aware that WPF 3.5 SP1 still has issues with the text layout mechanism (the ideal layout compared to pixel-aligned layout). But this was much worse, as you can see in the screen grab:

image

You may notice that I am applying a drop shadow effect to the label, for some eye candy. And believe it or not, after a few hours of tweaking the XAML, and selectively disabling Visuals, I found the culprit to be the DropShadowEffect! As a designer, I would expect the DropShadowEffect to create a shadow behind my object, with the same shape as the object boundaries. Apparently this is not what the effect does, but rather overlays (or underlays?) a version of the source Visual, causing the blur effect. The solution to these types of problems is very simple: you just need to embed my Visual inside a Grid, so that you can add a Border with the same shape under my Visual. That Border will then have the effect applied to it, so basically I have moved any blurriness behind my label:

Separate drop shadow compared to On-object drop shadow

You can easily test this behavior in Kaxaml, by entering the following XAML, and removing the Effect from the Border element:

<Border Background="Yellow" BorderBrush="Black" BorderThickness="1" Width="100" Height="26">
    <Border.Effect>
        <DropShadowEffect/>
    </Border.Effect>
    <TextBlock Text="Label"/>
</Border>

I would still like to understand the reason for this behavior of the DropShadowEffect, so if you have any explanation for it, do let me know.

Shout it

kick it on DotNetKicks.com

Share and Enjoy:
  • Digg
  • del.icio.us
  • DotNetKicks
  • DZone
  • Technorati
  • LinkedIn
  • Google Bookmarks
  • StumbleUpon
  • email
  • Print
Categories: Tips Tags:
  1. jd
    January 19th, 2010 at 09:23 | #1

    Not totally strange. When an effect is applied, your visual is rendered to an offscreen surface, and the shader is then applied to that texture.

    One consequence of rendering to a texture is that ClearType is not applied and text falls back to anti-aliased mode (much thicker and blurry).

    I had that problem myself and solved it just the same: overlay one visual with the drop shadow and another with the actual _crisp_ content.

    Hopefully WPF 4 will make things better (I haven’t tested that yet, but you can selectively force ClearType where WPF would disable it by default – that is when rendering to an offscreen surface).

    • January 25th, 2010 at 22:15 | #2

      Hmm, I can understand there is a technical reason behind the behavior, but I think this is one of those cases which would warrant a change of behavior to avoid this trap. I don’t think anyone would want (or expect) this behavior by default.

  1. January 17th, 2010 at 20:19 | #1