コード例 #1
0
        /// <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());
        }
コード例 #2
0
 /// <summary>
 /// 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="options">The image's resize, alignment options in the allocated space.</param>
 public void Redraw(CanvasBitmap surfaceBitmap, ImageSurfaceOptions options)
 {
     // Set the image options
     Options = options;
     // Redraw the IImageSurface
     Redraw(surfaceBitmap, Size, Options);
 }
コード例 #3
0
 /// <summary>
 /// Redraws the SurfaceImage with the given image options
 /// </summary>
 /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
 /// <returns>Task</returns>
 public void Redraw(ImageSurfaceOptions options)
 {
     // Set the image options
     Options = options;
     // Redraw the SurfaceImage
     RedrawSurfaceImageInternal();
 }
コード例 #4
0
        /// <summary>
        /// Resizes the SurfaceImage with the given size and redraws the SurfaceImage by loading
        /// image from the new Uri.
        /// </summary>
        /// <param name="uri">Uri of the image to be loaded onto the SurfaceImage.</param>
        /// <param name="size">New size of the SurfaceImage</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        /// <returns>Task</returns>
        public async Task RedrawAsync(Uri uri, Size size, ImageSurfaceOptions options)
        {
            // If the given Uri differs from the previously stored Uri
            // dispose the existing canvasBitmap
            if (!_uri.IsEqualTo(uri))
            {
                _canvasBitmap?.Dispose();
                _canvasBitmap = null;
            }

            // Set the image options
            Options = options;
            // Resize the surface only if AutoResize option is disabled
            if (!Options.AutoResize)
            {
                // resize the SurfaceImage
                _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 SurfaceImage
            await RedrawSurfaceImageInternalAsync();
        }
コード例 #5
0
 /// <summary>
 /// Redraws the IImageSurface with the given image options
 /// </summary>
 /// <param name="options">The image's resize and alignment options in the allocated space.</param>
 public void Redraw(ImageSurfaceOptions options)
 {
     // Set the image options
     Options = options;
     // Redraw the IImageSurface
     RedrawSurface();
 }
コード例 #6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="generator">ICompositionMaskGeneratorInternal object</param>
 /// <param name="surfaceBitmap">The CanvasBitmap whose alpha values will be used to create the Mask.</param>
 /// <param name="size">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 options and blur radius in the allocated space.</param>
 public ImageMaskSurface(ICompositionGeneratorInternal generator, CanvasBitmap surfaceBitmap, Size size,
                         Thickness padding, ImageSurfaceOptions options)
 {
     _generator = generator ??
                  throw new ArgumentNullException(nameof(generator), "CompositionGenerator cannot be null!");
     _surfaceLock = new object();
     // Create the Surface of the IImageMaskSurface
     _surface = _generator.CreateDrawingSurface(_surfaceLock, size);
     Size     = _surface?.Size ?? new Size(0, 0);
     _uri     = null;
     _raiseLoadCompletedEvent = false;
     if (surfaceBitmap != null)
     {
         _canvasBitmap = CanvasBitmap.CreateFromBytes(_generator.Device,
                                                      surfaceBitmap.GetPixelBytes(),
                                                      (int)surfaceBitmap.Bounds.Width,
                                                      (int)surfaceBitmap.Bounds.Height,
                                                      surfaceBitmap.Format);
     }
     // Set the mask padding
     MaskPadding = padding;
     // Set the image options
     Options = options;
     // Subscribe to DeviceReplaced event
     _generator.DeviceReplaced += OnDeviceReplaced;
 }
コード例 #7
0
 /// <summary>
 /// Redraws the IImageSurface (using the image in the given imageSurface) or the IImageMaskSurface
 /// (using the alpha values of image in the given imageSurface).
 /// </summary>
 /// <param name="imageSurface">IImageSurface whose image is to be loaded on the surface.</param>
 /// <param name="options">Describes the image's resize, alignment options in the allocated space.</param>
 public void Redraw(IImageSurface imageSurface, ImageSurfaceOptions options)
 {
     if (imageSurface != null)
     {
         Redraw(imageSurface.SurfaceBitmap, imageSurface.Size, options);
     }
     else
     {
         // Draw an empty surface
         Redraw(surfaceBitmap: null, options);
     }
 }
コード例 #8
0
 /// <summary>
 /// Redraws the IImageMaskSurface using the alpha values of the image in the given IImageMaskSurface using the given padding
 /// and options.
 /// </summary>
 /// <param name="imageMaskSurface">IImageMaskSurface whose image's alpha values are to be used to create the mask.</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">Describes the image's resize, alignment and blur radius options in the allocated space.</param>
 public void Redraw(IImageMaskSurface imageMaskSurface, Thickness padding, ImageSurfaceOptions options)
 {
     if (imageMaskSurface != null)
     {
         Redraw(imageMaskSurface.SurfaceBitmap, imageMaskSurface.Size, padding, options);
     }
     else
     {
         // Draw an empty surface
         Redraw(surfaceBitmap: null, Size, padding, options);
     }
 }
コード例 #9
0
 /// <summary>
 /// Disposes the resources used by the SurfaceImage
 /// </summary>
 public void Dispose()
 {
     _surface?.Dispose();
     if (_generator != null)
     {
         _generator.DeviceReplaced -= OnDeviceReplaced;
     }
     _canvasBitmap?.Dispose();
     _canvasBitmap = null;
     _surface      = null;
     _generator    = null;
     _uri          = null;
     Options       = null;
 }
コード例 #10
0
        /// <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();
        }
コード例 #11
0
 /// <summary>
 /// Resizes the SurfaceImage to the new size.
 /// </summary>
 /// <param name="size">New size of the SurfaceImage</param>
 /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
 public void Resize(Size size, ImageSurfaceOptions options)
 {
     // Set the image options
     Options = options;
     // Resize the surface only if AutoResize option is disabled
     if (!Options.AutoResize)
     {
         // resize the SurfaceImage
         _generator.ResizeDrawingSurface(_surfaceLock, _surface, size);
         // Set the size
         Size = _surface?.Size ?? new Size(0, 0);
     }
     // resize the SurfaceImage
     RedrawSurfaceImageInternal();
 }
コード例 #12
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="generator">ICompositionMaskGeneratorInternal object</param>
 /// <param name="uri">Uri of the image to be loaded onto the IImageSurface.</param>
 /// <param name="size">Size of the IImageSurface</param>
 /// <param name="options">The image's resize and alignment options in the allocated space.</param>
 public ImageSurface(ICompositionGeneratorInternal generator, Uri uri, Size size, ImageSurfaceOptions options)
 {
     _generator   = generator ?? throw new ArgumentNullException(nameof(generator), "CompositionGenerator cannot be null!");
     _surfaceLock = new object();
     // Create the Surface of the IImageSurface
     _surface = _generator.CreateDrawingSurface(_surfaceLock, size);
     Size     = _surface?.Size ?? new Size(0, 0);
     _uri     = uri;
     _raiseLoadCompletedEvent = _uri != null;
     _canvasBitmap            = null;
     // Set the image options
     Options = options;
     // Subscribe to DeviceReplaced event
     _generator.DeviceReplaced += OnDeviceReplaced;
     Status = ImageSurfaceLoadStatus.None;
 }
コード例 #13
0
        /// <summary>
        /// Redraws the SurfaceImage by loading image from the new Uri and image options
        /// </summary>
        /// <param name="uri">Uri of the image to be loaded on to the image surface.</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        /// <returns>Task</returns>
        public Task RedrawAsync(Uri uri, ImageSurfaceOptions options)
        {
            // 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;
            // Set the image options
            Options = options;
            // Reload the SurfaceImage
            return(RedrawSurfaceImageInternalAsync());
        }
コード例 #14
0
 /// <summary>
 /// Resizes the IImageMaskSurface to the new size and redraws it with the given padding using the given options.
 /// </summary>
 /// <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 Resize(Size size, Thickness padding, ImageSurfaceOptions options)
 {
     // Set the mask padding
     MaskPadding = padding;
     // Set the image options
     Options = options;
     // Resize the surface only if AutoResize option is disabled
     if (Size != size)
     {
         // resize the IImageMaskSurface
         _generator.ResizeDrawingSurface(_surfaceLock, _surface, size);
         // Set the size
         Size = _surface?.Size ?? new Size(0, 0);
     }
     // Redraw the IImageMaskSurface
     RedrawSurface();
 }
コード例 #15
0
        /// <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();
        }
コード例 #16
0
        /// <summary>
        /// Resizes the ImageSurface with the given size and redraws the ImageSurface by loading
        /// image from the new Uri.
        /// </summary>
        /// <param name="surfaceLock">The object to lock to prevent multiple threads
        /// from accessing the surface at the same time.</param>
        /// <param name="surface">CompositionDrawingSurface</param>
        /// <param name="uri">Uri of the image to be loaded onto the SurfaceImage.</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        /// <param name="canvasBitmap">The CanvasBitmap on which the image is loaded.</param>
        /// <returns>CanvasBitmap</returns>
        public async Task <CanvasBitmap> RedrawImageSurfaceAsync(object surfaceLock, CompositionDrawingSurface surface,
                                                                 Uri uri, ImageSurfaceOptions options, CanvasBitmap canvasBitmap)
        {
            if ((canvasBitmap == null) && (uri != null))
            {
                try
                {
                    canvasBitmap = await CanvasBitmap.LoadAsync(_canvasDevice, uri);
                }
                catch (IOException)
                {
                    // Do nothing here as RenderBitmap method will fill the surface
                    // with options.SurfaceBackgroundColor as the image failed to load
                    // from Uri
                }
            }

            // Render the image to the surface
            RenderBitmap(surfaceLock, surface, canvasBitmap, options);

            return(canvasBitmap);
        }
コード例 #17
0
 /// <summary>
 /// Resizes the ImageSurface to the given size and redraws the ImageSurface
 /// by rendering the canvasBitmap onto the surface.
 /// </summary>
 /// <param name="surfaceLock">The object to lock to prevent multiple threads
 /// from accessing the surface at the same time.</param>
 /// <param name="surface">CompositionDrawingSurface</param>
 /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
 /// <param name="canvasBitmap">The CanvasBitmap on which the image is loaded.</param>
 public void RedrawImageSurface(object surfaceLock, CompositionDrawingSurface surface,
                                ImageSurfaceOptions options, CanvasBitmap canvasBitmap)
 {
     // Render the image to the surface
     RenderBitmap(surfaceLock, surface, canvasBitmap, options);
 }
コード例 #18
0
 /// <summary>
 /// 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="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 options and blur radius in the allocated space.</param>
 public void Redraw(CanvasBitmap surfaceBitmap, Thickness padding, ImageSurfaceOptions options)
 {
     Redraw(surfaceBitmap, Size, padding, options);
 }
コード例 #19
0
 /// <summary>
 /// Resizes the IImageMaskSurface to the new size using the given options.
 /// </summary>
 /// <param name="size">New size of the IImageMaskSurface</param>
 /// <param name="options">The image's resize, alignment and blur radius options in the allocated space.</param>
 public void Resize(Size size, ImageSurfaceOptions options)
 {
     Resize(size, MaskPadding, options);
 }
コード例 #20
0
        /// <summary>
        /// Creates a ImageSurface having the given size onto which an image (based on the Uri
        /// and the options) is loaded.
        /// </summary>
        /// <param name="uri">Uri of the image to be loaded onto the SurfaceImage.</param>
        /// <param name="size">New size of the SurfaceImage</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        /// <returns>ICompositionSurfaceImage</returns>
        public async Task <IImageSurface> CreateImageSurfaceAsync(Uri uri, Size size, ImageSurfaceOptions options)
        {
            // Initialize the SurfaceImage
            var surfaceImage = new ImageSurface(this, uri, size, options);

            // Render the image onto the surface
            await surfaceImage.RedrawAsync();

            return((IImageSurface)surfaceImage);
        }
コード例 #21
0
 /// <summary>
 /// Resizes and redraws the IImageSurface (using the image in the given imageSurface) or the IImageMaskSurface
 /// (using the alpha values of image in the given imageSurface).
 /// </summary>
 /// <param name="imageSurface">IImageSurface whose image is to be loaded on the surface.</param>
 /// <param name="size">New size of the IImageMaskSurface.</param>
 /// <param name="options">Describes the image's resize, alignment options in the allocated space.</param>
 public void Redraw(IImageSurface imageSurface, Size size, ImageSurfaceOptions options)
 {
     Redraw(imageSurface?.SurfaceBitmap, size, options);
 }
コード例 #22
0
 /// <summary>
 /// Resizes the IImageMaskSurface with the given size and redraws the IImageMaskSurface by loading
 /// image from the new Uri.
 /// </summary>
 /// <param name="uri">Uri of the image to be loaded onto the IImageMaskSurface.</param>
 /// <param name="size">New size of the IImageMaskSurface</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)
 {
     return(RedrawAsync(uri, size, MaskPadding, options));
 }
コード例 #23
0
        /// <summary>
        /// Renders the CanvasBitmap on the CompositionDrawingSurface based on the given options.
        /// </summary>
        /// <param name="surfaceLock">The object to lock to prevent multiple threads
        /// from accessing the surface at the same time.</param>
        /// <param name="surface">CompositionDrawingSurface on which the CanvasBitmap has to be rendered.</param>
        /// <param name="canvasBitmap">CanvasBitmap created by loading the image from the Uri</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        private static void RenderBitmap(object surfaceLock, CompositionDrawingSurface surface, CanvasBitmap canvasBitmap,
            ImageSurfaceOptions options)
        {
            var surfaceSize = surface.Size;

            // If the canvasBitmap is null, then just fill the surface with the SurfaceBackgroundColor
            if (canvasBitmap == null)
            {
                // No need to render if the width and/or height of the surface is zero
                if (surfaceSize.IsEmpty || surfaceSize.Width.IsZero() || surfaceSize.Height.IsZero())
                    return;

                //
                // Since 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 (surfaceLock)
                {
                    using (var session = CanvasComposition.CreateDrawingSession(surface))
                    {
                        // Clear the surface with the SurfaceBackgroundColor
                        session.Clear(options.SurfaceBackgroundColor);
                    }

                    // No need to proceed further
                    return;
                }
            }

            //
            // Since 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 (surfaceLock)
            {
                // Is AutoResize Enabled?
                if (options.AutoResize)
                {
                    // If AutoResize is allowed and the canvasBitmap size and surface size are 
                    // not matching then resize the surface to match the canvasBitmap size.
                    //
                    // NOTE: HorizontalAlignment, Vertical Alignment and Stretch will be
                    // handled by the CompositionSurfaceBrush created using this surface.
                    //
                    if (canvasBitmap.Size != surfaceSize)
                    {
                        // Resize the surface
                        CanvasComposition.Resize(surface, canvasBitmap.Size);
                        surfaceSize = canvasBitmap.Size;
                    }

                    // No need to render if the width and/or height of the surface is zero
                    if (surfaceSize.IsEmpty || surface.Size.Width.IsZero() || surface.Size.Height.IsZero())
                        return;

                    // Draw the image to the surface
                    using (var session = CanvasComposition.CreateDrawingSession(surface))
                    {
                        // Render the image
                        session.DrawImage(canvasBitmap,                                         // CanvasBitmap
                            new Rect(0, 0, surfaceSize.Width, surfaceSize.Height),              // Target Rectangle
                            new Rect(0, 0, canvasBitmap.Size.Width, canvasBitmap.Size.Height),  // Source Rectangle
                            options.Opacity,                                                    // Opacity
                            options.Interpolation);                                             // Interpolation
                    }
                }
                else
                {
                    // No need to render if the width and/or height of the surface is zero
                    if (surfaceSize.IsEmpty || surface.Size.Width.IsZero() || surface.Size.Height.IsZero())
                        return;

                    var bitmapSize = canvasBitmap.Size;
                    var sourceWidth = bitmapSize.Width;
                    var sourceHeight = bitmapSize.Height;
                    var ratio = sourceWidth / sourceHeight;
                    var targetWidth = 0d;
                    var targetHeight = 0d;
                    var left = 0d;
                    var top = 0d;

                    // Stretch Mode
                    switch (options.Stretch)
                    {
                        case Stretch.None:
                            targetWidth = sourceWidth;
                            targetHeight = sourceHeight;
                            break;
                        case Stretch.Fill:
                            targetWidth = surfaceSize.Width;
                            targetHeight = surfaceSize.Height;
                            break;
                        case Stretch.Uniform:
                            // If width is greater than height
                            if (ratio > 1.0)
                            {
                                targetHeight = Math.Min(surfaceSize.Width / ratio, surfaceSize.Height);
                                targetWidth = targetHeight * ratio;
                            }
                            else
                            {
                                targetWidth = Math.Min(surfaceSize.Height * ratio, surfaceSize.Width);
                                targetHeight = targetWidth / ratio;
                            }
                            break;
                        case Stretch.UniformToFill:
                            // If width is greater than height
                            if (ratio > 1.0)
                            {
                                targetHeight = Math.Max(surfaceSize.Width / ratio, surfaceSize.Height);
                                targetWidth = targetHeight * ratio;
                            }
                            else
                            {
                                targetWidth = Math.Max(surfaceSize.Height * ratio, surfaceSize.Width);
                                targetHeight = targetWidth / ratio;
                            }
                            break;
                    }

                    // Horizontal Alignment
                    switch (options.HorizontalAlignment)
                    {
                        case AlignmentX.Left:
                            left = 0;
                            break;
                        case AlignmentX.Center:
                            left = (surfaceSize.Width - targetWidth) / 2.0;
                            break;
                        case AlignmentX.Right:
                            left = surfaceSize.Width - targetWidth;
                            break;
                    }

                    // Vertical Alignment
                    switch (options.VerticalAlignment)
                    {
                        case AlignmentY.Top:
                            top = 0;
                            break;
                        case AlignmentY.Center:
                            top = (surfaceSize.Height - targetHeight) / 2.0;
                            break;
                        case AlignmentY.Bottom:
                            top = surfaceSize.Height - targetHeight;
                            break;
                    }

                    // Draw the image to the surface
                    using (var session = CanvasComposition.CreateDrawingSession(surface))
                    {
                        // Clear the surface with the SurfaceBackgroundColor
                        session.Clear(options.SurfaceBackgroundColor);
                        // Render the image
                        session.DrawImage(canvasBitmap,                         // CanvasBitmap
                            new Rect(left, top, targetWidth, targetHeight),     // Target Rectangle
                            new Rect(0, 0, sourceWidth, sourceHeight),          // Source Rectangle
                            options.Opacity,                                    // Opacity
                            options.Interpolation);                             // Interpolation
                    }
                }
            }
        }
コード例 #24
0
 /// <summary>
 /// Resizes the ImageSurface to the given size and redraws the ImageSurface
 /// by rendering the canvasBitmap onto the surface.
 /// </summary>
 /// <param name="surfaceLock">The object to lock to prevent multiple threads
 /// from accessing the surface at the same time.</param>
 /// <param name="surface">CompositionDrawingSurface</param>
 /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
 /// <param name="canvasBitmap">The CanvasBitmap on which the image is loaded.</param>
 public void RedrawImageSurface(object surfaceLock, CompositionDrawingSurface surface,
     ImageSurfaceOptions options, CanvasBitmap canvasBitmap)
 {
     // Render the image to the surface
     RenderBitmap(surfaceLock, surface, canvasBitmap, options);
 }
コード例 #25
0
        /// <summary>
        /// Renders the CanvasBitmap on the CompositionDrawingSurface based on the given options.
        /// </summary>
        /// <param name="surfaceLock">The object to lock to prevent multiple threads
        /// from accessing the surface at the same time.</param>
        /// <param name="surface">CompositionDrawingSurface on which the CanvasBitmap has to be rendered.</param>
        /// <param name="canvasBitmap">CanvasBitmap created by loading the image from the Uri</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        private static void RenderBitmap(object surfaceLock, CompositionDrawingSurface surface, CanvasBitmap canvasBitmap,
                                         ImageSurfaceOptions options)
        {
            var surfaceSize = surface.Size;

            // If the canvasBitmap is null, then just fill the surface with the SurfaceBackgroundColor
            if (canvasBitmap == null)
            {
                // No need to render if the width and/or height of the surface is zero
                if (surfaceSize.IsEmpty || surfaceSize.Width.IsZero() || surfaceSize.Height.IsZero())
                {
                    return;
                }

                //
                // Since 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 (surfaceLock)
                {
                    using (var session = CanvasComposition.CreateDrawingSession(surface))
                    {
                        // Clear the surface with the SurfaceBackgroundColor
                        session.Clear(options.SurfaceBackgroundColor);
                    }

                    // No need to proceed further
                    return;
                }
            }

            //
            // Since 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 (surfaceLock)
            {
                // Is AutoResize Enabled?
                if (options.AutoResize)
                {
                    // If AutoResize is allowed and the canvasBitmap size and surface size are
                    // not matching then resize the surface to match the canvasBitmap size.
                    //
                    // NOTE: HorizontalAlignment, Vertical Alignment and Stretch will be
                    // handled by the CompositionSurfaceBrush created using this surface.
                    //
                    if (canvasBitmap.Size != surfaceSize)
                    {
                        // Resize the surface
                        CanvasComposition.Resize(surface, canvasBitmap.Size);
                        surfaceSize = canvasBitmap.Size;
                    }

                    // No need to render if the width and/or height of the surface is zero
                    if (surfaceSize.IsEmpty || surface.Size.Width.IsZero() || surface.Size.Height.IsZero())
                    {
                        return;
                    }

                    // Draw the image to the surface
                    using (var session = CanvasComposition.CreateDrawingSession(surface))
                    {
                        // Render the image
                        session.DrawImage(canvasBitmap,                                                      // CanvasBitmap
                                          new Rect(0, 0, surfaceSize.Width, surfaceSize.Height),             // Target Rectangle
                                          new Rect(0, 0, canvasBitmap.Size.Width, canvasBitmap.Size.Height), // Source Rectangle
                                          options.Opacity,                                                   // Opacity
                                          options.Interpolation);                                            // Interpolation
                    }
                }
                else
                {
                    // No need to render if the width and/or height of the surface is zero
                    if (surfaceSize.IsEmpty || surface.Size.Width.IsZero() || surface.Size.Height.IsZero())
                    {
                        return;
                    }

                    var bitmapSize   = canvasBitmap.Size;
                    var sourceWidth  = bitmapSize.Width;
                    var sourceHeight = bitmapSize.Height;
                    var ratio        = sourceWidth / sourceHeight;
                    var targetWidth  = 0d;
                    var targetHeight = 0d;
                    var left         = 0d;
                    var top          = 0d;

                    // Stretch Mode
                    switch (options.Stretch)
                    {
                    case Stretch.None:
                        targetWidth  = sourceWidth;
                        targetHeight = sourceHeight;
                        break;

                    case Stretch.Fill:
                        targetWidth  = surfaceSize.Width;
                        targetHeight = surfaceSize.Height;
                        break;

                    case Stretch.Uniform:
                        // If width is greater than height
                        if (ratio > 1.0)
                        {
                            targetHeight = Math.Min(surfaceSize.Width / ratio, surfaceSize.Height);
                            targetWidth  = targetHeight * ratio;
                        }
                        else
                        {
                            targetWidth  = Math.Min(surfaceSize.Height * ratio, surfaceSize.Width);
                            targetHeight = targetWidth / ratio;
                        }
                        break;

                    case Stretch.UniformToFill:
                        // If width is greater than height
                        if (ratio > 1.0)
                        {
                            targetHeight = Math.Max(surfaceSize.Width / ratio, surfaceSize.Height);
                            targetWidth  = targetHeight * ratio;
                        }
                        else
                        {
                            targetWidth  = Math.Max(surfaceSize.Height * ratio, surfaceSize.Width);
                            targetHeight = targetWidth / ratio;
                        }
                        break;
                    }

                    // Horizontal Alignment
                    switch (options.HorizontalAlignment)
                    {
                    case AlignmentX.Left:
                        left = 0;
                        break;

                    case AlignmentX.Center:
                        left = (surfaceSize.Width - targetWidth) / 2.0;
                        break;

                    case AlignmentX.Right:
                        left = surfaceSize.Width - targetWidth;
                        break;
                    }

                    // Vertical Alignment
                    switch (options.VerticalAlignment)
                    {
                    case AlignmentY.Top:
                        top = 0;
                        break;

                    case AlignmentY.Center:
                        top = (surfaceSize.Height - targetHeight) / 2.0;
                        break;

                    case AlignmentY.Bottom:
                        top = surfaceSize.Height - targetHeight;
                        break;
                    }

                    // Draw the image to the surface
                    using (var session = CanvasComposition.CreateDrawingSession(surface))
                    {
                        // Clear the surface with the SurfaceBackgroundColor
                        session.Clear(options.SurfaceBackgroundColor);
                        // Render the image
                        session.DrawImage(canvasBitmap,                                   // CanvasBitmap
                                          new Rect(left, top, targetWidth, targetHeight), // Target Rectangle
                                          new Rect(0, 0, sourceWidth, sourceHeight),      // Source Rectangle
                                          options.Opacity,                                // Opacity
                                          options.Interpolation);                         // Interpolation
                    }
                }
            }
        }
コード例 #26
0
 /// <summary>
 /// Redraws the IImageMaskSurface by loading image from the new Uri and image options
 /// </summary>
 /// <param name="uri">Uri of the image to be loaded on to the image surface.</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>
 /// <returns>Task</returns>
 public Task RedrawAsync(Uri uri, Thickness padding, ImageSurfaceOptions options)
 {
     return(RedrawAsync(uri, Size, padding, options));
 }
コード例 #27
0
 /// <summary>
 /// Redraws the IImageSurface by loading image from the new Uri and image options
 /// </summary>
 /// <param name="uri">Uri of the image to be loaded on to the image surface.</param>
 /// <param name="options">The image's resize and alignment options in the allocated space.</param>
 /// <returns>Task</returns>
 public Task RedrawAsync(Uri uri, ImageSurfaceOptions options)
 {
     return(RedrawAsync(uri, Size, options));
 }
コード例 #28
0
        /// <summary>
        /// Creates a ImageSurface having the given size onto which an image (based on the Uri
        /// and the options) is loaded.
        /// </summary>
        /// <param name="uri">Uri of the image to be loaded onto the SurfaceImage.</param>
        /// <param name="size">New size of the SurfaceImage</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        /// <returns>ICompositionSurfaceImage</returns>
        public async Task<IImageSurface> CreateImageSurfaceAsync(Uri uri, Size size, ImageSurfaceOptions options)
        {
            // Initialize the SurfaceImage
            var surfaceImage = new ImageSurface(this, uri, size, options);

            // Render the image onto the surface
            await surfaceImage.RedrawAsync();

            return (IImageSurface)surfaceImage;
        }
コード例 #29
0
        /// <summary>
        /// Redraws the SurfaceImage by loading image from the new Uri and image options
        /// </summary>
        /// <param name="uri">Uri of the image to be loaded on to the image surface.</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        /// <returns>Task</returns>
        public Task RedrawAsync(Uri uri, ImageSurfaceOptions options)
        {
            // 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;
            // Set the image options
            Options = options;
            // Reload the SurfaceImage
            return RedrawSurfaceImageInternalAsync();
        }
コード例 #30
0
        /// <summary>
        /// Resizes the ImageSurface with the given size and redraws the ImageSurface by loading 
        /// image from the new Uri.
        /// </summary>
        /// <param name="surfaceLock">The object to lock to prevent multiple threads
        /// from accessing the surface at the same time.</param>
        /// <param name="surface">CompositionDrawingSurface</param>
        /// <param name="uri">Uri of the image to be loaded onto the SurfaceImage.</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        /// <param name="canvasBitmap">The CanvasBitmap on which the image is loaded.</param>
        /// <returns>CanvasBitmap</returns>
        public async Task<CanvasBitmap> RedrawImageSurfaceAsync(object surfaceLock, CompositionDrawingSurface surface,
            Uri uri, ImageSurfaceOptions options, CanvasBitmap canvasBitmap)
        {
            if ((canvasBitmap == null) && (uri != null))
            {
                try
                {
                    canvasBitmap = await CanvasBitmap.LoadAsync(_canvasDevice, uri);
                }
                catch (IOException)
                {
                    // Do nothing here as RenderBitmap method will fill the surface
                    // with options.SurfaceBackgroundColor as the image failed to load
                    // from Uri
                }
            }

            // Render the image to the surface
            RenderBitmap(surfaceLock, surface, canvasBitmap, options);

            return canvasBitmap;
        }
コード例 #31
0
        /// <summary>
        /// Resizes the SurfaceImage with the given size and redraws the SurfaceImage by loading 
        /// image from the new Uri.
        /// </summary>
        /// <param name="uri">Uri of the image to be loaded onto the SurfaceImage.</param>
        /// <param name="size">New size of the SurfaceImage</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        /// <returns>Task</returns>
        public async Task RedrawAsync(Uri uri, Size size, ImageSurfaceOptions options)
        {
            // If the given Uri differs from the previously stored Uri
            // dispose the existing canvasBitmap
            if (!_uri.IsEqualTo(uri))
            {
                _canvasBitmap?.Dispose();
                _canvasBitmap = null;
            }

            // Set the image options
            Options = options;
            // Resize the surface only if AutoResize option is disabled
            if (!Options.AutoResize)
            {
                // resize the SurfaceImage
                _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 SurfaceImage
            await RedrawSurfaceImageInternalAsync();
        }
コード例 #32
0
 /// <summary>
 /// Resizes and redraws the IImageMaskSurface using the given CanvasBitmap's alpha values and 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="options">The image's resize, alignment options and blur radius in the allocated space.</param>
 public void Redraw(CanvasBitmap surfaceBitmap, Size size, ImageSurfaceOptions options)
 {
     Redraw(surfaceBitmap, size, MaskPadding, options);
 }
コード例 #33
0
 /// <summary>
 /// Resizes the SurfaceImage to the new size.
 /// </summary>
 /// <param name="size">New size of the SurfaceImage</param>
 /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
 public void Resize(Size size, ImageSurfaceOptions options)
 {
     // Set the image options
     Options = options;
     // Resize the surface only if AutoResize option is disabled
     if (!Options.AutoResize)
     {
         // resize the SurfaceImage
         _generator.ResizeDrawingSurface(_surfaceLock, _surface, size);
         // Set the size
         Size = _surface?.Size ?? new Size(0, 0);
     }
     // resize the SurfaceImage
     RedrawSurfaceImageInternal();
 }
コード例 #34
0
 /// <summary>
 /// Resizes and redraws the IImageMaskSurface using the alpha values of the image in the given IImageMaskSurface
 /// with the given padding and options.
 /// </summary>
 /// <param name="imageMaskSurface">IImageMaskSurface whose image's 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">Describes the image's resize, alignment and blur radius options in the allocated space.</param>
 public void Redraw(IImageMaskSurface imageMaskSurface, Size size, Thickness padding, ImageSurfaceOptions options)
 {
     Redraw(imageMaskSurface?.SurfaceBitmap, size, padding, options);
 }
コード例 #35
0
 /// <summary>
 /// Disposes the resources used by the SurfaceImage
 /// </summary>
 public void Dispose()
 {
     _surface?.Dispose();
     if (_generator != null)
         _generator.DeviceReplaced -= OnDeviceReplaced;
     _canvasBitmap?.Dispose();
     _canvasBitmap = null;
     _surface = null;
     _generator = null;
     _uri = null;
     Options = null;
 }
コード例 #36
0
 /// <summary>
 /// Redraws the SurfaceImage with the given image options
 /// </summary>
 /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
 /// <returns>Task</returns>
 public void Redraw(ImageSurfaceOptions options)
 {
     // Set the image options
     Options = options;
     // Redraw the SurfaceImage
     RedrawSurfaceImageInternal();
 }
コード例 #37
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="generator">ICompositionMaskGeneratorInternal object</param>
        /// <param name="uri">Uri of the image to be loaded onto the SurfaceImage.</param>
        /// <param name="size">Size of the SurfaceImage</param>
        /// <param name="options">Describes the image's resize and alignment options in the allocated space.</param>
        public ImageSurface(ICompositionGeneratorInternal generator, Uri uri, Size size, ImageSurfaceOptions options)
        {
            if (generator == null)
                throw new ArgumentNullException(nameof(generator), "CompositionGenerator cannot be null!");

            _generator = generator;
            _surfaceLock = new object();
            // Create the Surface of the SurfaceImage
            _surface = _generator.CreateDrawingSurface(_surfaceLock, size);
            Size = _surface?.Size ?? new Size(0, 0);
            _uri = uri;
            _canvasBitmap = null;
            // Set the image options
            Options = options;
            // Subscribe to DeviceReplaced event
            _generator.DeviceReplaced += OnDeviceReplaced;
        }