public IdenticonGenerator(string algorithm, Size size, Color defaultBackgroundColor, Size defaultBlocks, Encoding encoding, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     DefaultAlgorithm       = algorithm;
     DefaultSize            = size;
     DefaultBackgroundColor = defaultBackgroundColor;
     DefaultBlocks          = defaultBlocks;
     DefaultEncoding        = encoding;
     DefaultBlockGenerators = blockGenerators;
     DefaultBrushGenerator  = brushGenerator;
 }
 public IdenticonGenerator(string algorithm, Size size, Color defaultBackgroundColor, Size defaultBlocks, Encoding encoding, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     this.DefaultAlgorithm = algorithm;
     this.DefaultSize = size;
     this.DefaultBackgroundColor = defaultBackgroundColor;
     this.DefaultBlocks = defaultBlocks;
     this.DefaultEncoding = encoding;
     this.DefaultBlockGenerators = blockGenerators;
     this.DefaultBrushGenerator = brushGenerator;
 }
 public IdenticonGenerator WithBrushGenerator(IBrushGenerator brushGenerator)
 {
     DefaultBrushGenerator = brushGenerator;
     return(this);
 }
        public Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator, string algorithm)
        {
            var ha = (HashAlgorithm)CryptoConfig.CreateFromName(algorithm);

            if (ha == null)
            {
                throw new ArgumentOutOfRangeException(string.Format("Unknown algorithm '{0}'", algorithm));
            }

            if (blocks.Width < 1)
            {
                throw new ArgumentOutOfRangeException("blockshorizontal");
            }
            if (blocks.Height < 1)
            {
                throw new ArgumentOutOfRangeException("blocksvertical");
            }

            if (size.Width <= 0)
            {
                throw new ArgumentOutOfRangeException("width");
            }
            if (size.Height <= 0)
            {
                throw new ArgumentOutOfRangeException("height");
            }

            if (blockGenerators.Length == 0)
            {
                throw new ArgumentException("blockgenerators");
            }

            if (blockGenerators.Any(b => b == null))
            {
                throw new ArgumentNullException("blockgenerators");
            }

            size.Width  -= size.Width % blocks.Width;
            size.Height -= size.Height % blocks.Height;

            if (size.Width <= 0)
            {
                throw new ArgumentOutOfRangeException("width after rounding to nearest value");
            }
            if (size.Height <= 0)
            {
                throw new ArgumentOutOfRangeException("height after rounding to nearest value");
            }

            bool hasunevencols = blocks.Width % 2 != 0;
            var  allblockgens  = blockGenerators.ToArray();
            var  symblockgens  = blockGenerators.Where(sbg => sbg.IsSymmetric).ToArray();

            if (hasunevencols && symblockgens.Length == 0)
            {
                throw new Exception("At least one symmetrical blockgenerator required for identicons with uneven number of horizontal blocks");
            }

            ha.Initialize();
            var hash        = ha.ComputeHash(value);
            var blockwidth  = (int)Math.Ceiling((double)size.Width / blocks.Width);
            var blockheight = (int)Math.Ceiling((double)size.Height / blocks.Height);


            var result = new Bitmap(size.Width, size.Height);

            using (var bgbrush = new SolidBrush(backgroundcolor))
                using (var gfx = Graphics.FromImage(result))
                {
                    gfx.FillRectangle(bgbrush, 0, 0, size.Width, size.Height);

                    var dhash     = hash.Concat(hash).ToArray();
                    int hashlen   = hash.Length;
                    int i         = 0;
                    int halfwidth = blocks.Width / 2;
                    for (var x = 0; x < (hasunevencols ? halfwidth + 1 : halfwidth); x++)
                    {
                        for (var y = 0; y < blocks.Height; y++, i++)
                        {
                            var blockgen = GetBlockGenerator((x == halfwidth && hasunevencols) ? symblockgens : allblockgens, hash[i % hashlen]);
                            var seed     = BitConverter.ToUInt32(dhash, i % hashlen);

                            using (var fgbrush = brushGenerator.GetBrush(seed))
                            {
                                Rectangle rl = new Rectangle(x * blockwidth, y * blockheight, blockwidth, blockheight);
                                blockgen.Draw(gfx, rl, bgbrush, fgbrush, seed, false);

                                if ((x != halfwidth) || ((x == halfwidth) && !hasunevencols))
                                {
                                    Rectangle rr = new Rectangle((size.Width - blockwidth) - (x * blockwidth), y * blockheight, blockwidth, blockheight);
                                    blockgen.Draw(gfx, rr, bgbrush, fgbrush, seed, true);
                                }
                            }
                        }
                    }
                }
            return(result);
        }
 public Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     return(Create(value, size, backgroundcolor, blocks, blockGenerators, brushGenerator, DefaultAlgorithm));
 }
        public Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator, string algorithm)
        {
            if (ipaddress == null)
            {
                throw new ArgumentNullException("ipaddress");
            }

            return(Create(ipaddress.GetAddressBytes(), size, backgroundcolor, blocks, blockGenerators, brushGenerator, algorithm));
        }
 public Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     return(Create(ipaddress, size, backgroundcolor, blocks, blockGenerators, brushGenerator, DefaultAlgorithm));
 }
        public Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks, Encoding encoding, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator, string algorithm)
        {
            if (DefaultEncoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            return(Create(encoding.GetBytes(value ?? string.Empty), size, backgroundcolor, blocks, blockGenerators, brushGenerator));
        }
 public IdenticonGenerator WithBrushGenerator(IBrushGenerator brushGenerator)
 {
     this.DefaultBrushGenerator = brushGenerator;
     return this;
 }
 public Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     return this.Create(value, size, backgroundcolor, blocks, blockGenerators, brushGenerator, this.DefaultAlgorithm);
 }
        public Bitmap Create(byte[] value, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator, string algorithm)
        {
            var ha = HashAlgorithm.Create(algorithm);
            if (ha == null)
                throw new ArgumentOutOfRangeException(string.Format("Unknown algorithm '{0}'", algorithm));

            if (blocks.Width < 1)
                throw new ArgumentOutOfRangeException("blockshorizontal");
            if (blocks.Height < 1)
                throw new ArgumentOutOfRangeException("blocksvertical");

            if (size.Width <= 0)
                throw new ArgumentOutOfRangeException("width");
            if (size.Height <= 0)
                throw new ArgumentOutOfRangeException("height");

            if (blockGenerators.Length == 0)
                throw new ArgumentException("blockgenerators");

            if (blockGenerators.Any(b => b == null))
                throw new ArgumentNullException("blockgenerators");

            size.Width -= size.Width % blocks.Width;
            size.Height -= size.Height % blocks.Height;

            if (size.Width <= 0)
                throw new ArgumentOutOfRangeException("width after rounding to nearest value");
            if (size.Height <= 0)
                throw new ArgumentOutOfRangeException("height after rounding to nearest value");

            bool hasunevencols = blocks.Width % 2 != 0;
            var allblockgens = blockGenerators.ToArray();
            var symblockgens = blockGenerators.Where(sbg => sbg.IsSymmetric).ToArray();

            if (hasunevencols && symblockgens.Length==0)
                throw new Exception("At least one symmetrical blockgenerator required for identicons with uneven number of horizontal blocks");

            ha.Initialize();
            var hash = ha.ComputeHash(value);
            var blockwidth = (int)Math.Ceiling((double)size.Width / blocks.Width);
            var blockheight = (int)Math.Ceiling((double)size.Height / blocks.Height);

            var result = new Bitmap(size.Width, size.Height);
            using (var bgbrush = new SolidBrush(backgroundcolor))
            using (var gfx = Graphics.FromImage(result))
            {
                gfx.FillRectangle(bgbrush, 0, 0, size.Width, size.Height);

                var dhash = hash.Concat(hash).ToArray();
                int hashlen = hash.Length;
                int i = 0;
                int halfwidth = blocks.Width / 2;
                for (var x = 0; x < (hasunevencols ? halfwidth+1 : halfwidth); x++)
                {
                    for (var y = 0; y < blocks.Height; y++, i++)
                    {
                        var blockgen = GetBlockGenerator((x == halfwidth && hasunevencols) ? symblockgens : allblockgens, hash[i % hashlen]);
                        var seed = BitConverter.ToUInt32(dhash, i % hashlen);

                        using (var fgbrush = brushGenerator.GetBrush(seed))
                        {
                            Rectangle rl = new Rectangle(x * blockwidth, y * blockheight, blockwidth, blockheight);
                            blockgen.Draw(gfx, rl, bgbrush, fgbrush, seed, false);

                            if ((x != halfwidth) || ((x==halfwidth) && !hasunevencols))
                            {
                                Rectangle rr = new Rectangle((size.Width - blockwidth) - (x * blockwidth), y * blockheight, blockwidth, blockheight);
                                blockgen.Draw(gfx, rr, bgbrush, fgbrush, seed, true);
                            }
                        }
                    }
                }
            }
            return result;
        }
        public Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator, string algorithm)
        {
            if (ipaddress == null)
                throw new ArgumentNullException("ipaddress");

            return this.Create(ipaddress.GetAddressBytes(), size, backgroundcolor, blocks, blockGenerators, brushGenerator, algorithm);
        }
 public Bitmap Create(IPAddress ipaddress, Size size, Color backgroundcolor, Size blocks, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     return this.Create(ipaddress, size, backgroundcolor, blocks, blockGenerators, brushGenerator, this.DefaultAlgorithm);
 }
        public Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks, Encoding encoding, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator, string algorithm)
        {
            if (this.DefaultEncoding == null)
                throw new ArgumentNullException("encoding");

            return this.Create(encoding.GetBytes(value ?? string.Empty), size, backgroundcolor, blocks, blockGenerators, brushGenerator);
        }
 public Bitmap Create(string value, Size size, Color backgroundcolor, Size blocks, Encoding encoding, IBlockGenerator[] blockGenerators, IBrushGenerator brushGenerator)
 {
     return(this.Create(value, size, backgroundcolor, blocks, encoding, blockGenerators, brushGenerator, this.DefaultAlgorithm));
 }
Beispiel #16
0
 public Drawer(Config config, IBrushGenerator brushGenerator)
 {
     this.config = config;
     this.brushGenerator = brushGenerator;
 }