示例#1
0
        /// <summary>
        /// Add a password to the image.
        /// </summary>
        /// <param name="curImg">Image to add password to.</param>
        /// <param name="pass">Password to add to top left corner.</param>
        /// <returns>Image with the password in the top left corner.</returns>
        private (Stream, string) AddPassword(byte[] curImg, string pass)
        {
            // draw lower, it looks better
            pass = pass.TrimTo(10, true).ToLowerInvariant();
            using (var img = Image.Load <Rgba32>(curImg, out var format))
            {
                // choose font size based on the image height, so that it's visible
                var font = _fonts.NotoSans.CreateFont(img.Height / 12, FontStyle.Bold);
                img.Mutate(x =>
                {
                    // measure the size of the text to be drawing
                    var size = TextMeasurer.Measure(pass, new RendererOptions(font, new PointF(0, 0)));

                    // fill the background with black, add 5 pixels on each side to make it look better
                    x.FillPolygon(Color.ParseHex("00000080"),
                                  new PointF(0, 0),
                                  new PointF(size.Width + 5, 0),
                                  new PointF(size.Width + 5, size.Height + 10),
                                  new PointF(0, size.Height + 10));

                    // draw the password over the background
                    x.DrawText(pass,
                               font,
                               SixLabors.ImageSharp.Color.White,
                               new PointF(0, 0));
                });
                // return image as a stream for easy sending
                return(img.ToStream(format), format.FileExtensions.FirstOrDefault() ?? "png");
            }
        }
示例#2
0
        private void DrawBoundingBox(Image image, AABBBoundingBoxItemWithText boundingBoxWithText)
        {
            const int boundingBoxThickness = 5;
            Random    rand            = new Random();
            IPen      pen             = Pens.Solid(Color.FromRgb((byte)rand.Next(byte.MinValue, byte.MaxValue), (byte)rand.Next(byte.MinValue, byte.MaxValue), (byte)rand.Next(byte.MinValue, byte.MaxValue)), boundingBoxThickness);
            IPath     boundingBoxPath = boundingBoxWithText.BoundingBox.AsRectangularPolygon();

            image.Mutate(context =>
            {
                context.Draw(pen, boundingBoxPath);
                context.DrawText(boundingBoxWithText.Text, _textFont, Color.Black, new PointF(boundingBoxWithText.BoundingBox.Min.X, boundingBoxWithText.BoundingBox.Min.Y - (boundingBoxThickness * 3)));
            });
        }
示例#3
0
        public static Rgba32 HexToColor(string hexString)
        {
            if (hexString.ToLower() == "transparent")
            {
                return(Color.Transparent);
            }

            try
            {
                var drawingColor = System.Drawing.ColorTranslator.FromHtml("#" + hexString);
                return(Color.FromRgb(drawingColor.R, drawingColor.G, drawingColor.B));
            }
            catch (Exception ex)
            {
            }

            return(Color.FromRgb(254, 254, 254));
        }
示例#4
0
        // 이미지를 팔레트화시킨다.
        // 팔레트에는 반드시 검은색과 흰색은 빠지도록 한다.
        static string ExecuteQuantize(string sourceFileName, int maxColor = 30)
        {
            Logger.WriteLine($"Running {nameof(ExecuteQuantize)}");

            var targetFileName = AppendToFileName(sourceFileName, "-Q");

            var quOpts = new QuantizerOptions {
                MaxColors = maxColor
            };
            //var quantizer = new OctreeQuantizer(quOpts);
            var quantizer      = new WuQuantizer(quOpts);
            var config         = Configuration.Default;
            var pixelQuantizer = quantizer.CreatePixelSpecificQuantizer <Rgba32>(config);

            using (var image = Image.Load <Rgba32>(sourceFileName))
                using (var quantizedResult =
                           pixelQuantizer.BuildPaletteAndQuantizeFrame(image.Frames[0], image.Frames[0].Bounds()))
                {
                    var quantizedPalette = new List <Color>();
                    foreach (var p in quantizedResult.Palette.Span)
                    {
                        var c = new Color(p);
                        // 팔레트에 흰색과 검은색은 반드시 빠지도록 한다.
                        if (c != Color.White && c != Color.Black)
                        {
                            quantizedPalette.Add(c);
                        }
                    }

                    using (var stream = new FileStream(targetFileName, FileMode.Create))
                    {
                        var encoder = new PngEncoder
                        {
                            Quantizer = new PaletteQuantizer(quantizedPalette.ToArray()),
                            ColorType = PngColorType.Palette
                        };
                        image.SaveAsPng(stream, encoder);
                        stream.Close();
                    }

                    return(targetFileName);
                }
        }
示例#5
0
 public static void Clear(this Image <Rgba32> source, Color color)
 {
     Parallel.For(0, source.Height, (i) => { source.GetPixelRowSpan(i).Fill(color); });
 }
示例#6
0
 public static System.Drawing.Color ToSysDrawingColor(this SixLabors.ImageSharp.Color color)
 => System.Drawing.ColorTranslator.FromHtml("#" + color.ToHex().Substring(0, 6));
示例#7
0
        public byte[] CreateImage()
        {
            var buildQueue = CreateBuildQueue();

            if (buildQueue == null)
            {
                return(null);
            }

            var cropColours = new List <Rgba32>();

            if (this.CropImage)
            {
                if (this.RenderBackground)
                {
                    cropColours.Add(Color.FromRgb(142, 142, 94));
                    cropColours.Add(Color.FromRgb(152, 152, 101));
                }
                else
                {
                    cropColours.Add(HexToColor(this.RenderCanvasColour));
                }
            }

            using (var canvas = this.DrawingCanvas)
            {
                foreach (var asset in buildQueue)
                {
                    var image = SixLabors.ImageSharp.Image.Load <Rgba32>(asset.GetImagePath());

                    if (asset.Alpha != -1)
                    {
                        TintImage(image, "FFFFFF", (byte)asset.Alpha);
                    }

                    if (asset.ColourCode != null)
                    {
                        TintImage(image, asset.ColourCode, 255);
                    }

                    if (asset.Shadow)
                    {
                        image.Mutate(ctx =>
                        {
                            ctx.Opacity(0.2f);
                        });
                    }

                    var graphicsOptions = new GraphicsOptions();

                    if ((asset.Ink == "ADD" || asset.Ink == "33"))
                    {
                        graphicsOptions.ColorBlendingMode = PixelColorBlendingMode.Add;
                    }
                    else
                    {
                        graphicsOptions.ColorBlendingMode = PixelColorBlendingMode.Normal;
                    }

                    canvas.Mutate(ctx =>
                    {
                        ctx.DrawImage(image, new SixLabors.ImageSharp.Point(canvas.Width - asset.ImageX, canvas.Height - asset.ImageY), graphicsOptions);
                    });
                }


                using (Bitmap tempBitmap = canvas.ToBitmap())
                {
                    if (CropImage && cropColours.Count > 0)
                    {
                        var temp = canvas.ToBitmap();

                        // Crop the image
                        using (Bitmap croppedBitmap = ImageUtil.TrimBitmap(tempBitmap, cropColours.ToArray()))
                        {
                            return(RenderImage(croppedBitmap));
                        }
                    }
                    else
                    {
                        return(RenderImage(tempBitmap));
                    }
                }
            }
        }