public void IdenticonGenerator_Throws_OnInvalidBlocksVertical()
        {
            var i = new IdenticonGenerator();

            i.DefaultBlocks = new Size(10, 0);
            i.Create("test");
        }
        public void IdenticonGenerator_Throws_OnInvalidBlocksHorizontal()
        {
            var i = new IdenticonGenerator();

            i.DefaultBlocks = new Size(0, 10);
            i.Create("test");
        }
        public void IdenticonGenerator_Throws_OnNoBlockGenerators()
        {
            var i = new IdenticonGenerator();

            i.DefaultBlockGenerators = new IBlockGenerator[0];
            i.Create("test");
        }
        public void IdenticonGenerator_Throws_OnNullIPAddress()
        {
            var       i = new IdenticonGenerator();
            IPAddress a = null;

            i.Create(a);
        }
        public void IdenticonGenerator_Throws_OnNullBlockGenerator()
        {
            var i = new IdenticonGenerator();

            i.DefaultBlockGenerators = IdenticonGenerator.DefaultBlockGeneratorsConfig.Concat(new IBlockGenerator[] { null }).ToArray();
            i.Create("test");
        }
Beispiel #6
0
        public async Task <IActionResult> Index(int id)
        {
            if (System.IO.File.Exists($"./data/avatars/{id}.png"))
            {
                var file = await System.IO.File.ReadAllBytesAsync($"./data/avatars/{id}.png");

                return(File(file, "image/png"));
            }

            var user = Base.UserCache[id];

            if (user is null)
            {
                return(StatusCode(404));
            }

            var generator = new IdenticonGenerator();

            await using var ms = new MemoryStream();

            generator.Create(user.Username, new Size(64, 64)).Save(ms, ImageFormat.Png);

            await System.IO.File.WriteAllBytesAsync($"./data/avatars/{id}.png", ms.ToArray());

            return(File(ms.ToArray(), "image/png"));
        }
        public void IdenticonGenerator_Throws_OnNullEncoding()
        {
            var i = new IdenticonGenerator();

            i.DefaultEncoding = null;
            i.Create("test");
        }
        public void IdenticonGenerator_DoesNotThrowOnNullString()
        {
            var    i = new IdenticonGenerator();
            string s = null;

            i.Create(s);
        }
        public void IdenticonGenerator_Throws_OnNoSymmetricBlockGeneratorsForUnevenHorizontalBlocks()
        {
            var i = new IdenticonGenerator().WithBlocks(5, 5);

            //Get all NON-symmetric blockgens
            i.DefaultBlockGenerators = IdenticonGenerator.ExtendedBlockGeneratorsConfig.Where(bg => !bg.IsSymmetric).ToArray();
            i.Create("test");
        }
Beispiel #10
0
        private void Control_Loaded(object sender, RoutedEventArgs e)
        {
            var g = new IdenticonGenerator();

            AccountIdentImage.Source =
                AppHelpers.BitmapToImageSource(g.Create(FriendlyNameTextBlock.Text,
                                                        new Size((int)AccountIdentImage.Width, (int)AccountIdentImage.Height)));
        }
Beispiel #11
0
 private IdenticonGenerator CreateGenString(string alg, string s, bool useextended, bool usestatic)
 {
     var g = new IdenticonGenerator(alg);
     if (usestatic)
         g.DefaultBrushGenerator = new StaticColorBrushGenerator(StaticColorBrushGenerator.ColorFromText(s));
     if (useextended)
         g.DefaultBlockGenerators = IdenticonGenerator.ExtendedBlockGeneratorsConfig;
     return g;
 }
Beispiel #12
0
 private IdenticonGenerator CreateGenIp(string alg, IPAddress ip, bool useextended, bool usestatic)
 {
     var g = new IdenticonGenerator(alg);
     if (usestatic)
         g.DefaultBrushGenerator = new StaticColorBrushGenerator(StaticColorBrushGenerator.ColorFromIPAddress(ip));
     if (useextended)
         g.DefaultBlockGenerators = IdenticonGenerator.ExtendedBlockGeneratorsConfig;
     return g;
 }
Beispiel #13
0
        public ActionResult Index(int dimension, string text)
        {
            text = text ?? HttpContext.Request.UserHostAddress;
            var ic = new IdenticonGenerator();

            ic.DefaultBrushGenerator = new StaticColorBrushGenerator(StaticColorBrushGenerator.ColorFromText(text));
            var bitmap = ic.Create(text, new Size(dimension, dimension));

            var ms = new MemoryStream();

            bitmap.Save(ms, ImageFormat.Png);

            return(File(ms.ToArray(), "image/png"));
        }
Beispiel #14
0
        public Bitmap GenerateIcon(string algorithmBox)
        {
            var randomValue = RandomStr();

            var bg     = _colorGenBox.Equals("Random") ? (IBrushGenerator) new RandomColorBrushGenerator() : new StaticColorBrushGenerator(StaticColorBrushGenerator.ColorFromText(randomValue));
            var result = new IdenticonGenerator(algorithmBox)
                         .WithSize(400, 400)
                         .WithBackgroundColor(Color.Black)
                         .WithBlocks(5, 5)
                         .WithBlockGenerators(IdenticonGenerator.ExtendedBlockGeneratorsConfig)
                         .WithBrushGenerator(bg)
                         .Create(randomValue);

            return(result);
        }
Beispiel #15
0
        public Bitmap GenerateIcon()
        {
            var randomValue = RandomStr();

            var bg     = _colorGenBox.Equals("Random") ? (IBrushGenerator) new RandomColorBrushGenerator() : new StaticColorBrushGenerator(StaticColorBrushGenerator.ColorFromText(randomValue));
            var result = new IdenticonGenerator(MD5)
                         .WithSize(250, 250)
                         .WithBackgroundColor(Color.Black)
                         .WithBlocks(5, 5)
                         .WithBlockGenerators(IdenticonGenerator.DefaultBlockGeneratorsConfig)
                         .WithBrushGenerator(bg)
                         .Create(randomValue);

            return(result);
        }
Beispiel #16
0
        /// <summary>
        /// Generates the Identicon image and returns it to the client.
        /// </summary>
        public Task ProcessRequest(IDotvvmRequestContext context)
        {
            // generate the identicon
            var identicon = new IdenticonGenerator("SHA512", new Size(180, 180), Color.White, new Size(8, 8))
            {
                DefaultBrushGenerator  = new StaticColorBrushGenerator(Color.FromArgb(255, 41, 128, 185)),
                DefaultBlockGenerators = IdenticonGenerator.ExtendedBlockGeneratorsConfig
            };
            var name = Convert.ToString(context.Parameters["Identicon"]);

            using (var bitmap = identicon.Create(name))
            {
                // save it in the response stream
                context.HttpContext.Response.ContentType = "image/png";
                bitmap.Save(context.HttpContext.Response.Body, ImageFormat.Png);
            }

            return(Task.FromResult(0));
        }
Beispiel #17
0
        private IdenticonGenerator GetIdenticonGenerator()
        {
            var foreground = GetRandomColor();
            var background = GetRandomColor();

            while (background == foreground)
            {
                background = GetRandomColor();
            }

            var identiconGenerator = new IdenticonGenerator()
                                     .WithSize(360, 360)
                                     .WithBlocks(8, 8)
                                     .WithBackgroundColor(background)
                                     .WithAlgorithm("MD5")
                                     .WithBrushGenerator(new StaticColorBrushGenerator(foreground))
                                     .WithBlockGenerators(IdenticonGenerator.DefaultBlockGeneratorsConfig);

            return(identiconGenerator);
        }
Beispiel #18
0
        public static byte[] Generate()
        {
            var rnd   = new Random();
            var color = Color.FromArgb(255, rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));
            var gen   = new IdenticonGenerator()
                        .WithBlocks(5, 5)
                        .WithSize(512, 512)
                        .WithBlockGenerators(IdenticonGenerator.ExtendedBlockGeneratorsConfig)
                        .WithBackgroundColor(Color.White)
                        .WithBrushGenerator(new StaticColorBrushGenerator(color));

            using (var pic = gen.Create("awooo" + DateTime.Now.Ticks))
            {
                using (var stream = new MemoryStream())
                {
                    pic.Save(stream, ImageFormat.Jpeg);
                    return(stream.ToArray());
                }
            }
        }
        public static IHtmlString GenerateIdenticon(this HtmlHelper html, string value, int dimension, bool useStaticBrush = false)
        {
            var i = new IdenticonGenerator()
                    .WithBlockGenerators(IdenticonGenerator.ExtendedBlockGeneratorsConfig)
                    .WithBrushGenerator(useStaticBrush ?
                                        (IBrushGenerator) new StaticColorBrushGenerator(StaticColorBrushGenerator.ColorFromText(value)) : new RandomColorBrushGenerator()
                                        )
                    .WithSize(dimension, dimension);

            using (var bitmap = i.Create(value))
                using (var stream = new MemoryStream())
                {
                    bitmap.Save(stream, ImageFormat.Png);

                    var img = new TagBuilder("img");
                    img.Attributes.Add("width", bitmap.Width.ToString());
                    img.Attributes.Add("height", bitmap.Height.ToString());
                    img.Attributes.Add("src", String.Format("data:image/png;base64,{0}",
                                                            Convert.ToBase64String(stream.ToArray())));

                    return(MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing)));
                }
        }
        public void IdenticonGenerator_HasDefaultsForIPAddress()
        {
            var i = new IdenticonGenerator();

            i.Create(IPAddress.Loopback);
        }
        public void IdenticonGenerator_Throws_OnInvalidHeightAfterRounding()
        {
            var i = new IdenticonGenerator();

            i.Create("test", new Size(100, 10), Color.Red, new Size(2, 12));
        }
        public void IdenticonGenerator_Throws_OnInvalidHeight()
        {
            var i = new IdenticonGenerator();

            i.Create("test", new Size(10, 0));
        }
        public void IdenticonGenerator_Throws_OnInvalidWidth()
        {
            var i = new IdenticonGenerator();

            i.Create("test", new Size(0, 10));
        }
        public void IdenticonGenerator_HasDefaults()
        {
            var i = new IdenticonGenerator();

            i.Create("test");
        }
        public void IdenticonGenerator_HasDefaultsForByteArray()
        {
            var i = new IdenticonGenerator();

            i.Create(new byte[0]);
        }
        public void IdenticonGenerator_Throws_OnInvalidAlgorithm()
        {
            var i = new IdenticonGenerator("XXX");

            i.Create("test");
        }