6

I have a WPF Image control with attached blur effect. Is there a way to save the image (with blur) without using RenderTargetBitmap?

Thank you.

UPDATE: I'm using now new custom effect which derives from System.Windows.Media.Effects.ShaderEffect. I would like to save my image with shader effect applied.

Valentin V
  • 24,971
  • 33
  • 103
  • 152

5 Answers5

8

the only way you can render the bitmap is using RenderTargetBitmap.

Have a look at this example:

BitmapSource bitmap=GetYourBitmap();
Rectangle r=new Rectangle();
r.Background=new ImageBrush(bitmap);
r.Effect=yourEffect;

Size sz=new Size(bitmap.PixelWidth, bitmap.PixelHeight);
r.Measure(sz);
r.Arrange(new Rect(sz);

var rtb=new RenderTargetBitmap();
rtb.Render(r);
return rtb;//here is your bitmap with effects applied

Hope this helps

Narzanka
  • 246
  • 1
  • 3
  • 5
    its worth pointing out that this will process the effect on the CPU and not on the GPU. – Patrick Klug Aug 17 '10 at 00:01
  • 1
    Does not work for me. – Maria Nov 17 '21 at 15:41
  • Note that, according to https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.effects.shadereffect?view=windowsdesktop-8.0#remarks , this does not work with PS_3_0 shaders. Can confirm. – Niko O May 13 '23 at 22:43
2

I know this is an old question ... but I thought I would point people to Jamie Rodriguez's post (http://blogs.msdn.com/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx) on this subject.

I had a situation where using RenderTargetBitmap was resulting in an empty image ... and Jamie's post was the answer for me.

Hope it helps someone else too.

cplotts
  • 13,941
  • 9
  • 55
  • 66
2

This is something I wanted also. According to this: http://social.msdn.microsoft.com/Forums/en/wpfprerelease/thread/e2ebf264-e087-4bfe-a69b-24c884675c80 RenderTargetBitmap does not use HW (GPU) to render, only software. What a pity.

KV

kvik
  • 21
  • 1
1

Since the shader effect is by definition applied on the video card, the only way you can get a copy of it in main memory is to grab it from screen memory. So RenderTargetBitmap is your solution. Is there any particular reason you wanted to avoid it?

U62
  • 4,355
  • 1
  • 24
  • 37
  • Hi U62, thanks for your reply. I wanted to avoid it purely for perf. reasons. Thanks. – Valentin V Feb 14 '09 at 06:47
  • classy, you downvote my answer then award a bounty and the accepted answer to someone who tells you the exact same thing over a week later. I love SO, the users really make it what it is... – U62 Feb 27 '09 at 20:18
0

Here you go: http://perspectivefx.codeplex.com/ and all work done by GPU

Andreas
  • 3,843
  • 3
  • 40
  • 53