Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #453 ink strokes not captured until stylus up #540

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Fixed overlay distortion issues by tweaking DrawingGroup instead of m…
…argins
  • Loading branch information
pntmass committed Oct 26, 2019
commit f85b908d4c8d0399a107ff99902a699fc6e24f96
35 changes: 19 additions & 16 deletions ScreenToGif/Controls/InkCanvasExtended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public InkCanvasExtended()
// element and once its stroke is brought over to the UI thread it will
// be in front of previous strokes.
Canvas.SetZIndex(InkingImage, 99);

}
/// <summary>
/// Gets or set the eraser shape
Expand All @@ -51,6 +52,16 @@ public InkCanvasExtended()
/// </summary>
private Image InkingImage { get; }

/// <summary>
/// Tiny transparent corner point to prevent whitespace before DrawingGroup.TopLeft being cropped by Image control
/// </summary>
private readonly GeometryDrawing TransparentCornerPoint =
new GeometryDrawing(
new SolidColorBrush(Color.FromArgb(0, 255, 255, 255)),
new Pen(Brushes.White, 0.1),
new EllipseGeometry(new Point(0, 0), 0.1, 0.1)
).GetAsFrozen() as GeometryDrawing;

/// <summary>
/// Re-initializes InkingImage (the overlay that receives DrawingGroups from the inking thread)
/// </summary>
Expand Down Expand Up @@ -117,28 +128,20 @@ public void UpdateInkOverlay()
// all all its descendent DrawingGroups to this DrawingGroup that we are
// just using as a collection to return from the thread.
DrawingGroup drawingGroups = new DrawingGroup();

// This is a little jenky:
// We add a point in the top left so the DrawingGroup will be
// appropriately offset from the top left of the Image container.
drawingGroups.Children.Add(TransparentCornerPoint);

// We try to add both the visualTagets (in case both are active)
visualTarget1?.RootVisual?.visualToFrozenDrawingGroup(drawingGroups);
visualTarget2?.RootVisual?.visualToFrozenDrawingGroup(drawingGroups);

return drawingGroups.GetAsFrozen();
},
rawInkHostVisuals) as DrawingGroup;


// This is a little jenky, but we need to set the image margins otherwise
// the drawing content will be cropped and aligned top-left despite inking
// very far from origin. Because we set image to an empty drawingImage
// earlier, we don't need to worry about things visibly jumping on screen.
// Important note: Negatives are okay. If we set X,Y = (0,0), then the
// DrawingGroup.Bounds.TopLeft = (-3,0) [because user inked off the canvas
// for instance], then the point starting at (-3,0) will be shifted to (0,0)
// and our entire drawing will be moved over by 3 pixels, so here we just
// set the margin to whatever *negative* number the TopLeft has in that case,
// and we care only about going over the actual width/height (e.g. infinity)
var bounds = inkDrawingGroup.Bounds.TopLeft;
var w = System.Math.Min(bounds.X, this.ActualWidth);
var h = System.Math.Min(bounds.Y, this.ActualHeight);
InkingImage.Margin = new System.Windows.Thickness(w, h, 0, 0);

// At this point, just update the image source with the drawing image.
InkingImage.Source = new DrawingImage(inkDrawingGroup);
}
Expand Down