How to apply a drop shadow effect when drawing to a DrawingContext in WPF?

时间秒杀一切 提交于 2019-11-29 18:34:52

问题


I'm drawing into a DrawingContext, and I'd like to apply a drop shadow effect to part of the drawing. At the moment I create the relevant parts in a DrawingGroup and apply a BitmapEffect, but this has no effect:

var layer = new DrawingGroup();
using (var lcontext = layer.Open())
{
    // draw stuff in lcontext
}
layer.BitmapEffect = new DropShadowBitmapEffect { Color = Colors.Black, ShadowDepth = 3, Opacity = 0.5 };
context.DrawDrawing(layer);

This draws everything inside the layer correctly, but without the drop shadow effect.

What am I doing wrong / how else might I apply a drop shadow to a bunch of primitives in a DrawingContext?


回答1:


BitmapEffect is an old property (they used CPU-rendered effects) from pre .NET 3.5. The property has no effect in 4.0.

In 4.0 you should use Effect property, which uses Pixel Shaders.

DrawingGroup however doesn't appear to have an effect property - it sounds like you might need to set the effect on the parent UI element instead.




回答2:


If you're running on .NET Framework 3.5 SP1 or higher, you should use UIElement.Effect or Visual.VisualEffect (this one is protected so you would need to derive from it) instead, with an instance of System.Windows.Media.Effects.DropShadowEffect.



来源:https://stackoverflow.com/questions/8655749/how-to-apply-a-drop-shadow-effect-when-drawing-to-a-drawingcontext-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!