0

MSDN says, that one may apply blur bitmap effect do stuff drawn by the DrawingContext using the PushEffect method. But, both PushEffect and *BitmapEffects are marked as obsolete.

How may I apply blur to what is drawn by the DrawingContext?

10
  • could you share some code here? perhaps the xaml part of the container. Commented Jul 18, 2014 at 12:34
  • There is no Xaml. I'm doing it all in OnRender of my custom control. Commented Jul 18, 2014 at 12:35
  • I wonder what kind of "custom control" you're doing that requires OnRender()? Commented Jul 18, 2014 at 12:38
  • do you want to blur whole render or just a part of it? Commented Jul 18, 2014 at 12:53
  • You might perhaps use the UIElement.Effect property instead. Commented Jul 18, 2014 at 12:55

1 Answer 1

4

Here you go

in the constructor of your UserControl set the Effect property to an instance of BlurEffect and that would blur the whole render.

    public UserControl()
    {
        InitializeComponent();
        Effect = new BlurEffect() { Radius = 10 };
    }

Selective Blur

I attempted to achieve selective blur by leveraging RenderTargetBitmap class

I've created an extension method for simplified usage

Extension class

public static class DrawingContextExtension
{
    public static void RenderBlurred(this DrawingContext dc, int width, int height, Rect targetRect, double blurRadius, Action<DrawingContext> action)
    {
        Rect elementRect = new Rect(0, 0, width, height);
        BlurredElement element = new BlurredElement(action)
        {
            Width = width,
            Height = height,
            Effect = new BlurEffect() { Radius = blurRadius }
        };
        element.Arrange(elementRect);
        RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
        rtb.Render(element);
        dc.DrawImage(rtb, targetRect);
    }

    class BlurredElement : FrameworkElement
    {
        Action<DrawingContext> action;
        public BlurredElement(Action<DrawingContext> action)
        {
            this.action = action;
        }
        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            action(drawingContext);
        }
    }
}

example code

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        int boxSize = 20;

        Pen pen = new Pen(new SolidColorBrush(Colors.Black), 1);
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                Rect targetRect = new Rect(i * boxSize, j * boxSize, boxSize, boxSize);
                if (j % 2 == 0)
                {
                    Rect elementRect = new Rect(0, 0, boxSize, boxSize);
                    double blurRadius = 5;
                    drawingContext.RenderBlurred(boxSize, boxSize, targetRect, blurRadius, dc => dc.DrawRectangle(new SolidColorBrush(Colors.Transparent), pen, elementRect));
                }
                else
                {
                    drawingContext.DrawRectangle(new SolidColorBrush(Colors.Transparent), pen, targetRect);
                }
            }
        }
    }

result

result

in above example rectangles in every odd row is blurred

Sign up to request clarification or add additional context in comments.

5 Comments

I don't want to blur everything, only some elements of the drawing.
In that case we can have a workaround to render the stuff to an element and apply the blur to it and then render the blurred element to the user control. RenderTargetBitmap can help us here. let me try to code a sample for you.
Souldn't your constructor public BlurredElement(Action<DrawingContext> action) be public Placeholder(Action<DrawingContext> action) :)
Great SelectiveBlur! I realise this was 2014 but did you ever run into "RenderTargetBitmap throws MILERR_WIN32ERROR"? It happens to me when I attempt to draw 200+ items using RenderBlurred
@MickyD, I think I never hit this error, perhaps never crossed the limits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.