private void InkCanvasOnSelectionChanged(object sender, EventArgs eventArgs) { _selectedElements = new Collection <UIElement>(); _selectedStrokes = new StrokeCollection(); _originPoints.Clear(); _rotatePoint = new Point(); LastAngle = 0; _workAreaModel.SelectionChanged(); _selectedElements = _inkCanvas.GetSelectedElements(); _selectedStrokes = _inkCanvas.GetSelectedStrokes(); if (!IsSelected) { return; } var selectionBounds = ModelStorage.WorkAreaModel.CurrentInkCanvas.GetSelectionBounds(); if (selectionBounds.IsEmpty) { return; } _rotatePoint = new Point(selectionBounds.X + (selectionBounds.Width / 2), selectionBounds.Y + (selectionBounds.Height / 2)); foreach (var element in _selectedElements) { var translatePoint = element.TranslatePoint(new Point(0, 0), _inkCanvas); var x = element.DesiredSize.Width.Equals(0) ? 0 : (_rotatePoint.X - translatePoint.X) / element.DesiredSize.Width; var y = element.DesiredSize.Height.Equals(0) ? 0 : (_rotatePoint.Y - translatePoint.Y) / element.DesiredSize.Height; _originPoints.Add(element, new Point(x, y)); } LastAngle = 0; }
public void CopyInkCanvasObjects(InkCanvas inkCanvas, InkCanvas clipboardCanvas) { var selectionBounds = inkCanvas.GetSelectionBounds(); if (selectionBounds.IsEmpty) { return; } Clear(clipboardCanvas); var selectedElements = inkCanvas.GetSelectedElements().ToList(); var selectedStrokes = new StrokeCollection(inkCanvas.GetSelectedStrokes()); ClearSelection(inkCanvas); var elements = new List <UIElement>(); WPFHelper.CloneElementsTo(elements, selectedElements.GetEnumerator(), clipboardCanvas); WPFHelper.CloneStrokesTo(clipboardCanvas.Strokes, selectedStrokes); elements.ForEach(element => clipboardCanvas.Children.Add(element)); inkCanvas.Select(selectedStrokes, selectedElements); var dataObject = new DataObject(); dataObject.SetData(ClipboardIdFormat, _clipboardStateId = Guid.NewGuid()); dataObject.SetData(DataFormats.Bitmap, ImageHelper.GetCroppedImageFromInkCanvas(selectionBounds, inkCanvas)); Clipboard.SetDataObject(dataObject); }
public void StrokeTest() { InkCanvas MyCanvas = new InkCanvas(); GlyphChanged glyph = new GlyphChanged(MyCanvas); Assert.AreEqual(MyCanvas.GetSelectedStrokes().Count, 0); }
public void OnInkCanvasPreviewMouseDown(object sender, MouseEventArgs e) { if (e == null) { return; } // Finger if (e.StylusDevice != null && e.StylusDevice.StylusButtons.Count == 1 && !this.fingerSelectionMode) { this.InkCanvas.EditingMode = InkCanvasEditingMode.GestureOnly; return; } // Stylus if (e.StylusDevice != null && e.StylusDevice.StylusButtons.Count == 2) { return; } this.InkCanvas.EditingMode = InkCanvasEditingMode.Select; InkCanvas ic = (InkCanvas)sender; Point pt = e.GetPosition(ic); if (ic.HitTestSelection(pt) == InkCanvasSelectionHitResult.Selection) { this.strokesToMove = ic.GetSelectedStrokes(); this.dragDropInProgress = true; this.initialDragPoint = pt; this.polledMouseCoords = new Point(pt.X, pt.Y); } }
public static void GroupSelectedDrawings(InkCanvas inkCanvas, ICollection <StrokeCollection> groupedStrokesCollections) { var groupedStrokeCollection = new StrokeCollection(inkCanvas.GetSelectedStrokes()); UnGroupSelectedDrawings(inkCanvas, groupedStrokesCollections); groupedStrokesCollections.Add(groupedStrokeCollection); }
void CodeExamples() { //<Snippet8> // Set the selection mode based on a checkbox if ((bool)cbSelectionMode.IsChecked) { theInkCanvas.EditingMode = InkCanvasEditingMode.Select; } else { theInkCanvas.EditingMode = InkCanvasEditingMode.Ink; } //</Snippet8> //<Snippet9> // Get the selected strokes from the InkCanvas StrokeCollection selection = theInkCanvas.GetSelectedStrokes(); // Check to see if any strokes are actually selected if (selection.Count > 0) { // Change the color of each stroke in the collection to red foreach (System.Windows.Ink.Stroke stroke in selection) { stroke.DrawingAttributes.Color = System.Windows.Media.Colors.Red; } } //</Snippet9> }
/// <summary> /// Delete selected stroke / shape /// </summary> /// <param name="sender"></param> /// <param name="routedEventArgs"></param> private void OnDeleteStroke(object sender, RoutedEventArgs routedEventArgs) { m_inkCanvas.Strokes.Remove(m_inkCanvas.GetSelectedStrokes()); for (int i = 0; i < m_inkCanvas.GetSelectedElements().Count; i++) { m_inkCanvas.Children.Remove(m_inkCanvas.GetSelectedElements()[i]); } }
private void OnColorPick_cp(object sender, RoutedPropertyChangedEventArgs <System.Windows.Media.Color?> e) { if (!(bool)changeColor_btn.IsChecked) { MyStrokeCollection strokeCollection = new MyStrokeCollection(InkCanvas.GetSelectedStrokes()); colorHandler.onColorPick(InkCanvas, colorPicker, canvasObjectHandler.getPolygonShape().getPolygon()); canvasObjectHandler.getPolygonShape().replaceSelectedStroke(InkCanvas); } }
public void Start(InkCanvas inkCanvas) { ClearRepetitionCollections(); _selectedElements = new List <UIElement>(inkCanvas.GetSelectedElements()); _selectedStrokes = new StrokeCollection(inkCanvas.GetSelectedStrokes()); inkCanvas.EditingMode = InkCanvasEditingMode.None; _startPoint = new Point(-100, -100); ProcessRepeateSelectionState = RepeateSelectionState.WaitingStartPoint; }
/// <summary> /// Constructor /// </summary> public PasteCmd(InkCanvas canvas, InkToSketchWPF.InkCanvasSketch sketch, Point pastePoint) { isUndoable = true; PastedOnce = false; inkCanvas = canvas; inkSketch = sketch; PastePoint = pastePoint; ReplacedStrokes = inkCanvas.GetSelectedStrokes(); }
public void SelectColor() { StrokeCollection _StrokeCollection = _currentInkCanvas.GetSelectedStrokes(); if (_StrokeCollection.Count > 0) { System.Windows.Forms.ColorDialog _ColorDialog = new System.Windows.Forms.ColorDialog(); if (_ColorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { foreach (Stroke _Stroke in _StrokeCollection) { Color _Color = Color.FromArgb(255, _ColorDialog.Color.R, _ColorDialog.Color.G, _ColorDialog.Color.B); _Stroke.DrawingAttributes.Color = _Color; //StreamGeometry _Geometry = _Stroke.GetGeometry(); } } } }
public void replaceSelectedStroke(InkCanvas _InkCanvas) { MyStrokeCollection strokeCollection = new MyStrokeCollection(_InkCanvas.GetSelectedStrokes()); printPolygon(strokeCollection, _InkCanvas); foreach (Stroke aStroke in strokeCollection) { _InkCanvas.Strokes.Remove(aStroke); } }
void InkCanvas1SelectionChanged(object sender, EventArgs e) { InkCanvas _InkCanvas = (InkCanvas)sender; if (Keyboard.IsKeyDown(Key.LeftCtrl)) { if (_oldStrokeCollection != null) { foreach (Stroke _Stroke in _InkCanvas.GetSelectedStrokes()) { if (!_oldStrokeCollection.Contains(_Stroke)) { _oldStrokeCollection.Add(_Stroke); } } _InkCanvas.Select(_oldStrokeCollection); } } _oldStrokeCollection = _InkCanvas.GetSelectedStrokes(); }
public ImageEditorWindow() { InitializeComponent(); if (DataContext is ImageEditorViewModel vm) { vm.ObserveProperty(M => M.TransformedBitmap) .Subscribe(M => UpdateInkCanvas()); vm.InkCanvas = InkCanvas; vm.Window = this; InkCanvas.Strokes.StrokesChanged += (S, E) => { var item = new StrokeHistory(); item.Added.AddRange(E.Added); item.Removed.AddRange(E.Removed); vm.AddInkHistory(item); }; void SelectionMovingOrResizing(object Sender, InkCanvasSelectionEditingEventArgs Args) { vm.AddSelectHistory(new SelectHistory { NewRect = Args.NewRectangle, OldRect = Args.OldRectangle, Selection = InkCanvas.GetSelectedStrokes() }); } InkCanvas.SelectionMoving += SelectionMovingOrResizing; InkCanvas.SelectionResizing += SelectionMovingOrResizing; var inkCanvasVm = vm.InkCanvasViewModel; inkCanvasVm .SelectedColor .Subscribe(M => InkCanvas.DefaultDrawingAttributes.Color = M); inkCanvasVm .BrushSize .Subscribe(M => InkCanvas.DefaultDrawingAttributes.Height = InkCanvas.DefaultDrawingAttributes.Width = M); inkCanvasVm .SelectedTool .Subscribe(M => InkCanvas.SetInkTool(M)); } Image.SizeChanged += (S, E) => UpdateInkCanvas(); InkCanvas.DefaultDrawingAttributes.FitToCurve = true; }
public ImageEditorWindow() { InitializeComponent(); if (DataContext is ImageEditorViewModel vm) { vm.PropertyChanged += (S, E) => { if (E.PropertyName == nameof(vm.TransformedBitmap)) { UpdateInkCanvas(); } }; vm.InkCanvas = InkCanvas; InkCanvas.Strokes.StrokesChanged += (S, E) => { var item = new StrokeHistory(); item.Added.AddRange(E.Added); item.Removed.AddRange(E.Removed); vm.AddInkHistory(item); }; void SelectionMovingOrResizing(object Sender, InkCanvasSelectionEditingEventArgs Args) { vm.AddSelectHistory(new SelectHistory { NewRect = Args.NewRectangle, OldRect = Args.OldRectangle, Selection = InkCanvas.GetSelectedStrokes() }); } InkCanvas.SelectionMoving += SelectionMovingOrResizing; InkCanvas.SelectionResizing += SelectionMovingOrResizing; vm.Window = this; } Image.SizeChanged += (S, E) => UpdateInkCanvas(); ModesBox.SelectedIndex = 0; _settings = ServiceProvider.Get <Settings>().ImageEditor; ColorPicker.SelectedColor = _settings.BrushColor.ToWpfColor(); SizeBox.Value = _settings.BrushSize; InkCanvas.DefaultDrawingAttributes.FitToCurve = true; }
public static void UnGroupSelectedDrawings(InkCanvas inkCanvas, ICollection <StrokeCollection> groupedStrokesCollections) { foreach (var selectedStroke in inkCanvas.GetSelectedStrokes()) { var stroke = selectedStroke; groupedStrokesCollections.Where(strokeCollection => strokeCollection.Contains(stroke)) .ToList() .ForEach(strokeCollection => strokeCollection.Remove(stroke)); } groupedStrokesCollections.Where(strokeCollection => strokeCollection.Count == 0) .ToList() .ForEach(strokeCollection => groupedStrokesCollections.Remove(strokeCollection)); }
/// <summary> /// Unmagnifies the sketch by a preset amount and notifies listerners. /// </summary> public void ZoomOut(int pixels) { StrokeCollection oldSelection = InkCanvas.GetSelectedStrokes(); SelectAllStrokes(); System.Windows.Rect oldBounds = InkCanvas.GetSelectionBounds(); System.Windows.Rect smallerBounds = new System.Windows.Rect(oldBounds.TopLeft, new System.Windows.Size(oldBounds.Width * SketchPanelConstants.ZoomOutFactor, oldBounds.Height * SketchPanelConstants.ZoomOutFactor)); CommandList.MoveResizeCmd resize = new CommandList.MoveResizeCmd(ref inkSketch, oldBounds, smallerBounds); CM.ExecuteCommand(resize); InkCanvas.Select(oldSelection); InkChanged(false); }
//обработчик, который будет срабатывать при возникновении события ValueChanged - изменении значения слайдера и изменять толщину кисти в соответсвтии с ним public void GlyphSize(double value) { var drawingAttributes = MyCanvas.DefaultDrawingAttributes; //определяет внешний вид пера (выбираются стандартные настройки) drawingAttributes.Width = value; //ширина кисти drawingAttributes.Height = value; //высота кисти // изменение ширины выделенных глифов if (this.MyCanvas.GetSelectedStrokes().Count > 0) { foreach (Stroke stroke in MyCanvas.GetSelectedStrokes()) { stroke.DrawingAttributes.Width = value; stroke.DrawingAttributes.Height = value; } } }
public void SelectCanvas(int canvasIndex) { InkCanvas oldInkCanvas = _currentInkCanvas; _currentInkCanvas = (InkCanvas)_canvasList.Children[canvasIndex]; //copy images & strokes if (oldInkCanvas != null) { foreach (Image uIElement in oldInkCanvas.GetSelectedElements().OfType <Image>()) { oldInkCanvas.Children.Remove(uIElement); _currentInkCanvas.Children.Add(uIElement); } foreach (Stroke stroke in oldInkCanvas.GetSelectedStrokes()) { if (Keyboard.IsKeyDown(Key.LeftShift)) { _currentInkCanvas.Strokes.Add(stroke.Clone()); } if (Keyboard.IsKeyDown(Key.LeftAlt)) { oldInkCanvas.Strokes.Remove(stroke); _currentInkCanvas.Strokes.Add(stroke); } } } //hide all canvases foreach (InkCanvas inkCanvas in _canvasList.Children) { inkCanvas.Opacity = .2; inkCanvas.IsHitTestVisible = false; } _currentInkCanvas.IsHitTestVisible = true; _currentInkCanvas.Opacity = 1; //SetMode(InkCanvasEditingMode.Select, CustomMode.select); _currentInkCanvas.Select(new StrokeCollection()); if (null != oldInkCanvas) { oldInkCanvas.Select(new StrokeCollection()); } }
public void SelectCanvas(int i) { InkCanvas _oldInkCanvas = _InkCanvas; _InkCanvas = (InkCanvas)_CanvasList.Children[i]; if (_oldInkCanvas != null) { foreach (Image _UIElement in _oldInkCanvas.GetSelectedElements().OfType <Image>().ToList()) { _oldInkCanvas.Children.Remove(_UIElement); _InkCanvas.Children.Add(_UIElement); } foreach (Stroke _Stroke in _oldInkCanvas.GetSelectedStrokes()) { if (Keyboard.IsKeyDown(Key.LeftShift)) { _InkCanvas.Strokes.Add(_Stroke.Clone()); } if (Keyboard.IsKeyDown(Key.LeftAlt)) { _oldInkCanvas.Strokes.Remove(_Stroke); _InkCanvas.Strokes.Add(_Stroke); } } } foreach (InkCanvas _InkCanvas1 in _CanvasList.Children) { _InkCanvas1.Opacity = .2; _InkCanvas1.IsHitTestVisible = false; } _InkCanvas.IsHitTestVisible = true; _InkCanvas.Opacity = 1; //SetMode(InkCanvasEditingMode.Select, CustomMode.select); _InkCanvas.Select(new StrokeCollection()); if (null != _oldInkCanvas) { _oldInkCanvas.Select(new StrokeCollection()); } }
//<Snippet3> void InkCanvas_PreviewMouseDown(object sender, MouseEventArgs e) { InkCanvas ic = (InkCanvas)sender; Point pt = e.GetPosition(ic); // If the user is moving selected strokes, prepare the strokes to be // moved to another InkCanvas. if (ic.HitTestSelection(pt) == InkCanvasSelectionHitResult.Selection) { StrokeCollection selectedStrokes = ic.GetSelectedStrokes(); StrokeCollection strokesToMove = selectedStrokes.Clone(); // Remove the offset of the selected strokes so they // are positioned when the strokes are dropped. Rect inkBounds = strokesToMove.GetBounds(); TranslateStrokes(strokesToMove, -inkBounds.X, -inkBounds.Y); // Perform drag and drop. MemoryStream ms = new MemoryStream(); strokesToMove.Save(ms); DataObject dataObject = new DataObject( StrokeCollection.InkSerializedFormat, ms); DragDropEffects effects = DragDrop.DoDragDrop(ic, dataObject, DragDropEffects.Move); if ((effects & DragDropEffects.Move) == DragDropEffects.Move) { // Remove the selected strokes // from the current InkCanvas. ic.Strokes.Remove(selectedStrokes); } } }
public static void SelectionChanged(InkCanvas inkCanvas, ICollection <StrokeCollection> groupedStrokesCollections) { lock (SelectionChangedInProgress) { if (SelectionChangedInProgress.Contains(inkCanvas)) { return; } SelectionChangedInProgress.Add(inkCanvas); } try { var selectedStrokes = inkCanvas.GetSelectedStrokes(); var uniqueStrokesCollection = new HashSet <Stroke>(selectedStrokes); foreach (var groupedStrokesCollection in groupedStrokesCollections) { if (!groupedStrokesCollection.Any(uniqueStrokesCollection.Contains)) { continue; } foreach (var stroke in groupedStrokesCollection) { uniqueStrokesCollection.Add(stroke); } } inkCanvas.Select(new StrokeCollection(uniqueStrokesCollection), inkCanvas.GetSelectedElements()); } finally { lock (SelectionChangedInProgress) { if (SelectionChangedInProgress.Contains(inkCanvas)) { SelectionChangedInProgress.Remove(inkCanvas); } } } }
private void InkCanvas_SelectionChanged(object sender, EventArgs e) { var selectedStrokes = InkCanvas.GetSelectedStrokes(); using (MemoryStream ms = new MemoryStream()) { selectedStrokes.Save(ms); var myInkCollector = new InkCollector(); var ink = new Ink(); ink.Load(ms.ToArray()); using (RecognizerContext myRecoContext = new RecognizerContext()) { RecognitionStatus status = RecognitionStatus.ProcessFailed; myRecoContext.Strokes = ink.Strokes; try { var recoResult = myRecoContext.Recognize(out status); if (status == RecognitionStatus.NoError) { textBlock.Text = recoResult.TopString; //InkCanvas.Strokes.Clear(); } else { MessageBox.Show("ERROR: " + status.ToString()); } } catch (Exception) { //MessageBox.Show("ERROR: " + status.ToString()); } } } }
/// <summary> /// OnPreviewMouseDown is called before InkCanvas sees the Down. We use this /// handler to hit test any selection and initiate a drag and drop operation /// if the user clicks on the selection to move it. /// </summary> private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { InkCanvas inkCanvas = (InkCanvas)sender; Point downPosition = e.GetPosition(inkCanvas); //see if we hit the selection, and not a grab handle if (inkCanvas.HitTestSelection(downPosition) == InkCanvasSelectionHitResult.Selection) { //clone the selected strokes so we can transform them without affecting the actual selection StrokeCollection selectedStrokes = inkCanvas.GetSelectedStrokes(); List <UIElement> selectedElements = new List <UIElement>(inkCanvas.GetSelectedElements()); DataObject dataObject = new DataObject(); //copy ink to the clipboard if we have any. we do this even if there are no //strokes since clonedStrokes will be used by the Xaml clipboard code below StrokeCollection clonedStrokes = selectedStrokes.Clone(); if (clonedStrokes.Count > 0) { //translate the strokes relative to the down position Matrix translation = new Matrix(); translation.Translate(-downPosition.X, -downPosition.Y); clonedStrokes.Transform(translation, false); //save the strokes to a dataobject to use during the dragdrop operation MemoryStream ms = new MemoryStream(); clonedStrokes.Save(ms); ms.Position = 0; dataObject.SetData(StrokeCollection.InkSerializedFormat, ms); //we don't close the MemoryStream here, we'll do it in OnDrop } //Now we're going to add Xaml to the dragdrop dataobject. We'll create an //InkCanvas and add any selected strokes and elements in the selection to it InkCanvas inkCanvasForDragDrop = new InkCanvas(); foreach (UIElement childElement in selectedElements) { //we can't add elements in the selection to the InkCanvas that //represents selection (since they are already parented to the //inkCanvas that has the selection) so we need to clone them. //To clone each element, we need to convert it to Xaml and back again string childXaml = XamlWriter.Save(childElement); UIElement clonedChildElement = (UIElement)XamlReader.Load(new XmlTextReader(new StringReader(childXaml))); //adjust top and left relative to the down position double childLeft = InkCanvas.GetLeft(clonedChildElement); InkCanvas.SetLeft(clonedChildElement, childLeft - downPosition.X); double childTop = InkCanvas.GetTop(clonedChildElement); InkCanvas.SetTop(clonedChildElement, childTop - downPosition.Y); inkCanvasForDragDrop.Children.Add(clonedChildElement); } //last, add the cloned strokes in case our drop location only supports Xaml //this preserves both the ink and the selected elements inkCanvasForDragDrop.Strokes = clonedStrokes; //now copy the Xaml for the InkCanvas that represents selection to the clipboard string inkCanvasXaml = XamlWriter.Save(inkCanvasForDragDrop); dataObject.SetData(DataFormats.Xaml, inkCanvasXaml); //The call to DragDrop.DoDragDrop will block until the drag and drop operation is completed //once it does, 'effects' will have been updated by OnDragOver getting called //if we're moving the strokes and elements, we'll remove them. If we're copying them //(the CTRL key is pressed) we won't remove them. DragDropEffects effects = DragDrop.DoDragDrop(inkCanvas, dataObject, DragDropEffects.Move | DragDropEffects.Copy); if (effects == DragDropEffects.Move) { inkCanvas.Strokes.Remove(selectedStrokes); foreach (UIElement childElement in selectedElements) { inkCanvas.Children.Remove(childElement); } } } }
/// <summary> /// Create the move/resize command. /// Note: We are subscribing to the before-erase hook so we can get the stroke's bounds. /// </summary> protected void inkCanvas_SelectionMovingResizing(object sender, InkCanvasSelectionEditingEventArgs e) { // Create the command with arguments from before-the-event hook c = new CommandList.MoveResizeCmd(ref inkSketch, InkCanvas.GetSelectedStrokes(), e.OldRectangle, e.NewRectangle); }