Change InkDrawingAttributes Opacity for Pen brush

Anderson Cavalcante 21 Reputation points
2023-01-26T14:51:19.3+00:00

I using a color picker to draw with a pen, but using this code I can't change de opacity of the pen color:

InkDrawingAttributes inkDrawingAttributes = InkCanvas.InkPresenter.CopyDefaultDrawingAttributes();

inkDrawingAttributes.Color = ColorPenSelected;

InkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(inkDrawingAttributes);

This way works well with a pen:

User's image

But, using the InkCanvas.InkPresenter.CopyDefaultDrawingAttributes() the inkDrawingAttributes.PencilProperties is null and I can't change the Opacity. It is not allowed to change the opacity.

I could change the opacity with this code:

InkDrawingAttributes inkDrawingAttributes = InkDrawingAttributes.CreateForPencil();

inkDrawingAttributes.Color = ColorPenSelected;

inkDrawingAttributes.PencilProperties.Opacity = (double)ColorPenSelected.A * 5 / 255;

InkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(inkDrawingAttributes);

Using as a pencil in CreateForPencil().

Now, I could change the Opacity. However, the brush texture is different, even using Opacity 100%, compared to the first image. There are many dots in the line, instead an unique line:

User's image

So, How can I change the opacity for the Pen brush and keep the same texture as the first image? With a continuous line, without dots as in the second image.

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,989 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
745 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 31,481 Reputation points Microsoft Vendor
    2023-02-02T02:59:58.07+00:00

    Hello,

    Welcome to Microsoft Q&A!

    After talking with the team, I have to say that this behavior is expected. The InkStroke of the default pen will always be rendered in full opacity. The InkDrawingAttributes.Color Property accept ARGB values but the value of the transparency component (A, or alpha channel) is ignored.

    So you can't change the opacity of the InkDrawingAttributes for the default pen brush.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments