public async Task DrawSurface(CompositionDrawingSurface surface, Uri uri, Size size) { var canvasDevice = CanvasComposition.GetCanvasDevice(_graphicsDevice); using (var canvasBitmap = await CanvasBitmap.LoadAsync(canvasDevice, uri)) { var bitmapSize = canvasBitmap.Size; // // Because the drawing is done asynchronously and multiple threads could // be trying to get access to the device/surface at the same time, we need // to do any device/surface work under a lock. // lock (_drawingLock) { Size surfaceSize = size; if (surfaceSize.IsEmpty) { // Resize the surface to the size of the image CanvasComposition.Resize(surface, bitmapSize); surfaceSize = bitmapSize; } // Draw the image to the surface using (var session = CanvasComposition.CreateDrawingSession(surface)) { session.Clear(Windows.UI.Color.FromArgb(0, 0, 0, 0)); session.DrawImage(canvasBitmap, new Rect(0, 0, surfaceSize.Width, surfaceSize.Height), new Rect(0, 0, bitmapSize.Width, bitmapSize.Height)); } } } }
public async Task Draw(CompositionGraphicsDevice device, Object drawingLock, CompositionDrawingSurface surface, Size size) { var canvasDevice = CanvasComposition.GetCanvasDevice(device); CanvasBitmap canvasBitmap; if (_softwareBitmap != null) { canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, _softwareBitmap); } else if (_file != null) { SoftwareBitmap softwareBitmap = await LoadFromFile(_file); canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(canvasDevice, softwareBitmap); } else { canvasBitmap = await CanvasBitmap.LoadAsync(canvasDevice, _uri); } var bitmapSize = canvasBitmap.Size; // // Because the drawing is done asynchronously and multiple threads could // be trying to get access to the device/surface at the same time, we need // to do any device/surface work under a lock. // lock (drawingLock) { Size surfaceSize = size; if (surface.Size != size || surface.Size == new Size(0, 0)) { // Resize the surface to the size of the image CanvasComposition.Resize(surface, bitmapSize); surfaceSize = bitmapSize; } // Allow the app to process the bitmap if requested if (_handler != null) { _handler(surface, canvasBitmap, device); } else { // Draw the image to the surface using (var session = CanvasComposition.CreateDrawingSession(surface)) { session.Clear(Windows.UI.Color.FromArgb(0, 0, 0, 0)); session.DrawImage(canvasBitmap, new Rect(0, 0, surfaceSize.Width, surfaceSize.Height), new Rect(0, 0, bitmapSize.Width, bitmapSize.Height)); } } } }
private void InitWin2D(Vector2 canvasSize) { var dpi = GraphicsInformation.Dpi; var surfaceFactory = SurfaceFactory.GetSharedSurfaceFactoryForCompositor(_compositor); _device = CanvasComposition.GetCanvasDevice(surfaceFactory.GraphicsDevice); _canvas = new Canvas(_device, canvasSize); _canvas.SizeChanged += OnCanvasSizeChanged; _currentColor = Colors.Black; SwitchToPencilTool(); _canvasPresenter = new CanvasPresenter(_canvas, _device); var surface = _canvasPresenter.GetSurface(_compositor); var brush = _compositor.CreateSurfaceBrush(surface); brush.BitmapInterpolationMode = CompositionBitmapInterpolationMode.NearestNeighbor; _visual.Brush = brush; _visual.Size = _canvas.Size; _canvasPresenter.Start(); }
/// <summary> /// Constructor /// </summary> /// <param name="graphicsDevice">Composition Graphics Device</param> public CompositionGenerator(CompositionGraphicsDevice graphicsDevice) { if (graphicsDevice == null) { throw new ArgumentNullException(nameof(graphicsDevice), "GraphicsDevice cannot be null!"); } // Composition Graphics Device _graphicsDevice = graphicsDevice; _isGraphicsDeviceCreator = false; _graphicsDevice.RenderingDeviceReplaced += RenderingDeviceReplaced; if (!DesignMode.DesignModeEnabled) { DisplayInformation.DisplayContentsInvalidated += OnDisplayContentsInvalidated; } // Compositor _compositor = _graphicsDevice.Compositor; // Canvas Device _canvasDevice = CanvasComposition.GetCanvasDevice(_graphicsDevice); _isCanvasDeviceCreator = false; _canvasDevice.DeviceLost += DeviceLost; }