如何为InkCanvas画笔颜色设置笔触不透明度?如何设置UWP中墨迹笔触不透明度?
Hey there! Let's tackle both of your ink opacity questions one by one—super straightforward once you know where to look.
In WPF, the InkCanvas uses DrawingAttributes to control all stroke-related properties, including opacity. You can set this either directly in XAML or through code-behind:
XAML Approach
Just define the DrawingAttributes inline within your InkCanvas, and set the Opacity property (values range from 0.0 for fully transparent to 1.0 for fully opaque):
<InkCanvas> <InkCanvas.DefaultDrawingAttributes> <DrawingAttributes Color="Blue" Opacity="0.5" /> </InkCanvas.DefaultDrawingAttributes> </InkCanvas>
Code-Behind Approach
If you need to adjust opacity dynamically (like responding to a slider input), create a DrawingAttributes instance, set its properties, and assign it to the InkCanvas's DefaultDrawingAttributes:
// Initialize DrawingAttributes with desired opacity var drawingAttrs = new DrawingAttributes { Color = Colors.Red, Opacity = 0.3, // 30% opacity Width = 5, Height = 5 }; // Assign to your InkCanvas control myInkCanvas.DefaultDrawingAttributes = drawingAttrs;
UWP's InkCanvas relies on the InkPresenter to manage stroke settings, and it uses DrawingAttributes (from the Windows.UI.Input.Inking namespace) to control opacity. Here's how to implement it:
XAML Approach
You can set default stroke attributes directly on the InkPresenter nested inside your InkCanvas:
<InkCanvas> <InkCanvas.InkPresenter> <InkPresenter> <InkPresenter.DefaultDrawingAttributes> <DrawingAttributes Color="Green" Opacity="0.6" /> </InkPresenter.DefaultDrawingAttributes> </InkPresenter> </InkCanvas.InkPresenter> </InkCanvas>
Code-Behind Approach
For dynamic opacity changes, create a DrawingAttributes object, configure the Opacity property, and assign it to the InkPresenter's DefaultDrawingAttributes:
using Windows.UI.Input.Inking; // Create and configure DrawingAttributes var inkAttrs = new DrawingAttributes { Color = Windows.UI.Colors.Purple, Opacity = 0.4, // 40% transparency Size = new Windows.Foundation.Size(8, 8) }; // Assign to your InkCanvas's InkPresenter myInkCanvas.InkPresenter.DefaultDrawingAttributes = inkAttrs;
Quick side note: If you want to adjust the opacity of existing strokes (not just new ones), loop through the InkPresenter.StrokeContainer.GetStrokes() collection and update each stroke's DrawingAttributes.Opacity property individually.
内容的提问来源于stack exchange,提问作者Santhiya

