예제 #1
0
        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;
        }
예제 #2
0
        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);
        }
예제 #3
0
파일: Drawer.cs 프로젝트: makarov906/di
        public void Draw(Dictionary<string, Rectangle> cloud)
        {
            var bitmap = new Bitmap(config.ImageSize.Width, config.ImageSize.Height);
            var g = Graphics.FromImage(bitmap);

            foreach (var tag in cloud)
            {
                g.DrawString(tag.Key, new Font(config.TagFontName, tag.Value.Height / 2), brushGenerator.GetBrush(),
                    tag.Value.Location);
            }

            bitmap.Save(config.OutputFileName, ImageFormat.Jpeg);
        }