Exemplo n.º 1
0
        /// <inheritdoc />
        public System.IO.Stream CreateImageMask(System.IO.Stream sourceImageStream, string maskColorCode)
        {
            #region validation

            if (sourceImageStream == null)
            {
                throw new ArgumentNullException(nameof(sourceImageStream));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            if (!regex.IsMatch(maskColorCode))
            {
                throw new ArgumentException($"Color code {maskColorCode} is malformed!");
            }

            #endregion

            // create solid color brush to draw mask
            IBrush brush = new SolidBrush(Rgba32Util.InitFromHex(maskColorCode));

            // create graphic options for overlay of alpha-channel-image with solid color brush
            //   non-transparent pixels of image should be overlayed with solid color brush
            GraphicsOptions graphicsOptions = new GraphicsOptions()
            {
                AlphaCompositionMode = PixelAlphaCompositionMode.SrcIn,
                ColorBlendingMode    = PixelColorBlendingMode.Normal
            };

            using (Image <Rgba32> originalImage = LoadImageFromStream <Rgba32>(sourceImageStream, out IImageFormat imageFormat))
            {
                originalImage.Mutate(x => x.Fill(graphicsOptions, brush));
                return(SaveAsStream(originalImage, imageFormat));
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public System.IO.Stream FillPolygon(System.IO.Stream sourceImageStream, string polygonColorCode, Polygon polygon)
        {
            #region validation

            if (sourceImageStream == null)
            {
                throw new ArgumentNullException(nameof(sourceImageStream));
            }

            if (string.IsNullOrEmpty(polygonColorCode))
            {
                throw new ArgumentNullException(nameof(polygonColorCode));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            if (!regex.IsMatch(polygonColorCode))
            {
                throw new ArgumentException($"Color code {polygonColorCode } is malformed!");
            }

            if (polygon == null)
            {
                throw new ArgumentNullException(nameof(polygon));
            }

            if (polygon.Points == null)
            {
                throw new ArgumentException(nameof(polygon.Points));
            }

            if (polygon.Points.Count() < 2)
            {
                throw new ArgumentException($"Polygon {nameof(polygon)} has to contain more than one entry!");
            }

            #endregion

            IBrush brush = new SolidBrush(Rgba32Util.InitFromHex(polygonColorCode));

            ShapeGraphicsOptions graphicsOptions = new ShapeGraphicsOptions()
            {
                GraphicsOptions = new GraphicsOptions()
                {
                    ColorBlendingMode = PixelColorBlendingMode.Normal
                }
            };

            using (Image <Rgba32> originalImage = LoadImageFromStream <Rgba32>(sourceImageStream, out IImageFormat imageFormat))
            {
                originalImage.Mutate(c => c.FillPolygon(graphicsOptions, brush, polygon.Points.Select(p => new PointF(p.X, p.Y)).ToArray()));

                return(SaveAsStream(originalImage, imageFormat));
            }
        }
Exemplo n.º 3
0
        public void GetHexColorRegexLength9Test()
        {
            // Init
            Regex  hexColorRegex = HexColorUtil.GetHexColorRegex();
            string hexColor      = "#f5C93dd47";

            // Act
            bool result = hexColorRegex.IsMatch(hexColor);

            // Assert
            Assert.IsFalse(result);
        }
Exemplo n.º 4
0
        public void GetHexColorRegexRGBATest()
        {
            // Init
            Regex  hexColorRegex = HexColorUtil.GetHexColorRegex();
            string hexColor      = "#f5C9";

            // Act
            bool result = hexColorRegex.IsMatch(hexColor);

            // Assert
            Assert.IsTrue(result);
        }
Exemplo n.º 5
0
        public void GetHexColorRegexInvalidCharacterTest()
        {
            // Init
            Regex  hexColorRegex = HexColorUtil.GetHexColorRegex();
            string hexColor      = "#f5C93Td4";

            // Act
            bool result = hexColorRegex.IsMatch(hexColor);

            // Assert
            Assert.IsFalse(result);
        }
Exemplo n.º 6
0
        public System.IO.Stream RecolorImage(System.IO.Stream imageStream, string sourceColorCode, string targetColorCode)
        {
            #region validation

            if (imageStream == null)
            {
                throw new ArgumentNullException(nameof(imageStream));
            }

            if (string.IsNullOrEmpty(sourceColorCode))
            {
                throw new ArgumentNullException(nameof(sourceColorCode));
            }

            if (string.IsNullOrEmpty(targetColorCode))
            {
                throw new ArgumentNullException(nameof(targetColorCode));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            if (!regex.IsMatch(sourceColorCode))
            {
                throw new ArgumentException($"Color code {sourceColorCode} is malformed!");
            }

            if (!regex.IsMatch(targetColorCode))
            {
                throw new ArgumentException($"Color code {targetColorCode} is malformed!");
            }

            #endregion

            Rgba32 sourceColor = Rgba32Util.InitFromHex(sourceColorCode);
            Rgba32 targetColor = Rgba32Util.InitFromHex(targetColorCode);

            RecolorBrush <Rgba32> recolorBrush = new RecolorBrush <Rgba32>(sourceColor, targetColor, 0.2f);

            using (Image <Rgba32> originalImage = LoadImageFromStream(imageStream))
                using (Image <Rgba32> targetImage = originalImage.Clone())
                {
                    targetImage.Mutate(x => x.Fill(recolorBrush));

                    return(SaveAsStream(targetImage));
                }
        }
Exemplo n.º 7
0
        /// <inheritdoc />
        public System.IO.Stream DrawRects(System.IO.Stream sourceImageStream, IEnumerable <DrawableRect> rects)
        {
            #region validation

            if (sourceImageStream == null)
            {
                throw new ArgumentNullException(nameof(sourceImageStream));
            }

            if (rects == null)
            {
                throw new ArgumentNullException(nameof(rects));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            foreach (DrawableRect rect in rects)
            {
                if (!regex.IsMatch(rect.BorderColorCode))
                {
                    throw new ArgumentException($"Color code {rect.BorderColorCode} is malformed!");
                }
            }

            #endregion

            using (Image <Rgba32> sourceImage = LoadImageFromStream <Rgba32>(sourceImageStream, out IImageFormat imageFormat))
            {
                foreach (DrawableRect rect in rects)
                {
                    Rgba32     rectColor = Rgba32Util.InitFromHex(rect.BorderColorCode);
                    SolidBrush brush     = new SolidBrush(rectColor);

                    sourceImage.Mutate(x => x.DrawPolygon(brush, rect.BorderThickness, new PointF[] {
                        new Vector2(rect.X, rect.Y),
                        new Vector2(rect.X + rect.Width, rect.Y),
                        new Vector2(rect.X + rect.Width, rect.Y + rect.Height),
                        new Vector2(rect.X, rect.Y + rect.Height)
                    }));
                }

                return(SaveAsStream(sourceImage, imageFormat));
            }
        }
Exemplo n.º 8
0
        /// <inheritdoc />
        public System.IO.Stream CreateImage(Rect dimension, string backgroundColorCode)
        {
            #region validation

            if (dimension == null)
            {
                throw new ArgumentNullException(nameof(dimension));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            if (!regex.IsMatch(backgroundColorCode))
            {
                throw new ArgumentException($"Color code {backgroundColorCode} is malformed!");
            }

            #endregion

            using (Image <Rgba32> image = new Image <Rgba32>(SixLabors.ImageSharp.Configuration.Default, dimension.Width, dimension.Height, Rgba32Util.InitFromHex(backgroundColorCode)))
            {
                return(SaveAsStream(image, PngFormat.Instance));
            }
        }
Exemplo n.º 9
0
        public System.IO.Stream DrawRect(System.IO.Stream sourceImageStream, Rect rect, string rectColorCode)
        {
            #region validation

            if (sourceImageStream == null)
            {
                throw new ArgumentNullException(nameof(sourceImageStream));
            }

            if (rect == null)
            {
                throw new ArgumentNullException(nameof(rect));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            if (!regex.IsMatch(rectColorCode))
            {
                throw new ArgumentException($"Color code {rectColorCode} is malformed!");
            }

            #endregion

            Rgba32 rectColor = Rgba32Util.InitFromHex(rectColorCode);

            SolidBrush <Rgba32> brush = new SolidBrush <Rgba32>(rectColor);
            using (Image <Rgba32> sourceImage = LoadImageFromStream(sourceImageStream))
            {
                sourceImage.Mutate(x => x.DrawPolygon(brush, 5.0f, new SixLabors.Primitives.PointF[] {
                    new Vector2(rect.X, rect.Y),
                    new Vector2(rect.X + rect.Width, rect.Y),
                    new Vector2(rect.X + rect.Width, rect.Y + rect.Height),
                    new Vector2(rect.X, rect.Y + rect.Height)
                }));

                return(SaveAsStream(sourceImage));
            }
        }
Exemplo n.º 10
0
        /// <inheritdoc />
        public System.IO.Stream DrawRect(System.IO.Stream sourceImageStream, Rect rect, string rectColorCode)
        {
            #region validation

            if (sourceImageStream == null)
            {
                throw new ArgumentNullException(nameof(sourceImageStream));
            }

            if (rect == null)
            {
                throw new ArgumentNullException(nameof(rect));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            if (!regex.IsMatch(rectColorCode))
            {
                throw new ArgumentException($"Color code {rectColorCode} is malformed!");
            }

            #endregion

            List <DrawableRect> drawableRects = new List <DrawableRect>
            {
                new DrawableRect()
                {
                    BorderColorCode = rectColorCode,
                    X      = rect.X,
                    Y      = rect.Y,
                    Width  = rect.Width,
                    Height = rect.Height
                }
            };

            return(DrawRects(sourceImageStream, drawableRects));
        }
Exemplo n.º 11
0
        public System.IO.Stream FillImage(System.IO.Stream imageStream, string fillColor, int?targetImageWidth, int?targetImageHeight)
        {
            #region valiation

            if (imageStream == null)
            {
                throw new ArgumentNullException(nameof(imageStream));
            }

            if (string.IsNullOrEmpty(fillColor))
            {
                throw new ArgumentNullException(nameof(fillColor));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            if (!regex.IsMatch(fillColor))
            {
                throw new ArgumentException($"Color code {fillColor} is malformed!");
            }

            #endregion

            if (!targetImageWidth.HasValue || !targetImageHeight.HasValue)
            {
                return(FillSquareImage(imageStream, fillColor));
            }

            // targetWidth or targetHeight has to be original size, because of positioning image into borders

            // get original size of image
            Rect imageDimension = ImageUtil.GetDimensionFromImage(imageStream);

            int targetWidth  = imageDimension.Width;
            int targetHeight = imageDimension.Height;

            double scalingFactorWidth  = (double)targetImageWidth / (double)imageDimension.Width;
            double scalingFactorHeight = (double)targetImageHeight / (double)imageDimension.Height;

            if (scalingFactorHeight == scalingFactorWidth)
            {
                // quadratic
            }
            else if (scalingFactorWidth > scalingFactorHeight)
            {
                targetWidth = (int)((double)targetImageWidth / (double)targetImageHeight * imageDimension.Height);
            }
            else
            {
                targetHeight = (int)((double)targetImageHeight / (double)targetImageWidth * imageDimension.Width);
            }

            // create parameter for resizing image
            ResizeOptions resizeOptions = new ResizeOptions()
            {
                Size = new SixLabors.Primitives.Size(targetWidth, targetHeight),
                Mode = ResizeMode.BoxPad
            };

            Rgba32 backgroundColor = Rgba32Util.InitFromHex(fillColor);

            using (Image <Rgba32> originalImage = LoadImageFromStream(imageStream))
                using (Image <Rgba32> targetImage = originalImage.Clone(ctx => ctx.Resize(resizeOptions)))
                {
                    targetImage.Mutate(x => x.BackgroundColor(backgroundColor));

                    return(SaveAsStream(targetImage));
                }
        }
Exemplo n.º 12
0
        /// <inheritdoc />
        public System.IO.Stream DrawCuboids(System.IO.Stream sourceImageStream, IEnumerable <DrawableCuboid> cuboids)
        {
            #region validation

            if (sourceImageStream == null)
            {
                throw new ArgumentNullException(nameof(sourceImageStream));
            }

            if (cuboids == null)
            {
                throw new ArgumentNullException(nameof(cuboids));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            foreach (DrawableCuboid rect in cuboids)
            {
                if (!regex.IsMatch(rect.BorderColorCode))
                {
                    throw new ArgumentException($"Color code {rect.BorderColorCode} is malformed!");
                }
            }

            #endregion

            using (Image <Rgba32> sourceImage = LoadImageFromStream <Rgba32>(sourceImageStream, out IImageFormat imageFormat))
            {
                foreach (DrawableCuboid cuboid in cuboids)
                {
                    Rgba32     rectColor = Rgba32Util.InitFromHex(cuboid.BorderColorCode);
                    SolidBrush brush     = new SolidBrush(rectColor);

                    Rectangle background = new Rectangle(cuboid.XBackground, cuboid.YBackground, cuboid.WidthBackground, cuboid.HeightBackground);
                    Rectangle foreground = new Rectangle(cuboid.X, cuboid.Y, cuboid.Width, cuboid.Height);

                    Rgba32 backgroundColor = rectColor;
                    backgroundColor.A = (byte)(255 * 0.2);

                    // draw foreground
                    sourceImage.Mutate(x => x.FillPolygon(backgroundColor, new PointF[] {
                        new Vector2(cuboid.X, cuboid.Y),
                        new Vector2(cuboid.X + cuboid.Width, cuboid.Y),
                        new Vector2(cuboid.X + cuboid.Width, cuboid.Y + cuboid.Height),
                        new Vector2(cuboid.X, cuboid.Y + cuboid.Height)
                    }));
                    sourceImage.Mutate(x => x.DrawPolygon(brush, cuboid.BorderThickness, new PointF[] {
                        new Vector2(cuboid.X, cuboid.Y),
                        new Vector2(cuboid.X + cuboid.Width, cuboid.Y),
                        new Vector2(cuboid.X + cuboid.Width, cuboid.Y + cuboid.Height),
                        new Vector2(cuboid.X, cuboid.Y + cuboid.Height)
                    }));

                    SolidBrush backgroundBrush = new SolidBrush(backgroundColor);

                    // draw background
                    // upper line
                    if (cuboid.Y >= cuboid.YBackground)
                    {
                        sourceImage.Mutate(x => x.DrawLines(backgroundBrush, cuboid.BorderThickness, new PointF[] {
                            new Vector2(cuboid.XBackground, cuboid.YBackground),
                            new Vector2(cuboid.XBackground + cuboid.WidthBackground, cuboid.YBackground)
                        }));
                    }

                    // left line
                    if (cuboid.X >= cuboid.XBackground)
                    {
                        sourceImage.Mutate(x => x.DrawLines(backgroundBrush, cuboid.BorderThickness, new PointF[] {
                            new Vector2(cuboid.XBackground, cuboid.YBackground),
                            new Vector2(cuboid.XBackground, cuboid.YBackground + cuboid.HeightBackground)
                        }));
                    }

                    // lower line
                    if (cuboid.Y + cuboid.Height <= cuboid.YBackground + cuboid.HeightBackground)
                    {
                        sourceImage.Mutate(x => x.DrawLines(backgroundBrush, cuboid.BorderThickness, new PointF[] {
                            new Vector2(cuboid.XBackground + cuboid.WidthBackground, cuboid.YBackground + cuboid.HeightBackground),
                            new Vector2(cuboid.XBackground, cuboid.YBackground + cuboid.HeightBackground)
                        }));
                    }

                    // right line
                    if (cuboid.X + cuboid.Width <= cuboid.XBackground + cuboid.WidthBackground)
                    {
                        sourceImage.Mutate(x => x.DrawLines(backgroundBrush, cuboid.BorderThickness, new PointF[] {
                            new Vector2(cuboid.XBackground + cuboid.WidthBackground, cuboid.YBackground),
                            new Vector2(cuboid.XBackground + cuboid.WidthBackground, cuboid.YBackground + cuboid.HeightBackground)
                        }));
                    }


                    // connect upper left corners if not hidden by foreground
                    if (foreground.Top > background.Top || foreground.Left > background.Left)
                    {
                        sourceImage.Mutate(x => x.DrawLines(backgroundBrush, cuboid.BorderThickness, new PointF[] {
                            new Vector2(cuboid.X, cuboid.Y),
                            new Vector2(cuboid.XBackground, cuboid.YBackground)
                        }));
                    }

                    // connect upper right corners if not hidden by foreground
                    if (foreground.Top > background.Top || foreground.Right < background.Right)
                    {
                        sourceImage.Mutate(x => x.DrawLines(backgroundBrush, cuboid.BorderThickness, new PointF[] {
                            new Vector2(cuboid.X + cuboid.Width, cuboid.Y),
                            new Vector2(cuboid.XBackground + cuboid.WidthBackground, cuboid.YBackground)
                        }));
                    }

                    // connect lower left corners if not hidden by foreground
                    if (foreground.Bottom < background.Bottom || foreground.Left > background.Left)
                    {
                        sourceImage.Mutate(x => x.DrawLines(backgroundBrush, cuboid.BorderThickness, new PointF[] {
                            new Vector2(cuboid.X, cuboid.Y + cuboid.Height),
                            new Vector2(cuboid.XBackground, cuboid.YBackground + cuboid.HeightBackground)
                        }));
                    }


                    // connect lower right corners if not hidden by foreground
                    if (foreground.Bottom < background.Bottom || foreground.Right < background.Right)
                    {
                        sourceImage.Mutate(x => x.DrawLines(backgroundBrush, cuboid.BorderThickness, new PointF[] {
                            new Vector2(cuboid.X + cuboid.Width, cuboid.Y + cuboid.Height),
                            new Vector2(cuboid.XBackground + cuboid.WidthBackground, cuboid.YBackground + cuboid.HeightBackground)
                        }));
                    }
                }

                return(SaveAsStream(sourceImage, imageFormat));
            }
        }
Exemplo n.º 13
0
        /// <inheritdoc />
        public System.IO.Stream RecolorImage(System.IO.Stream imageStream, string sourceColorCode, string targetColorCode)
        {
            #region validation

            if (imageStream == null)
            {
                throw new ArgumentNullException(nameof(imageStream));
            }

            if (string.IsNullOrEmpty(sourceColorCode))
            {
                throw new ArgumentNullException(nameof(sourceColorCode));
            }

            if (string.IsNullOrEmpty(targetColorCode))
            {
                throw new ArgumentNullException(nameof(targetColorCode));
            }

            Regex regex = HexColorUtil.GetHexColorRegex();
            if (!regex.IsMatch(sourceColorCode))
            {
                throw new ArgumentException($"Color code {sourceColorCode} is malformed!");
            }

            if (!regex.IsMatch(targetColorCode))
            {
                throw new ArgumentException($"Color code {targetColorCode} is malformed!");
            }

            #endregion

            Rgba32 sourceColor = Rgba32Util.InitFromHex(sourceColorCode);
            Rgba32 targetColor = Rgba32Util.InitFromHex(targetColorCode);
            float  threshold   = 0.1f;

            // RecolorBrush recolorBrush = new RecolorBrush(sourceColor, targetColor, threshold);

            using (Image <Rgba32> originalImage = LoadImageFromStream <Rgba32>(imageStream, out IImageFormat imageFormat))
                using (Image <Rgba32> targetImage = originalImage.Clone())
                {
                    // fill does not repace correctly. transparent colors will not be generated to image
                    // targetImage.Mutate(x => x.Fill(recolorBrush));

                    var rmin = sourceColor.R - (sourceColor.R * threshold);
                    var rmax = sourceColor.R + (sourceColor.R * threshold);
                    var gmin = sourceColor.G - (sourceColor.G * threshold);
                    var gmax = sourceColor.G + (sourceColor.G * threshold);
                    var bmin = sourceColor.B - (sourceColor.B * threshold);
                    var bmax = sourceColor.B + (sourceColor.B * threshold);

                    for (int x = 0; x < targetImage.Width; x++)
                    {
                        for (int y = 0; y < targetImage.Height; y++)
                        {
                            var pixel = targetImage[x, y];
                            if (pixel.R > rmin && pixel.R < rmax && pixel.G > gmin && pixel.G < gmax && pixel.B > bmin && pixel.B < bmax)
                            {
                                targetImage[x, y] = targetColor;
                            }
                        }
                    }

                    return(SaveAsStream(targetImage, imageFormat));
                }
        }