Exemplo n.º 1
0
        private async void CanvasCreateResources(object o, RoutedEventArgs routedEventArgs)
        {
            // initialize current displayinfo
            UpdateDisplayInfo();
            DisplayInformation.DisplayContentsInvalidated += DisplayInformation_DisplayContentsInvalidated;
            // create swapchain
            var swapChain = new CanvasSwapChain(CanvasDevice.GetSharedDevice(), (float)CanvasWidth, (float)CanvasHeight, 96);

            _canvas.SwapChain = swapChain;
            // create back buffer
            _buffer    = CanvasRenderTargetExtension.CreateEmpty(_canvas, new Size(CanvasWidth, CanvasHeight));
            _tmpBuffer = CanvasRenderTargetExtension.CreateEmpty(_canvas, new Size(CanvasWidth, CanvasHeight));
            // create default layer
            AddLayer();
            _background = await CanvasBitmap.LoadAsync(_canvas.SwapChain, new Uri("ms-appx:///PaintCanvas/Assets/canvas.png"));

            //_canvas.Invalidate();
            Invalidate();
            Win2dInitialized?.Invoke(this, EventArgs.Empty);
            // initialize background input thread

            ThreadPool.RunAsync(_ =>
            {
                // touch processor
                _inputSource = _canvas.CreateCoreIndependentInputSource(
                    CoreInputDeviceTypes.Touch | CoreInputDeviceTypes.Mouse | CoreInputDeviceTypes.Pen
                    );
                _inputSource.PointerPressed      += CanvasPointerPressed;
                _inputSource.PointerMoved        += CanvasPointerMoved;
                _inputSource.PointerReleased     += CanvasPointerReleased;
                _inputSource.PointerCaptureLost  += CanvasPointerReleased;
                _inputSource.PointerWheelChanged += CanvasWheelChanged;
                // setup gesture recognizer
                _recognizer = new GestureRecognizer {
                    AutoProcessInertia = true
                };
                _recognizer.GestureSettings =
                    GestureSettings.ManipulationTranslateInertia | GestureSettings.ManipulationTranslateRailsX |
                    GestureSettings.ManipulationTranslateRailsY | GestureSettings.ManipulationTranslateX |
                    GestureSettings.ManipulationTranslateY |
                    GestureSettings.ManipulationScale | GestureSettings.ManipulationScaleInertia;
                _recognizer.ManipulationUpdated += _recognizer_ManipulationUpdated;
                _inputSource.Dispatcher.ProcessEvents(CoreProcessEventsOption.ProcessUntilQuit);
            }, WorkItemPriority.High, WorkItemOptions.TimeSliced);
        }
Exemplo n.º 2
0
        public async Task <bool> Export(IRandomAccessStream saveTo, Guid encoderId)
        {
            var canvas = CanvasRenderTargetExtension.CreateEmpty(_canvas, new Size(CanvasWidth, CanvasHeight), 96);

            using (var ds = canvas.CreateDrawingSession())
            {
                DrawFrame(ds);
            }
            var pixels  = canvas.GetPixelBytes();
            var encoder = await BitmapEncoder.CreateAsync(encoderId, saveTo);

            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
                                 (uint)CanvasWidth, (uint)CanvasHeight, 96, 96, pixels);
            await encoder.FlushAsync();

            canvas.Dispose();
            return(true);
        }
Exemplo n.º 3
0
        public InkLayer AddLayer(string name)
        {
            var layer = new InkLayer
            {
                Image     = CanvasRenderTargetExtension.CreateEmpty(_canvas, new Size(CanvasWidth, CanvasHeight)),
                Name      = name,
                IsVisible = true,
                Opacity   = 100
            };

            layer.PropertyChanged += Layer_PropertyChanged;
            using (var ds = layer.Image.CreateDrawingSession())
            {
                ds.Clear();
            }
            _layers.Insert(0, layer);
            ActiveLayer = layer;
            return(layer);
        }
Exemplo n.º 4
0
        public async Task <bool> ImportPicture(IRandomAccessStream stream)
        {
            var layer = new InkLayer
            {
                Image     = CanvasRenderTargetExtension.CreateEmpty(_canvas, new Size(CanvasWidth, CanvasHeight)),
                Name      = "Imported",
                IsVisible = true,
                Opacity   = 100
            };

            layer.PropertyChanged += Layer_PropertyChanged;
            using (var bitmap = await CanvasBitmap.LoadAsync(_canvas.SwapChain, stream))
                using (var ds = layer.Image.CreateDrawingSession())
                {
                    ds.Clear();
                    ds.DrawImage(bitmap);
                }
            _layers.Add(layer);
            //_canvas.Invalidate();
            Invalidate();
            return(true);
        }
Exemplo n.º 5
0
 private void CanvasSizeChanged(object sender, SizeChangedEventArgs sizeChangedEventArgs)
 {
     _canvas?.SwapChain?.ResizeBuffers((float)CanvasWidth, (float)CanvasHeight, 96);
     foreach (var layer in _layers)
     {
         var image = CanvasRenderTargetExtension.CreateEmpty(_canvas, new Size(CanvasWidth, CanvasHeight));
         using (var ds = image.CreateDrawingSession())
         {
             ds.DrawImage(layer.Image);
         }
         var old = layer.Image;
         layer.Image = image;
         old.Dispose();
     }
     if (_buffer == null)
     {
         return;
     }
     _buffer.Dispose();
     _tmpBuffer.Dispose();
     _buffer    = CanvasRenderTargetExtension.CreateEmpty(_canvas, new Size(CanvasWidth, CanvasHeight));
     _tmpBuffer = CanvasRenderTargetExtension.CreateEmpty(_canvas, new Size(CanvasWidth, CanvasHeight));
     Invalidate();
 }