예제 #1
0
        public void GenerateImage(string achievementName, long gs, AchievementType type, ulong imageID)
        {
            // first determine background image path
            string backgroundImagePath;

            switch (type)
            {
            case AchievementType.XboxOneRare:
                backgroundImagePath = Configuration.PathAchievementXboxOneRareBackground;
                break;

            case AchievementType.Xbox360:
                backgroundImagePath = Configuration.PathAchievementXbox360Background;
                break;

            default:
                backgroundImagePath = Configuration.PathAchievementXboxOneBackground;
                break;
            }

            // passing the relative path was breaking it, so now just going to pass it the file stream instead
            string path = Path.Combine(Directory.GetCurrentDirectory(), backgroundImagePath);

            using (var backgroundStream = new FileStream(path, FileMode.Open))
            {
                // now do stuff with the image
                using (MagickImage image = new MagickImage(backgroundImagePath))
                {
                    MagickImage headerLayer = new MagickImage(MagickColor.FromRgba(0, 0, 0, 0), image.Width, image.Height);

                    if (type == AchievementType.XboxOne || type == AchievementType.XboxOneRare)
                    {
                        headerLayer.Settings.FontFamily    = "Segoe UI";
                        headerLayer.Settings.FontPointsize = 36;
                        headerLayer.Settings.TextGravity   = Gravity.Southwest;
                        headerLayer.Settings.FillColor     = MagickColor.FromRgb(255, 255, 255);
                    }

                    if (type == AchievementType.XboxOne || type == AchievementType.Xbox360)
                    {
                        var s = $"{gs} - {achievementName}";
                        headerLayer.Annotate(s, new MagickGeometry(225, 30, 700, 80), Gravity.West);
                    }
                    else if (type == AchievementType.XboxOneRare)
                    {
                        int    rarePercent;
                        Random r = new Random();
                        rarePercent = r.Next(1, 5);
                        headerLayer.Annotate($"Rare achievement unlocked - {rarePercent}%", new MagickGeometry(155, 5, 400, 70), Gravity.West);
                        headerLayer.Annotate($"{gs} - {achievementName}", new MagickGeometry(195, 55, 400, 70), Gravity.West);
                    }

                    image.Composite(headerLayer, CompositeOperator.Over);
                    image.Write(GenerateImagePath(imageID));
                }
            }
            // increment the generation counter
            GenerationCounter++;
        }
예제 #2
0
        public void Test_Affine()
        {
            using (MagickImage image = new MagickImage(MagickColors.White, 300, 300))
            {
                Assert.AreEqual(null, image.Settings.Affine);

                image.Annotate("Magick.NET", Gravity.Center);
                ColorAssert.AreEqual(MagickColors.White, image, 200, 200);

                image.Settings.Affine = new DrawableAffine(10, 20, 30, 40, 50, 60);
                image.Annotate("Magick.NET", Gravity.Center);
                ColorAssert.AreEqual(MagickColors.Black, image, 200, 200);
            }
        }
예제 #3
0
 public void ShouldThrowExceptionWhenTextIsNull()
 {
     using (var image = new MagickImage())
     {
         Assert.Throws <ArgumentNullException>("text", () => image.Annotate(null, Gravity.Center));
     }
 }
예제 #4
0
 public void ShouldThrowExceptionWhenTextIsNull()
 {
     using (var image = new MagickImage())
     {
         Assert.Throws <ArgumentNullException>("text", () => image.Annotate(null, new MagickGeometry(1, 2, 3, 4)));
     }
 }
예제 #5
0
 public void ShouldThrowExceptionWhenGeometryIsNull()
 {
     using (var image = new MagickImage())
     {
         Assert.Throws <ArgumentNullException>("boundingArea", () => image.Annotate("test", null));
     }
 }
예제 #6
0
 public void ShouldThrowExceptionWhenTextIsEmpty()
 {
     using (var image = new MagickImage())
     {
         Assert.Throws <ArgumentException>("text", () => image.Annotate(string.Empty, new MagickGeometry(1, 2, 3, 4), Gravity.Center));
     }
 }
예제 #7
0
        public static void GradientBackground()
        {
            using (MagickImage image = new MagickImage("gradient:blue-darkblue", 400, 175))
            {
                image.Swirl(45);
                image.Border(2);

                using (var text = new MagickImage("gradient:blue-red"))
                {
                    text.Settings.Font = "Arial";
                    //text.Morphology(MorphologyMethod.EdgeIn, Kernel.Diamond);

                    text.Settings.FontPointsize = 70;

                    //text.Tile(new MagickImage("gradient:blue-red"),CompositeOperator.Overlay);

                    text.Annotate("Gradient Fun", new MagickGeometry("+10+65"), Gravity.Center);

                    text.Write("text.png");
                    image.Composite(text, CompositeOperator.Over);
                }

                image.Write("preview.png");
            }
        }
예제 #8
0
        private static byte[] GenerateWatermarkFromText(dynamic data, ILogger log)
        {
            byte[] imageBytes;

            var imagePath = DownloadImageFromUrl(data.imageUri.Value, log);

            using (MagickImage image = new MagickImage(imagePath))
            {
                image.Settings.FillColor     = MagickColors.Transparent;
                image.Settings.StrokeColor   = MagickColors.Transparent;
                image.Settings.FontPointsize = 40;
                image.Annotate(data.watermarkText.Value, (Gravity)data.watermarkLocation.Value);
                imageBytes = image.ToByteArray();
            }
            try
            {
                if (File.Exists(imagePath))
                {
                    File.Delete(imagePath);
                }
            }
            catch { }

            return(imageBytes);
        }
예제 #9
0
 public void TextOverlay()
 {
     using (var image = new MagickImage(MagickColors.LightBlue, 400, 150))
     {
         //image.Settings.Font = @"I:\RocketLauncher\Media\Fonts\amstrad_cpc464.ttf";
         image.Settings.Page        = new MagickGeometry(400, 150);
         image.Settings.FillPattern = new MagickImage("pattern:checkerboard");
         image.Annotate("Anthony\nneh", Gravity.Center);
         image.Write($"{path}\\font_tileoverlay.png");
     }
 }
예제 #10
0
                public void ShouldUseTheSpecifiedSettings()
                {
                    using (var image = new MagickImage(MagickColors.GhostWhite, 200, 200))
                    {
                        image.Settings.FontPointsize = 30;
                        image.Settings.FillColor     = MagickColors.Orange;
                        image.Settings.StrokeColor   = MagickColors.Orange;
                        image.Annotate("Magick.NET", new MagickGeometry(75, 125, 0, 0), Gravity.Undefined, 45);

                        ColorAssert.Equal(MagickColors.GhostWhite, image, 104, 83);
                        ColorAssert.Equal(MagickColors.Orange, image, 117, 70);
                    }
                }
예제 #11
0
        public void Test_TextAntiAlias()
        {
            using (MagickImage image = new MagickImage(MagickColors.Azure, 300, 300))
            {
                Assert.AreEqual(true, image.Settings.TextAntiAlias);

                image.Settings.TextAntiAlias = false;
                image.Settings.FontPointsize = 100;
                image.Annotate("TEST", Gravity.Center);

                ColorAssert.AreEqual(MagickColors.Azure, image, 175, 112);
            }
        }
예제 #12
0
                public void ShouldUseTheSpecifiedGravity()
                {
                    using (var image = new MagickImage(MagickColors.Thistle, 200, 50))
                    {
                        image.Settings.FontPointsize = 20;
                        image.Settings.FillColor     = MagickColors.Purple;
                        image.Settings.StrokeColor   = MagickColors.Purple;
                        image.Annotate("Magick.NET", Gravity.East);

                        ColorAssert.Equal(MagickColors.Purple, image, 196, 17);
                        ColorAssert.Equal(MagickColors.Thistle, image, 173, 17);
                    }
                }
            public void ShouldDisableTextAntialiasingWhenFalse()
            {
                using (IMagickImage image = new MagickImage(MagickColors.Azure, 300, 300))
                {
                    Assert.IsTrue(image.Settings.TextAntiAlias);

                    image.Settings.TextAntiAlias = false;
                    image.Settings.FontPointsize = 100;
                    image.Annotate("TEST", Gravity.Center);

                    ColorAssert.AreEqual(MagickColors.Azure, image, 156, 119);
                    ColorAssert.AreEqual(MagickColors.Azure, image, 173, 119);
                }
            }
            public void ShouldDisableTextAntialiasingWhenFalse()
            {
                using (var image = new MagickImage(MagickColors.Azure, 300, 300))
                {
                    Assert.True(image.Settings.TextAntiAlias);

                    image.Settings.TextAntiAlias = false;
                    image.Settings.FontPointsize = 100;
                    image.Annotate("TEST", Gravity.Center);

                    ColorAssert.Equal(MagickColors.Azure, image, 158, 125);
                    ColorAssert.Equal(MagickColors.Black, image, 158, 126);
                    ColorAssert.Equal(MagickColors.Azure, image, 209, 127);
                    ColorAssert.Equal(MagickColors.Black, image, 209, 128);
                }
            }
예제 #15
0
        string RenderImage(string message)
        {
            Directory.CreateDirectory("WFBotImageCaches");
            var path   = Path.Combine("WFBotImageCaches", $"{Guid.NewGuid():D}.png");
            var metric = testImage.FontTypeMetrics(message, false);
            var margin = (int)(metric.TextHeight / 10.0);

            using var image = new MagickImage(new MagickColor(42, 43, 48), (int)metric.TextWidth /*+ margin*2*/, (int)metric.TextHeight /*+margin*2*/);
            image.Settings.TextAntiAlias = true;
            image.Settings.Density       = new Density(72);
            image.Settings.FontPointsize = 36;
            image.Settings.Font          = "msyh.ttf";
            image.Settings.StrokeColor   = new MagickColor(255, 255, 255);
            image.Settings.FillColor     = new MagickColor(255, 255, 255);

            image.Annotate(message, new MagickGeometry(margin, (int)Math.Ceiling((margin + metric.TextHeight) / 2.0)), Gravity.Northwest);
            image.Write(path);
            return(path);
        }
예제 #16
0
        public string ExecuteThread(FileItem item, string infile, string dest, ValuePairEnumerator configData)
        {
            var conf = new OverlayTransformViewModel(configData);

            using (MagickImage image = new MagickImage(infile))
            {
                Gravity gravity = Gravity.Center;
                if (conf.A11)
                {
                    gravity = Gravity.Northwest;
                }
                if (conf.A12)
                {
                    gravity = Gravity.North;
                }
                if (conf.A13)
                {
                    gravity = Gravity.Northeast;
                }
                if (conf.A21)
                {
                    gravity = Gravity.West;
                }
                if (conf.A22)
                {
                    gravity = Gravity.Center;
                }
                if (conf.A23)
                {
                    gravity = Gravity.East;
                }
                if (conf.A31)
                {
                    gravity = Gravity.Southwest;
                }
                if (conf.A32)
                {
                    gravity = Gravity.South;
                }
                if (conf.A33)
                {
                    gravity = Gravity.Southeast;
                }


                if (File.Exists(conf.OverlayFile))
                {
                    // Read the watermark that will be put on top of the image
                    using (MagickImage watermark = new MagickImage(conf.OverlayFile))
                    {
                        if (conf.StrechOverlay)
                        {
                            watermark.Resize(image.Width, image.Height);
                        }
                        // Optionally make the watermark more transparent
                        if (conf.Transparency != 100)
                        {
                            watermark.Evaluate(Channels.Alpha, EvaluateOperator.Add,
                                               -(255 * (100 - conf.Transparency) / 100));
                        }
                        // Draw the watermark in the bottom right corner
                        image.Composite(watermark, gravity, CompositeOperator.Over);

                        //// Optionally make the watermark more transparent
                        //watermark.Evaluate(Channels.Alpha, EvaluateOperator.Divide, 4);

                        //// Or draw the watermark at a specific location
                        //image.Composite(watermark, 200, 50, CompositeOperator.Over);
                    }
                }

                string text = "";
                if (!string.IsNullOrEmpty(conf.Text))
                {
                    Regex           regPattern = new Regex(@"\[(.*?)\]", RegexOptions.Singleline);
                    MatchCollection matchX     = regPattern.Matches(conf.Text);
                    text = matchX.Cast <Match>()
                           .Aggregate(conf.Text,
                                      (current1, match) =>
                                      item.FileNameTemplates.Where(
                                          template =>
                                          String.Compare(template.Name, match.Value,
                                                         StringComparison.InvariantCultureIgnoreCase) == 0).Aggregate(current1,
                                                                                                                      (current, template) => current.Replace(match.Value, template.Value)));

                    image.Font          = conf.Font;
                    image.FontPointsize = conf.FontSize;
                    Color color = (Color)ColorConverter.ConvertFromString(conf.FontColor);
                    image.FillColor   = new MagickColor(color.R, color.G, color.B, color.A);
                    image.StrokeColor = new MagickColor(color.R, color.G, color.B, color.A);
                    image.Annotate(text, gravity);
                }
                image.Format = MagickFormat.Jpeg;
                image.Write(dest);
            }
            return(dest);
        }
예제 #17
0
        public static void AnnotateWheel(string filePath,
                                         string wheelText,
                                         string fontName                  = @"C:\Windows\Fonts\fuddle.ttf",
                                         double fontPointSize             = 36,
                                         string pattern                   = "Bricks",
                                         System.Drawing.Color fillColor   = new System.Drawing.Color(),
                                         System.Drawing.Color strokeColor = new System.Drawing.Color(),
                                         double strokeWidth               = 1.0,
                                         System.Drawing.Color shadowColor = new System.Drawing.Color(),
                                         int imageWidth                   = 400, int imageHeight = 175)
        {
            if (!IsFontInstalled(fontName))
            {
                return;
            }

            fontName = fontName.Replace(" ", "-");

            using (MagickImage image = new MagickImage(MagickColors.Transparent, imageWidth, imageHeight))
            {
                image.Settings.Font        = fontName;
                image.Settings.StrokeWidth = strokeWidth;
                image.Settings.StrokeColor = strokeColor;
                image.Settings.Density     = new Density(72, 72);

                image.Settings.FontPointsize = fontPointSize;

                var newWheelText = wheelText.Replace(" ", "\n");

                try
                {
                    TypeMetric typeMetric = image.FontTypeMetrics(newWheelText);

                    while (typeMetric.TextWidth < image.Width)
                    {
                        image.Settings.FontPointsize++;
                        typeMetric = image.FontTypeMetrics(wheelText);
                    }
                    image.Settings.FontPointsize--;

                    image.Settings.FillPattern = new MagickImage("pattern:" + pattern);

                    image.Annotate(wheelText, new MagickGeometry(imageWidth, imageHeight), Gravity.Center);

                    image.Shadow(shadowColor);

                    using (var image2 = new MagickImage(image))
                    {
                        image2.Colorize(fillColor, new Percentage(30));

                        image2.Settings.StrokeColor = strokeColor;
                        image2.Settings.StrokeWidth = strokeWidth;

                        image2.Write(filePath);
                    }
                }
                catch (MagickException)
                {
                }
            }
        }
예제 #18
0
        public void Execute()
        {
            MontageConfig montageConfig = Config.Montages;

            if (montageConfig == null)
            {
                throw new ConfigurationException("Montage action performed, but no montages are defined in the configuration.");
            }

            string font = montageConfig.Font ?? DefaultFontFamily();

            MontageSettings montageSettings = new MontageSettings()
            {
                BackgroundColor = MagickColors.None,
                Geometry        = new MagickGeometry(2, 2, 0, 0),
                TileGeometry    = new MagickGeometry(8, 0),
                Font            = font
            };

            MagickReadSettings magickSettings = new MagickReadSettings()
            {
                FontFamily      = font,
                FontPointsize   = 14,
                BackgroundColor = MagickColors.None,
                FillColor       = MagickColors.White
            };

            foreach (PatternConfig pattern in montageConfig.Patterns)
            {
                string              name         = pattern.Name;
                DataResolver        resolver     = new DataResolver(pattern.Source);
                List <ResolvedData> resolverData = resolver.Data;

                if (resolverData.Count == 0)
                {
                    throw new ConfigurationException($"The pattern for montage {name} doesn't match any files, exiting the application.");
                }

                resolverData.Sort();

                using (MagickImageCollection collection = new MagickImageCollection())
                {
                    foreach (ResolvedData resolvedFile in resolverData)
                    {
                        string filename      = resolvedFile.Name;
                        string fileShortName = Path.GetFileNameWithoutExtension(filename);

                        MagickImage magickImage = resolvedFile.ToMagickImage(magickSettings);
                        magickImage.Extent(128, 144, MagickColors.None);

                        MagickColor       color         = MagickColor.FromRgba(0, 0, 0, 88);
                        DrawableRectangle drawableRect  = new DrawableRectangle(0, 128, 144, 144);
                        DrawableFillColor drawableColor = new DrawableFillColor(color);
                        magickImage.Draw(drawableColor, drawableRect);

                        magickImage.Annotate(fileShortName, Gravity.South);

                        collection.Add(magickImage);
                        Logger.Debug("Appended montage {0} with: {1}", name, resolvedFile);
                    }

                    Logger.Debug("Finished collecting input, creating a montage of {0} images.", collection.Count);

                    using (IMagickImage montage = collection.Montage(montageSettings))
                    {
                        string filepath = Path.Combine("build", "montages", name + ".png");

                        FileInfo outputPath = new FileInfo(filepath);
                        outputPath.Directory?.Create();

                        Logger.Debug("Writing monatage with name {0} to {1}.", name, filepath);
                        montage.Write(outputPath);
                    }
                }
            }
        }
예제 #19
0
        public bool Execute(WorkFlowCommand command, Context context)
        {
            if (context?.ImageStream == null)
            {
                return(true);
            }
            if (!CheckCondition(command, context))
            {
                return(true);
            }
            context.ImageStream.Seek(0, SeekOrigin.Begin);
            MagickImage watermark = null;

            using (MagickImage image = new MagickImage(context.ImageStream))
            {
                // check the cache if the image is already is loaded
                if (context.Cache.ContainsKey(command.GetHashCode().ToString()))
                {
                    var cache = context.Cache[command.GetHashCode().ToString()];
                    if (cache.Id == command.Properties["OverlayFile"].ToString(context))
                    {
                        watermark = (MagickImage)cache.Object;
                    }
                    else
                    {
                        cache.DisposeObject();
                        context.Cache.Remove(command.GetHashCode().ToString());
                    }
                }
                if (watermark == null && File.Exists(command.Properties["OverlayFile"].ToString(context)))
                {
                    // Read the watermark that will be put on top of the image
                    watermark = new MagickImage(command.Properties["OverlayFile"].ToString(context));

                    if (command.Properties["StrechOverlay"].ToBool(context))
                    {
                        watermark.Resize(image.Width, image.Height);
                    }
                    // Optionally make the watermark more transparent
                    context.Cache.Add(command.GetHashCode().ToString(),
                                      new CacheObject()
                    {
                        Id     = command.Properties["OverlayFile"].ToString(context),
                        Object = watermark
                    });
                }

                if (watermark != null)
                {
                    if (command.Properties["Transparency"].ToInt(context) != 100)
                    {
                        watermark.Evaluate(Channels.Alpha, EvaluateOperator.Add,
                                           -(255 * (100 - command.Properties["Transparency"].ToInt(context)) / 100));
                    }
                    image.Composite(watermark,
                                    (Gravity)Enum.Parse(typeof(Gravity), command.Properties["Position"].ToString(context)),
                                    CompositeOperator.Over);
                }

                if (!string.IsNullOrEmpty(command.Properties["Text"].ToString(context)))
                {
                    image.Settings.Font          = command.Properties["TextFont"].ToString(context);
                    image.Settings.FontPointsize = command.Properties["TextPointSize"].ToInt(context);;

                    Color color = (Color)ColorConverter.ConvertFromString(command.Properties["TextFillColor"].ToString(context));
                    image.Settings.FillColor = new MagickColor(color.R, color.G, color.B, (byte)(255 * (100 - command.Properties["TextTransparency"].ToInt(context)) / 100));
                    color = (Color)ColorConverter.ConvertFromString(command.Properties["TextStrokeColor"].ToString(context));
                    image.Settings.StrokeColor = new MagickColor(color.R, color.G, color.B, (byte)(255 * (100 - command.Properties["TextTransparency"].ToInt(context)) / 100));

                    image.Annotate(command.Properties["Text"].ToString(context).Replace("\\n", "\n"),
                                   (Gravity)Enum.Parse(typeof(Gravity), command.Properties["TextPosition"].ToString(context)));
                }

                image.Write(context.ImageStream, MagickFormat.Jpg);
            }
            return(true);
        }