private async Task <(Stream imageStream, string contentType)> GetPngEncodedPreview( IImageElementValue imageElementValue, int templateCode, int width, int height, Action <Image <Rgba32> > mutateImage) { var cts = new CancellationTokenSource(_requestTimeout); var rawStream = await GetRawStream(imageElementValue, cts.Token); EnsureRequestCanBeProcessed(rawStream, cts.Token); using (var source = Decode(templateCode, rawStream)) { using (var target = Crop(source, imageElementValue)) { Resize(target, new Size(width, height)); mutateImage?.Invoke(target); var pngFormat = ImageFormats.Png; var targetStream = Encode(target, pngFormat); return(targetStream, pngFormat.DefaultMimeType); } } }
public async Task <(Stream imageStream, string contentType)> GetPreview( IImageElementValue imageElementValue, int templateCode, int width, int height) { return(await GetPngEncodedPreview(imageElementValue, templateCode, width, height, null)); }
public async Task <(Stream imageStream, string contentType)> GetRoundedPreview( IImageElementValue imageElementValue, int templateCode, int width, int height) { return(await GetPngEncodedPreview( imageElementValue, templateCode, width, height, target => ApplyRoundedCorners(target, target.Height * 0.5f))); }
public static bool TryGetSizeSpecificBitmapImageRawValue(this IImageElementValue imageElementValue, int width, int height, out string rawValue) { rawValue = null; if (imageElementValue is ICompositeBitmapImageElementValue compositeBitmapImageElementValue) { var sizeSpecificImage = compositeBitmapImageElementValue.SizeSpecificImages .SingleOrDefault(x => x.Size.Width == width && x.Size.Height == height); if (sizeSpecificImage != null) { rawValue = sizeSpecificImage.Raw; return(true); } return(false); } return(false); }
public async Task <(Stream imageStream, string contentType)> GetScaledPreview( IImageElementValue imageElementValue, int templateCode, int width, int height) { var cts = new CancellationTokenSource(_requestTimeout); var rawStream = await GetRawStream(imageElementValue, cts.Token); await EnsureRequestCanBeProcessed(rawStream, cts.Token); using (var source = Decode(templateCode, rawStream, out var imageFormat)) { var anchorPositionMode = EvaluateAnchorPositionMode(imageElementValue); Resize(source, new Size(width, height), anchorPositionMode); var targetStream = Encode(source, imageFormat); return(targetStream, imageFormat.DefaultMimeType); } }
private static AnchorPositionMode EvaluateAnchorPositionMode(IImageElementValue imageElementValue) { if (!(imageElementValue is IScalableBitmapImageElementValue scalableBitmapImageElementValue)) { return(AnchorPositionMode.Center); } switch (scalableBitmapImageElementValue.Anchor) { case Anchor.TopLeft: return(AnchorPositionMode.TopLeft); case Anchor.Top: return(AnchorPositionMode.Top); case Anchor.TopRight: return(AnchorPositionMode.TopRight); case Anchor.Left: return(AnchorPositionMode.Left); case Anchor.Middle: return(AnchorPositionMode.Center); case Anchor.Right: return(AnchorPositionMode.Right); case Anchor.BottomLeft: return(AnchorPositionMode.BottomLeft); case Anchor.Bottom: return(AnchorPositionMode.Bottom); case Anchor.BottomRight: return(AnchorPositionMode.BottomRight); default: return(AnchorPositionMode.Center); } }
private static Image <Rgba32> Crop(Image <Rgba32> image, IImageElementValue elementValue) { if (!(elementValue is ICompositeBitmapImageElementValue compositeBitmapImage)) { return(image); } var imageRectangle = image.Bounds(); var cropAreaRectangle = new Rectangle( compositeBitmapImage.CropArea.Left, compositeBitmapImage.CropArea.Top, compositeBitmapImage.CropArea.Width, compositeBitmapImage.CropArea.Height); if (!imageRectangle.IntersectsWith(cropAreaRectangle)) { return(image); } if (imageRectangle.Contains(cropAreaRectangle)) { image.Mutate(ctx => ctx.Crop(cropAreaRectangle)); return(image); } Rgba32 backgroundColor; var leftTopPixel = image[imageRectangle.Left, imageRectangle.Top]; var rightTopPixel = image[imageRectangle.Right - 1, imageRectangle.Top]; var rightBottomPixel = image[imageRectangle.Right - 1, imageRectangle.Bottom - 1]; var leftBottomPixel = image[imageRectangle.Left, imageRectangle.Bottom - 1]; if (ArePixelColorsClose(leftTopPixel, rightBottomPixel) && ArePixelColorsClose(rightTopPixel, rightBottomPixel) && ArePixelColorsClose(rightBottomPixel, leftBottomPixel) && ArePixelColorsClose(leftBottomPixel, leftTopPixel)) { backgroundColor = leftTopPixel; } else { var redTop = GetMeanColorValue(image.Width, (intervalCount, index) => GetImagePixelColorOnTop(image, intervalCount, index).R); var greenTop = GetMeanColorValue(image.Width, (intervalCount, index) => GetImagePixelColorOnTop(image, intervalCount, index).G); var blueTop = GetMeanColorValue(image.Width, (intervalCount, index) => GetImagePixelColorOnTop(image, intervalCount, index).B); var redBottom = GetMeanColorValue(image.Width, (intervalCount, index) => GetImagePixelColorOnBottom(image, intervalCount, index).R); var greenBottom = GetMeanColorValue(image.Width, (intervalCount, index) => GetImagePixelColorOnBottom(image, intervalCount, index).G); var blueBottom = GetMeanColorValue(image.Width, (intervalCount, index) => GetImagePixelColorOnBottom(image, intervalCount, index).B); var redLeft = GetMeanColorValue(image.Height, (intervalCount, index) => GetImagePixelColorOnLeft(image, intervalCount, index).R); var greenLeft = GetMeanColorValue(image.Height, (intervalCount, index) => GetImagePixelColorOnLeft(image, intervalCount, index).G); var blueLeft = GetMeanColorValue(image.Height, (intervalCount, index) => GetImagePixelColorOnLeft(image, intervalCount, index).B); var redRight = GetMeanColorValue(image.Height, (intervalCount, index) => GetImagePixelColorOnRight(image, intervalCount, index).R); var greenRight = GetMeanColorValue(image.Height, (intervalCount, index) => GetImagePixelColorOnRight(image, intervalCount, index).G); var blueRight = GetMeanColorValue(image.Height, (intervalCount, index) => GetImagePixelColorOnRight(image, intervalCount, index).B); backgroundColor = new Rgba32( (byte)((redTop + redBottom + redLeft + redRight) / 4), (byte)((greenTop + greenBottom + greenLeft + greenRight) / 4), (byte)((blueTop + blueBottom + blueLeft + blueRight) / 4)); } var unionRectangle = Rectangle.Union(imageRectangle, cropAreaRectangle); cropAreaRectangle.Offset(-unionRectangle.X, -unionRectangle.Y); var extentImage = new Image <Rgba32>(unionRectangle.Width, unionRectangle.Height); extentImage.Mutate(x => x.BackgroundColor(backgroundColor) .DrawImage(image, image.Size(), new Point(-unionRectangle.X, -unionRectangle.Y), GraphicsOptions.Default) .Crop(cropAreaRectangle)); return(extentImage); }