Exemplo n.º 1
0
            static CanvasBitmap MakeThumbnailPretty(CanvasBitmap capturedBitmap, float thumbnailWidth, float thumbnailHeight, Rect targetRect)
            {
                var pixelColors = capturedBitmap.GetPixelColors();

                // Remove any unused space around the edge of the bitmap, so it will fill the thumbnail.
                Rect cropRect = CropCapturedBitmap(capturedBitmap, pixelColors);

                // Choose a (hopefully) aesthetically pleasing background color.
                Color backgroundColor = ChooseBackgroundColor(pixelColors);

                // Apply letterbox scaling to fit the image into the target thumbnail.
                Vector2 outputSize = new Vector2((float)targetRect.Width, (float)targetRect.Height);
                var     sourceSize = new Vector2((float)cropRect.Width, (float)cropRect.Height);
                var     letterbox  = Utils.GetDisplayTransform(outputSize, sourceSize);
                var     translate  = Matrix3x2.CreateTranslation((float)targetRect.X, (float)targetRect.Y);

                // Position the image where we want it.
                var scaledImage = new Transform2DEffect
                {
                    Source = new AtlasEffect
                    {
                        Source          = capturedBitmap,
                        SourceRectangle = cropRect,
                    },
                    InterpolationMode = CanvasImageInterpolation.HighQualityCubic,
                    TransformMatrix   = letterbox * translate,
                };

                // Create the final thumbnail image.
                var finalImage = new CompositeEffect
                {
                    Sources =
                    {
                        // Blurred shadow.
                        new ShadowEffect
                        {
                            Source = new MorphologyEffect
                            {
                                Source = scaledImage,
                                Mode   = MorphologyEffectMode.Dilate,
                                Width  = dilateAmount,
                                Height = dilateAmount,
                            },
                            BlurAmount = blurAmount,
                        },

                        // Overlay the image itself.
                        scaledImage
                    }
                };

                // Rasterize the effect into a rendertarget.
                CanvasRenderTarget output = new CanvasRenderTarget(capturedBitmap.Device, thumbnailWidth, thumbnailHeight, 96);

                using (var ds = output.CreateDrawingSession())
                {
                    ds.Clear(backgroundColor);
                    ds.DrawImage(finalImage);
                }

                return(output);
            }