nokia Imaging SDK customize BlendFilter

2019-09-01 04:32发布

问题:

I have created this code

Uri _blendImageUri = new Uri(@"Assets/1.png", UriKind.Relative);
var _blendImageProvider = new StreamImageSource((System.Windows.Application.GetResourceStream(_blendImageUri).Stream));

var bf = new BlendFilter(_blendImageProvider);

Filter work nice. But I want change image size for ForegroundSource property. How can I load image with my size?

回答1:

If I understood you correctly you are trying to blend ForegroundSource with only a part of the original image? That is called local blending at it is currently not supported on the BlendFilter itself.

You can however use ReframingFilter to reframe the ForegroundSource and then blend it. Your chain will look like something like this:

using (var mainImage = new StreamImageSource(...))
using (var filterEffect = new FilterEffect(mainImage))
{
    using (var secondaryImage = new StreamImageSource(...))
    using (var secondaryFilterEffect = new FilterEffect(secondaryImage))
    using (var reframing = new ReframingFilter(new Rect(0, 0, 500, 500), 0))    //reframe your image, thus "setting" the location and size of the content when blending
    {
        secondaryFilterEffect.Filters = new [] { reframing };

        using (var blendFilter = new BlendFilter(secondaryFilterEffect)
        using (var renderer = new JpegRenderer(filterEffect))
        {
            filterEffect.Filters = new [] { blendFilter };

            await renderer.RenderAsync();
        }
    }
}

As you can see, you can use the reframing filter to position the content of your ForegroundSource so that it will only blend locally. Note that when reframeing you can set the borders outside of the image location (for example new Rect(-100, -100, 500, 500)) and the areas outside of the image will appear as black transparent areas - exactly what you need in BlendFilter.