我知道,如何使用CanvasDrawingSession绘制具有笔触粗细的矩形。但是问题是,笔触粗细在矩形的内部和外部增长。我需要笔触粗细只能在矩形的内部增长。我不知道如何做到这一点! 我试过的是?
我的xaml代码:
<Canvas x:Name="MyCanvas"> </Canvas>
我的c#代码:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
CanvasVirtualControl canvasVirtualControl = new CanvasVirtualControl();
canvasVirtualControl.Width = 1486;
canvasVirtualControl.Height = 610;
MyCanvas.Children.Add(canvasVirtualControl);
Canvas.SetLeft(canvasVirtualControl, 0);
Canvas.SetTop(canvasVirtualControl, 100);
canvasVirtualControl.RegionsInvalidated += CanvasVirtualControl_RegionsInvalidated;
}
private void CanvasVirtualControl_RegionsInvalidated(CanvasVirtualControl sender, CanvasRegionsInvalidatedEventArgs args)
{
CanvasDrawingSession drawingSession;
Rect rect = new Rect(args.InvalidatedRegions[0].Left, args.InvalidatedRegions[0].Top, args.InvalidatedRegions[0].Width, args.InvalidatedRegions[0].Height);
using (drawingSession = sender.CreateDrawingSession(rect))
{
drawingSession.DrawRectangle(new Rect(0, 0, 200, 200), Windows.UI.Color.FromArgb(255, 255, 0, 0), 40);
}
}
为什么这样绘制矩形?我想这是因为矩形内部和外部都增加了笔触厚度。如何解决带有矩形厚度的绘制矩形仅在矩形内部生成?
更新的问题:
在使CanvasVirtualControl的整个区域无效的同时,笔触厚度在矩形的内部和外部增大。如果我使Invas无效的CanvasVirtualControl的特定矩形区域,strokeThickness仅在内部增大。
我的Xaml代码:
<Canvas x:Name="MyCanvas">
<Button Content="InvalidateWholeRegion" Height="100" Canvas.Left="0" Canvas.Top="0" Click="InvalidateWholeRegion"/>
<Button Content="InvalidateParticularRegion" Height="100" Canvas.Left="300" Canvas.Top="0" Click="InvalidateParticularRegion"/>
</Canvas>
我的c#代码:
CanvasVirtualControl canvasVirtualControl;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
canvasVirtualControl = new CanvasVirtualControl();
canvasVirtualControl.Width = 1486;
canvasVirtualControl.Height = 610;
MyCanvas.Children.Add(canvasVirtualControl);
Canvas.SetLeft(canvasVirtualControl, 0);
Canvas.SetTop(canvasVirtualControl, 100);
canvasVirtualControl.RegionsInvalidated += CanvasVirtualControl_RegionsInvalidated;
}
private void CanvasVirtualControl_RegionsInvalidated(CanvasVirtualControl sender, CanvasRegionsInvalidatedEventArgs args)
{
CanvasDrawingSession drawingSession;
Rect rect = new Rect(args.InvalidatedRegions[0].Left, args.InvalidatedRegions[0].Top, args.InvalidatedRegions[0].Width, args.InvalidatedRegions[0].Height);
using (drawingSession = sender.CreateDrawingSession(rect))
{
drawingSession.DrawRectangle(new Rect(rect.X, rect.Y, 100, 100), Windows.UI.Color.FromArgb(255, 255, 0, 0), 40);
}
}
private void InvalidateParticularRegion(object sender, RoutedEventArgs e)
{
canvasVirtualControl.Invalidate(new Rect(200, 200, 100, 100));
}
private void InvalidateWholeRegion(object sender, RoutedEventArgs e)
{
canvasVirtualControl.Invalidate(new Rect(0, 0, 1486, 610));
}
如何绘制笔划在使特定区域无效时,厚膜也会长到外面吗?
答案 0 :(得分:0)
但是问题是,笔划厚度在矩形的内部和外部增大。我需要笔划厚度必须仅在矩形的内部增大。
DrawRectangle
方法的绘制路径是笔画的中心,它是设计使然。当您将Rect
设置为 0、0、200、200 时,捆绑包将裁剪左上角的一半。因此它将绘制一个如上的矩形。
如果要绘制完整的矩形,则需要确保起点是合理的值。因此,我们需要将Rect
修改为 20、20、200、200 。
private void CanvasVirtualControl_RegionsInvalidated(CanvasVirtualControl sender, CanvasRegionsInvalidatedEventArgs args)
{
CanvasDrawingSession drawingSession;
Rect rect = new Rect(args.InvalidatedRegions[0].Left, args.InvalidatedRegions[0].Top, args.InvalidatedRegions[0].Width, args.InvalidatedRegions[0].Height);
using (drawingSession = sender.CreateDrawingSession(rect))
{
var dashedStroke = new CanvasStrokeStyle()
{
LineJoin = CanvasLineJoin.Round
};
drawingSession.DrawRectangle(new Rect(20, 20, 200, 200), Windows.UI.Color.FromArgb(255, 255, 0, 0),40,dashedStroke);
}
}