示例#1
0
        public static BitmapSource OverlayInk(BitmapSource bitmapSource, double scale, StrokeCollection inkStrokes)
        {
            if (bitmapSource == null)
            {
                return((BitmapSource)null);
            }
            if (inkStrokes == null || inkStrokes.Count == 0)
            {
                return(bitmapSource);
            }
            Rect   rect1        = new Rect(0.0, 0.0, (double)bitmapSource.PixelWidth, (double)bitmapSource.PixelHeight);
            Vector offsetVector = (Vector)RectExtensions.GetCenter(rect1);
            Rect   rect2        = RectExtensions.Union(Enumerable.Select <Stroke, Rect>((IEnumerable <Stroke>)inkStrokes, (Func <Stroke, Rect>)(stroke => stroke.GetBounds())));

            rect2.Scale(scale, scale);
            rect2.Offset(offsetVector);
            Rect   rect3   = Rect.Union(rect1, rect2);
            Canvas canvas1 = new Canvas();

            canvas1.Width  = rect3.Width;
            canvas1.Height = rect3.Height;
            canvas1.Children.Add((UIElement) new Image()
            {
                Source = (ImageSource)bitmapSource
            });
            UIElementCollection children      = canvas1.Children;
            InkPresenter        inkPresenter1 = new InkPresenter();

            inkPresenter1.Strokes         = inkStrokes;
            inkPresenter1.Margin          = new Thickness(offsetVector.X, offsetVector.Y, -offsetVector.X, -offsetVector.Y);
            inkPresenter1.LayoutTransform = (Transform) new ScaleTransform(scale, scale);
            InkPresenter inkPresenter2 = inkPresenter1;

            children.Add((UIElement)inkPresenter2);
            Canvas canvas2   = canvas1;
            Point  newOrigin = new Point(-rect3.Left, -rect3.Top);

            EnumerableExtensions.ForEach <UIElement>(Enumerable.OfType <UIElement>((IEnumerable)canvas2.Children), (Action <UIElement>)(child => DependencyObjectExtensions.SetCanvasPos((DependencyObject)child, newOrigin)));
            return(new FrameworkElementCapturer((FrameworkElement)canvas2, ImageCapturer.MaxSize).Capture());
        }