How to make Win2D BlendEffect apply to current drawing surface (background)?

129 views Asked by At

I want to draw some images on to existing canvas using multiply blend mode. However, I don't know how to do it as the BlendEffect class require me to assign the Background variable but that is suppose to be the canvas which I could not put there.

    private void OnDrawCanvas(CanvasControl sender, CanvasDrawEventArgs args)
    {
        var list = new LinkedList<ImageNode>();
    
        mRootNode.GetTraverseList(list, false);
    
        foreach (var item in list)
        {
            if (!item.treeVisible)
                continue;
            
            if (item.mLayerPixels != null)
            {
                if (item.mLayer.BlendModeKey == BlendModeType.MULTIPLY)
                {
                    var blendEffect = new BlendEffect()
                    {
                        //Background = ???, // what to put????
                        Foreground = item.mLayerPixels,
                        Mode = BlendEffectMode.Multiply
                    };
                    args.DrawingSession.DrawImage(blendEffect, item.mLayer.Left, item.mLayer.Top);
                }
                else
                {
    
                    args.DrawingSession.DrawImage(item.mLayerPixels, item.mLayer.Left, item.mLayer.Top);
                }
            }
        }
    
    }

1

There are 1 answers

0
Nick On

I ended up creating an offscreen CanvasRenderTarget to do the blending. When all the drawing is done, I create a CanvasBitmap from CanvasRenderTarget which allow me to draw the final result to the UI with args.DrawingSession.DrawImage();