Esempio n. 1
0
        public async Task ReadFromAsync(IRandomAccessStream imageStream)
        {
            if ((_canvas == null) || IsDisposed)
            {
                return;
            }
            CanvasControl canvas = _canvas;

            if (_cBitmap != null)
            {
                _cBitmap.Dispose();
                _cBitmap = null;
            }
            // TODO: Remove this catch after the Fix in Win2d
            try
            {
                _cBitmap = await CanvasBitmap.LoadAsync(canvas, imageStream);
            }
            catch (InvalidCastException)
            {
            }
            if (!IsDisposed)
            {
                await canvas.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => canvas.Invalidate()).AsTask().ConfigureAwait(false);
            }
        }
        /// <summary>
        /// Resizes the IImageSurface with the given size and redraws the IImageSurface by loading
        /// image from the new Uri.
        /// </summary>
        /// <param name="uri">Uri of the image to be loaded onto the IImageSurface.</param>
        /// <param name="size">New size of the IImageSurface</param>
        /// <param name="options">The image's resize and alignment options in the allocated space.</param>
        /// <returns>Task</returns>
        public Task RedrawAsync(Uri uri, Size size, ImageSurfaceOptions options)
        {
            // If the given Uri differs from the previously stored Uri or if the ImageSurface was
            // directly created from a CanvasBitmap, dispose the existing canvasBitmap
            if ((_uri != null && !_uri.IsEqualTo(uri)) ||
                (_uri == null && _canvasBitmap != null))
            {
                _canvasBitmap?.Dispose();
                _canvasBitmap            = null;
                _raiseLoadCompletedEvent = uri != null;
            }

            // Set the image options
            Options = options;
            // Resize the surface only if AutoResize option is disabled
            if (!Options.AutoResize)
            {
                // resize the IImageSurface
                _generator.ResizeDrawingSurface(_surfaceLock, _surface, size);
                // Set the size
                Size = _surface?.Size ?? new Size(0, 0);
            }
            // Set the new Uri of the image to be loaded
            _uri = uri;
            // Reload the IImageSurface
            return(RedrawSurfaceAsync());
        }
Esempio n. 3
0
        public void Dispose()
        {
            RenderTarget?.Dispose();
            RenderTarget = null;

            RenderTargetSurface?.Dispose();
            RenderTargetSurface = null;
        }
        /// <summary>
        /// Redraws the SurfaceImage by loading image from the new Uri
        /// </summary>
        /// <param name="uri">Uri of the image to be loaded on to the image surface.</param>
        /// <returns>Task</returns>
        public Task RedrawAsync(Uri uri)
        {
            // If the given Uri differs from the previously stored Uri
            // dispose the existing canvasBitmap
            if (!_uri.IsEqualTo(uri))
            {
                _canvasBitmap?.Dispose();
                _canvasBitmap = null;
            }

            // Set the new Uri of the image to be loaded
            _uri = uri;
            // Reload the SurfaceImage
            return(RedrawSurfaceImageInternalAsync());
        }
Esempio n. 5
0
 private void ThisUnloaded(object sender, RoutedEventArgs e)
 {
     DisplayInformation.DisplayContentsInvalidated -= DisplayInformation_DisplayContentsInvalidated;
     _canvas?.RemoveFromVisualTree();
     _canvas = null;
     // dispose buffers
     _buffer.Dispose();
     _tmpBuffer.Dispose();
     _background.Dispose();
     foreach (var buffer in _undoBuffer)
     {
         buffer.Value.Dispose();
     }
     foreach (var buffer in _redoBuffer)
     {
         buffer.Value.Dispose();
     }
     foreach (var layer in _layers)
     {
         layer.Image.Dispose();
     }
     _undoBuffer.Clear();
     _redoBuffer.Clear();
     _layers.Clear();
     _buffer     = null;
     _tmpBuffer  = null;
     _background = null;
 }
Esempio n. 6
0
        private async Task AddThumbnail_FreeResource(StackPanel stackPanel, CanvasBitmap canvasBitmap, string effectName)
        {
            InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();

            if (canvasBitmap != null)
            {
                await canvasBitmap.SaveAsync(randomAccessStream, CanvasBitmapFileFormat.Png);

                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(randomAccessStream);

                Image image = new Image();
                image.Width  = simplePhotoEditor.thumbnailSize;
                image.Height = simplePhotoEditor.thumbnailSize;
                image.Margin = new Thickness(10, 5, 10, 5);
                image.Source = bitmapImage;

                TextBlock textBlock = new TextBlock();
                textBlock.Text          = effectName;
                textBlock.Margin        = new Thickness(1, 1, 1, 10);
                textBlock.TextAlignment = TextAlignment.Center;


                StackPanel stackPanelBlock = new StackPanel();
                stackPanelBlock.Orientation = Orientation.Vertical;
                stackPanelBlock.Children.Add(image);
                stackPanelBlock.Children.Add(textBlock);
                stackPanelBlock.Tapped += Image_Tapped;

                stackPanel.Children.Add(stackPanelBlock);

                canvasBitmap.Dispose();
                canvasBitmap = null;
            }
        }
Esempio n. 7
0
        public IImage Downsize(float maxWidthOrHeight, bool disposeOriginal = false)
        {
            if (Width > maxWidthOrHeight || Height > maxWidthOrHeight)
            {
                using (var memoryStream = new InMemoryRandomAccessStream())
                {
                    Save(memoryStream.AsStreamForWrite());
                    memoryStream.Seek(0);

                    // ReSharper disable once AccessToDisposedClosure
                    var newBitmap = AsyncPump.Run(async() => await CanvasBitmap.LoadAsync(_creator, memoryStream, 96));
                    using (var memoryStream2 = new InMemoryRandomAccessStream())
                    {
                        // ReSharper disable once AccessToDisposedClosure
                        AsyncPump.Run(async() => await newBitmap.SaveAsync(memoryStream2, CanvasBitmapFileFormat.Png));

                        memoryStream2.Seek(0);
                        var newImage = W2DGraphicsService.Instance.LoadImageFromStream(memoryStream2.AsStreamForRead());
                        if (disposeOriginal)
                        {
                            _bitmap.Dispose();
                        }

                        return(newImage);
                    }
                }
            }

            return(this);
        }
Esempio n. 8
0
        /// <summary>
        /// 设置图片
        /// </summary>
        /// <param name="file"></param>
        public async Task SetImage(StorageFile file)
        {
            File = file;
            CanvasBitmap?.Dispose();
            CanvasBitmap = await CanvasBitmap.LoadAsync(CanvasDevice, await file.OpenAsync(FileAccessMode.Read));

            Upload = true;
        }
Esempio n. 9
0
        /// <summary>
        /// 设置图片
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public async Task SetImage(IRandomAccessStream stream)
        {
            CanvasBitmap?.Dispose();
            CanvasBitmap = await CanvasBitmap.LoadAsync(CanvasDevice, stream);
            await WaterBerbouPelJicayweeno("");

            Upload = true;
        }
Esempio n. 10
0
 public void Dispose()
 {
     if (tempSurface != null)
     {
         bound = default(Rect);
         tempSurface.Dispose();
         blur.Dispose();
         canDraw = false;
     }
 }
Esempio n. 11
0
 private void SvgAnimPlayer_Unloaded(object sender, RoutedEventArgs e)
 {
     lock (_lockobj)
     {
         _win2DSvg?.Dispose();
         _win2DSvg = null;
         _handBitmap?.Dispose();
         _handBitmap = null;
     };
 }
Esempio n. 12
0
        private void Eyedropper_Unloaded(object sender, RoutedEventArgs e)
        {
            UnhookEvents();
            if (_popup != null)
            {
                _popup.IsOpen = false;
            }

            _appScreenshot?.Dispose();
            _appScreenshot = null;
        }
Esempio n. 13
0
        public void Dispose()
        {
            if (surfaceLoaded && bitmap != null)
            {
                bitmap.Dispose();

                surfaceLoaded = false;
            }
            freeParticles.Clear();
            ActiveParticles.Clear();
        }
        public void ResetBitmapResources()
        {
            if (canvasBitmap != null)
            {
                canvasBitmap.Dispose();
                canvasBitmap = null;
            }

            if (canvasRenderTarget != null)
            {
                canvasRenderTarget.Dispose();
                canvasRenderTarget = null;
            }

            if (canvasRenderTargetPreview != null)
            {
                canvasRenderTargetPreview.Dispose();
                canvasRenderTargetPreview = null;
            }
            
        }
Esempio n. 15
0
 public void ConvertEmptyASCII(int width, int height)
 {
     CanvasVisibility = Visibility.Collapsed;
     ASCIIText        = GetEmptyText(width, height);
     TextWidth        = width;
     TextHeight       = height;
     if (canvasBitmap != null)
     {
         canvasBitmap.Dispose();
         canvasBitmap = null;
     }
 }
Esempio n. 16
0
        /// <summary>
        /// 设置图片
        /// </summary>
        /// <param name="file"></param>
        public async Task SetImage(StorageFile file)
        {
            File = file;
            CanvasBitmap?.Dispose();
            ImageFile = GetFileImage(file);
            if (ImageFile != ImageEnum.Gif)
            {
                CanvasBitmap = await CanvasBitmap.LoadAsync(CanvasDevice, await file.OpenAsync(FileAccessMode.Read));
            }

            Upload = true;
        }
Esempio n. 17
0
 private void DisposeLiveviewImageBitmap()
 {
     rwLock.EnterWriteLock();
     try
     {
         LiveviewImageBitmap?.Dispose();
         LiveviewImageBitmap = null;
     }
     finally
     {
         rwLock.ExitWriteLock();
     }
 }
        private void Eyedropper_Unloaded(object sender, RoutedEventArgs e)
        {
            UnhookEvents();
            if (_popup != null)
            {
                _popup.IsOpen = false;
            }

            _appScreenshot?.Dispose();
            _appScreenshot = null;

            Window.Current.CoreWindow.PointerCursor = DefaultCursor;
        }
Esempio n. 19
0
        //Dispose variables and clear memory
        void DisposeVariables()
        {
            try
            {
                Debug.WriteLine("Setting the last background task run date.");
                vApplicationSettings["BgStatusLastRunDate"] = DateTimeNow.ToString(vCultureInfoEng);

                Debug.WriteLine("Disposing variables and clearing memory.");
                if (Win2DCanvasDevice != null)
                {
                    Win2DCanvasDevice.Dispose();
                }
                if (Win2DCanvasRenderTarget != null)
                {
                    Win2DCanvasRenderTarget.Dispose();
                }
                if (Win2DCanvasBitmap != null)
                {
                    Win2DCanvasBitmap.Dispose();
                }
                if (Win2DCanvasImageBrush != null)
                {
                    Win2DCanvasImageBrush.Dispose();
                }
                if (Win2DCanvasTextFormatTitle != null)
                {
                    Win2DCanvasTextFormatTitle.Dispose();
                }
                if (Win2DCanvasTextFormatBody != null)
                {
                    Win2DCanvasTextFormatBody.Dispose();
                }
                if (Win2DCanvasTextFormatSub != null)
                {
                    Win2DCanvasTextFormatSub.Dispose();
                }
                if (Win2DCanvasTextFormatTextLeft != null)
                {
                    Win2DCanvasTextFormatTextLeft.Dispose();
                }
                if (Win2DCanvasTextFormatTextRight != null)
                {
                    Win2DCanvasTextFormatTextRight.Dispose();
                }
                if (Win2DCanvasTextFormatTextCenter != null)
                {
                    Win2DCanvasTextFormatTextCenter.Dispose();
                }
            }
            catch { }
        }
Esempio n. 20
0
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            _radialConfiguration.IsMenuSuppressed = false;

            _radialController.RotationChanged      -= RadialController_RotationChanged;
            _radialController.ScreenContactStarted -= RadialController_ScreenContactStarted;
            _radialController.ScreenContactEnded   -= RadialController_ScreenContactEnded;
            _radialController.ButtonPressed        -= RadialController_ButtonPressed;
            _radialController.ButtonReleased       -= RadialController_ButtonReleased;

            _effect.Dispose();
            _canvasBitmap.Dispose();
            _canvasRenderTarget.Dispose();
        }
        /// <summary>
        /// Resizes and redraws the IImageMaskSurface using the given CanvasBitmap's alpha values with the given padding
        /// using the given options.
        /// </summary>
        /// <param name="surfaceBitmap">Image whose alpha values are to be used to create the mask.</param>
        /// <param name="size">New size of the IImageMaskSurface.</param>
        /// <param name="padding">The padding between the IImageMaskSurface outer bounds and the bounds of the area where
        /// the mask, created from the loaded image's alpha values, should be rendered.</param>
        /// <param name="options">The image's resize, alignment and blur radius options in the allocated space.</param>
        public void Redraw(CanvasBitmap surfaceBitmap, Size size, Thickness padding, ImageSurfaceOptions options)
        {
            if (_canvasBitmap != surfaceBitmap)
            {
                // Dispose the previous canvas bitmap resource (if any)
                if (_canvasBitmap != null)
                {
                    _canvasBitmap.Dispose();
                    _canvasBitmap = null;
                }

                if (surfaceBitmap != null)
                {
                    // No need to copy again if _canvasBitmap and surfaceBitmap are same
                    if (_canvasBitmap != surfaceBitmap)
                    {
                        // Copy the surface bitmap onto _canvasBitmap
                        _canvasBitmap = CanvasBitmap.CreateFromBytes(_generator.Device,
                                                                     surfaceBitmap.GetPixelBytes(),
                                                                     (int)surfaceBitmap.Bounds.Width,
                                                                     (int)surfaceBitmap.Bounds.Height,
                                                                     surfaceBitmap.Format);
                    }
                }
                else
                {
                    _canvasBitmap = null;
                }
            }

            _uri = null;
            _raiseLoadCompletedEvent = false;

            // Set the options
            Options = options;
            // Resize if required
            if (Size != size)
            {
                // resize the IImageMaskSurface
                _generator.ResizeDrawingSurface(_surfaceLock, _surface, size);
                // Set the size
                Size = _surface?.Size ?? new Size(0, 0);
            }

            // Set the mask padding
            MaskPadding = padding;
            // Redraw the IImageMaskSurface
            RedrawSurface();
        }
Esempio n. 22
0
        private static async Task CropImageWithShapeAsync(WriteableBitmap writeableBitmap, IRandomAccessStream stream, Rect croppedRect, BitmapFileFormat bitmapFileFormat, CropShape cropShape)
        {
            var device       = CanvasDevice.GetSharedDevice();
            var clipGeometry = CreateClipGeometry(device, cropShape, new Size(croppedRect.Width, croppedRect.Height));

            if (clipGeometry == null)
            {
                return;
            }

            CanvasBitmap sourceBitmap = null;

            using (var randomAccessStream = new InMemoryRandomAccessStream())
            {
                await CropImageAsync(writeableBitmap, randomAccessStream, croppedRect, bitmapFileFormat);

                sourceBitmap = await CanvasBitmap.LoadAsync(device, randomAccessStream);
            }

            using (var offScreen = new CanvasRenderTarget(device, (float)croppedRect.Width, (float)croppedRect.Height, 96f))
            {
                using (var drawingSession = offScreen.CreateDrawingSession())
                    using (var markCommandList = new CanvasCommandList(device))
                    {
                        using (var markDrawingSession = markCommandList.CreateDrawingSession())
                        {
                            markDrawingSession.FillGeometry(clipGeometry, Colors.Black);
                        }

                        var alphaMaskEffect = new AlphaMaskEffect
                        {
                            Source    = sourceBitmap,
                            AlphaMask = markCommandList
                        };
                        drawingSession.DrawImage(alphaMaskEffect);
                        alphaMaskEffect.Dispose();
                    }

                clipGeometry.Dispose();
                sourceBitmap.Dispose();
                var pixelBytes    = offScreen.GetPixelBytes();
                var bitmapEncoder = await BitmapEncoder.CreateAsync(GetEncoderId(bitmapFileFormat), stream);

                bitmapEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, offScreen.SizeInPixels.Width, offScreen.SizeInPixels.Height, 96.0, 96.0, pixelBytes);
                await bitmapEncoder.FlushAsync();
            }
        }
Esempio n. 23
0
        private void OnUnloaded(object sender, RoutedEventArgs e)
        {
            Subscribe(false);

            _canvas.CreateResources -= OnCreateResources;
            _canvas.Draw            -= OnDraw;
            _canvas.Unloaded        -= OnUnloaded;
            _canvas.RemoveFromVisualTree();
            _canvas = null;

            Dispose();

            //_animation?.Dispose();
            _animation = null;
            _bitmap?.Dispose();
            _bitmap = null;
        }
        /// <summary>
        /// Resizes and redraws the IImageSurface using the given CanvasBitmap using the given options.
        /// </summary>
        /// <param name="surfaceBitmap">Image whose alpha values are to be used to create the mask.</param>
        /// <param name="size">New size of the IImageSurface.</param>
        /// <param name="options">The image's resize, alignment options in the allocated space.</param>
        public void Redraw(CanvasBitmap surfaceBitmap, Size size, ImageSurfaceOptions options)
        {
            if (_canvasBitmap != surfaceBitmap)
            {
                // Dispose the previous canvas bitmap resource (if any)
                if (_canvasBitmap != null)
                {
                    _canvasBitmap.Dispose();
                    _canvasBitmap = null;
                }

                if (surfaceBitmap != null)
                {
                    if (_canvasBitmap != surfaceBitmap)
                    {
                        // Copy the surface bitmap onto _canvasBitmap
                        _canvasBitmap = CanvasBitmap.CreateFromBytes(_generator.Device,
                                                                     surfaceBitmap.GetPixelBytes(),
                                                                     (int)surfaceBitmap.Bounds.Width,
                                                                     (int)surfaceBitmap.Bounds.Height,
                                                                     surfaceBitmap.Format);
                    }
                }
                else
                {
                    _canvasBitmap = null;
                }
            }

            _uri = null;
            _raiseLoadCompletedEvent = false;

            // Set the options
            Options = options;
            // Resize the surface only if AutoResize option is disabled
            if (!Options.AutoResize && Size != size)
            {
                // resize the IImageMaskSurface
                _generator.ResizeDrawingSurface(_surfaceLock, _surface, size);
                // Set the size
                Size = _surface?.Size ?? new Size(0, 0);
            }
            // Redraw the IImageMaskSurface
            RedrawSurface();
        }
Esempio n. 25
0
        private void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            if (_bitmap != null)
            {
                _bitmap.Dispose();
            }

            var buffer = ArrayPool <byte> .Shared.Rent(256 * 256 * 4);

            _bitmap = CanvasBitmap.CreateFromBytes(sender, buffer, _frameSize.Width, _frameSize.Height, DirectXPixelFormat.B8G8R8A8UIntNormalized);
            ArrayPool <byte> .Shared.Return(buffer);

            if (args.Reason == CanvasCreateResourcesReason.FirstTime)
            {
                OnSourceChanged(UriToPath(Source), _source);
                Invalidate();
            }
        }
Esempio n. 26
0
 public void Dispose()
 {
     RenderTarget?.Dispose();
     RenderTarget = null;
 }