对图像的模糊效果:保存前后的不同结果

时间:2016-01-18 16:08:10

标签: c# .net wpf blur drawingvisual

我的图像编辑器中有一个带有模糊工具的错误。

当我选择矩形来设置模糊效果时,当我应用时,结果会有所不同,请参阅:

enter image description here enter image description here

要创建“之前”我做:

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;

要创建“After”,我会这样做:

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);

当我使用模糊半径时,它有时会比两个图像更加不同。

为什么不一样?

1 个答案:

答案 0 :(得分:0)

发现问题。

当我在屏幕上绘制矩形时,我将模糊效果应用于屏幕上的像素。

当我点击“保存”时,模糊效果将应用于磁盘上图像上的像素。

巨大差异。