Re: How can I blur a image?

1,342 views
Skip to first unread message

Brian Osman

unread,
Dec 10, 2017, 9:39:36 AM12/10/17
to skia-d...@googlegroups.com
I'm not sure what's exposed in SkiaSharp, but you want to be using an ImageFilter, rather than a MaskFilter. Mask filters affect the geometry (which for a single image, is just a rectangle - they effectively blur the "shape", but not the contents of that shape), while image filters affect all the pixels within the shape.

This uses C++ Skia code, but you should be able to translate it to SkiaSharp: https://fiddle.skia.org/c/e1094f2d082a5c9dd074be8149c76b23

On Sun, Dec 10, 2017, 12:41 AM Miranda <hardly.a....@gmail.com> wrote:
Right now I'm using Skia through the SkiaSharp wrapper.
I really new to Skia, so I'm not really sure how to do even the most basic things...

As far as I know, I should avoid using SKBitmap since it's going to be deprecated in in the future (right?).
So I'm using a "image" as input and a "surface" as output.
I created a paint, a blur filter and then set the paint's filter to the blur filter.
Then I draw the image on the surface using the paint and save it.
But the output image is exactly equal to the input.
So uh... How can I blur a image?

Here's the piece of code I wrote:



            using (var image = SKImage.FromEncodedData(SKData.Create(@"../../../images/testInput.bmp")))
            using (var surface = SKSurface.Create(info: new SKImageInfo(image.Width, image.Height)))
            using (var canvas = surface.Canvas)
            using (var paint = new SKPaint())
            using (var filter = SKMaskFilter.CreateBlur(blurStyle: SKBlurStyle.Normal, sigma: 10, flags: SKBlurMaskFilterFlags.HighQuality)) {
                paint.IsAntialias = true;
                paint.MaskFilter = filter;
                paint.BlendMode = SKBlendMode.Difference;
                canvas.DrawImage(image, 0, 0, paint);
                using (var snapshot = surface.Snapshot())
                using (var data = snapshot.Encode(format: SkiaSharp.SKEncodedImageFormat.Png, quality: 100)) {
                    if (data == null)
                        throw new InvalidOperationException(nameof(SkiaSharp) + " failed... And didn't explain why.");

                    using (var fileStream = new FileStream(@"../../../images/testOutput.bmp", FileMode.Create))
                        data.SaveTo(fileStream);
                }
                System.Environment.Exit(1);
                throw new NotImplementedException();
            }

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To post to this group, send email to skia-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

Hal Canary

unread,
Dec 10, 2017, 9:40:10 AM12/10/17
to skia-discuss

To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss+unsubscribe@googlegroups.com.

To post to this group, send email to skia-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages