Рисование мышкой в PictureBox
Ребята, кто знает вкратце код рисования мышкой на PictureBox?
Источник: Stack Overflow на русском
Ребята, кто знает вкратце код рисования мышкой на PictureBox?
Насколько я знаю, PictureBox служит только для отображения картинок, а не рисования; для рукописного ввода/рисования служит класс InkPresenter. Код XAML:
<Canvas>
<TextBlock Text="InkPresenter Control" FontWeight="Bold" Margin="50,30,0,0" />
<Rectangle Height="500" Width="500" Margin="50,50,0,0" Stroke="Black" />
<InkPresenter x:Name="MyIP" Height="500" Width="500"
Margin="50,50,0,0"
MouseLeftButtonDown="MyIP_MouseLeftButtonDown"
LostMouseCapture="MyIP_LostMouseCapture"
MouseMove="MyIP_MouseMove"
Background="Transparent" Opacity="1" />
</Canvas>
Код cs:
public Page()
{
InitializeComponent();
SetBoundary();
}
Stroke NewStroke;
// A new stroke object named MyStroke is created.
// MyStroke is added to the StrokeCollection of the InkPresenter named MyIP
private void MyIP_MouseLeftButtonDown(object sender, MouseEventArgs e)
{
MyIP.CaptureMouse();
StylusPointCollection MyStylusPointCollection = new StylusPointCollection();
MyStylusPointCollection.Add(e.StylusDevice.GetStylusPoints(MyIP));
NewStroke = new Stroke(MyStylusPointCollection);
MyIP.Strokes.Add(NewStroke);
}
// StylusPoint objects are collected from the MouseEventArgs and added to MyStroke.
private void MyIP_MouseMove(object sender, MouseEventArgs e)
{
if (NewStroke != null)
NewStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(MyIP));
}
// MyStroke is completed
private void MyIP_LostMouseCapture(object sender, MouseEventArgs e)
{
NewStroke = null;
}
// Set the Clip property of the inkpresenter so that the strokes
// are contained within the boundary of the inkpresenter
private void SetBoundary()
{
RectangleGeometry MyRectangleGeometry = new RectangleGeometry();
MyRectangleGeometry.Rect = new Rect(0, 0, MyIP.ActualWidth, MyIP.ActualHeight);
MyIP.Clip = MyRectangleGeometry;
}