在图像上添加模糊效果

时间:2015-05-04 07:21:03

标签: c# xaml windows-phone-8

我想要用户点击药水上的图像模糊效果。用户可以在图像上轻击多次,当他点击他点击的图像药水时会模糊。

using (var blurfilters = new FilterEffect(source))
{
    var blur = new BlurFilter();

    blurfilters.Filters = new IFilter[] { blur };
    var target = new WriteableBitmap((int)img1.ActualWidth, (int)img1.ActualHeight);
    using (var renderer = new WriteableBitmapRenderer(blurfilters, target))
    {
        await renderer.RenderAsync();
        img1.Source = target;
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个:

http://www.blendrocks.com/code-blend/2015/1/29/implementing-image-blur-in-a-windows-universal-app

这适用于我的通用应用程序。

有效代码是:

private void OnDraw(CanvasControl sender, CanvasDrawEventArgs args)
{
    if (imageLoaded)
    {
        using (var session = args.DrawingSession)
        {
            session.Units = CanvasUnits.Pixels;

            double displayScaling = DisplayInformation.GetForCurrentView().LogicalDpi / 96.0;

            double pixelWidth = sender.ActualWidth * displayScaling;

            var scalefactor = pixelWidth / image.Size.Width;

            scaleEffect.Source = this.image;
            scaleEffect.Scale = new Vector2()
            {
                X = (float)scalefactor,
                Y = (float)scalefactor
            };

            blurEffect.Source = scaleEffect;
            blurEffect.BlurAmount = Blur;

            session.DrawImage(blurEffect, 0.0f, 0.0f);
        }
    }
}

对于Silverlight应用程序,请尝试以下方法:

http://www.dotnetcurry.com/showarticle.aspx?ID=1033

如果您希望高斯模糊,那么另一个很好的例子是:

WP8: Is there an easy way to scale and blur an BitmapImage for windows phone app?

答案 1 :(得分:0)

试试这个:

var blur = new BlurFilter(30);  

编辑:

要做到gaussian blur与WritableBitmapExtensions执行以下操作(出于某种原因concolution不编辑writableBitmap,所以你必须重新分配它同样writableBitmap看到的结果):

WriteableBitmap target = new WriteableBitmap((int)img1.ActualWidth, (int)img1.ActualHeight);
target = target.Convolute(WriteableBitmapExtensions.KernelGaussianBlur5x5);

target = target.Convolute(WriteableBitmapExtensions.KernelGaussianBlur3x3);