Ejemplo n.º 1
0
        // crop
        public SoftwareBitmap Crop(SoftwareBitmap softwareBitmap, Rect bounds)
        {
            if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8)
            {
                softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8);
            }

            var resourceCreator = CanvasDevice.GetSharedDevice();

            using (var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap))
                using (var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, (float)bounds.Width, (float)bounds.Width, canvasBitmap.Dpi))
                    using (var drawingSession = canvasRenderTarget.CreateDrawingSession())
                        using (var cropEffect = new CropEffect())
                            using (var atlasEffect = new AtlasEffect())
                            {
                                drawingSession.Clear(Colors.White);

                                cropEffect.SourceRectangle = bounds;
                                cropEffect.Source          = canvasBitmap;

                                atlasEffect.SourceRectangle = bounds;
                                atlasEffect.Source          = cropEffect;

                                drawingSession.DrawImage(atlasEffect);
                                drawingSession.Flush();

                                return(SoftwareBitmap.CreateCopyFromBuffer(canvasRenderTarget.GetPixelBytes().AsBuffer(), BitmapPixelFormat.Bgra8, (int)bounds.Width, (int)bounds.Width, BitmapAlphaMode.Premultiplied));
                            }
        }
Ejemplo n.º 2
0
        public static SoftwareBitmap CropAndResize(this SoftwareBitmap softwareBitmap, Rect bounds, float newWidth, float newHeight)
        {
            var resourceCreator = CanvasDevice.GetSharedDevice();

            using (var canvasBitmap = CanvasBitmap.CreateFromSoftwareBitmap(resourceCreator, softwareBitmap))
                using (var canvasRenderTarget = new CanvasRenderTarget(resourceCreator, newWidth, newHeight, canvasBitmap.Dpi))
                    using (var drawingSession = canvasRenderTarget.CreateDrawingSession())
                        using (var scaleEffect = new ScaleEffect())
                            using (var cropEffect = new CropEffect())
                                using (var atlasEffect = new AtlasEffect())
                                {
                                    drawingSession.Clear(Colors.White);

                                    cropEffect.SourceRectangle = bounds;
                                    cropEffect.Source          = canvasBitmap;

                                    atlasEffect.SourceRectangle = bounds;
                                    atlasEffect.Source          = cropEffect;

                                    scaleEffect.Source = atlasEffect;
                                    scaleEffect.Scale  = new System.Numerics.Vector2(newWidth / (float)bounds.Width, newHeight / (float)bounds.Height);
                                    drawingSession.DrawImage(scaleEffect);
                                    drawingSession.Flush();

                                    return(SoftwareBitmap.CreateCopyFromBuffer(canvasRenderTarget.GetPixelBytes().AsBuffer(), BitmapPixelFormat.Bgra8, (int)newWidth, (int)newHeight, BitmapAlphaMode.Premultiplied));
                                }
        }