Blur effect on image: different result before and after saving

608 Views Asked by At

I have a bug in my image editor with the blur tool.

When I select the rectangle to set the blur effect, and when I apply, result is a bit different see:

enter image description here enter image description here

To create the "Before" I do:

var blurredImage = ExtractImageToBlur(); // extract the selected area from image

BlurredImageRectangle.Fill = new ImageBrush(blurredImage);

var effect = new BlurEffect();
effect.KernelType = KernelType.Gaussian;
effect.RenderingBias = RenderingBias.Quality;
effect.Radius = m_blurValue;

BlurredImageRectangle.Effect = effect;

To create the "After", I do:

var blurredImage = ExtractImageToBlur(); // extract the selected area from image

Rectangle rectangleToRender = new Rectangle();

rectangleToRender.Fill = new ImageBrush(blurredImage);

var effect = new BlurEffect();
effect.KernelType = KernelType.Gaussian;
effect.RenderingBias = RenderingBias.Quality;
effect.Radius = m_blurValue;

rectangleToRender.Effect = effect;

Size size = new Size(croppedImg.PixelWidth, croppedImg.PixelHeight);
rectangleToRender.Measure(size);
rectangleToRender.Arrange(new Rect(size));

var render = new RenderTargetBitmap(croppedImg.PixelWidth, croppedImg.PixelHeight, 96, 96, PixelFormats.Pbgra32);
render.Render(rectangleToRender);

// Merge the source with the blurred section
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext context = drawingVisual.RenderOpen())
{
    int left = (int)(Canvas.GetLeft(BlurredImageRectangle) * WidthRatio);
    int top = (int)(Canvas.GetTop(BlurredImageRectangle) * HeightRatio);

    context.DrawImage(Source, new Rect(0, 0, Source.PixelWidth, Source.PixelHeight));
    context.DrawImage(render, new Rect(left, top, croppedImg.PixelWidth, croppedImg.PixelHeight));
}

var bitmap = new RenderTargetBitmap(Source.PixelWidth, Source.PixelHeight, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(drawingVisual);

And when I play with the blur radius, its sometimes a lot more different from both images.

Why its not the same?

1

There are 1 best solutions below

0
mlemay On BEST ANSWER

Found the problem.

When I was drawing the rectangle on the screen, I applied the blur effect on the pixels on the screen.

When I hit save, the blur effect is applied on the pixel on the image on disk.

Huge difference.