Beispiel #1
0
        private static Image RenderOne(string hexDigest, int width, int height)
        {
            var robo = RoboHash.Create(hexDigest);

            return(robo.Render(width, height));
        }
Beispiel #2
0
        public Image Render(string set, string backgroundSet, string color, int width, int height, Options options = Options.None)
        {
            Image image1 = null, image2 = null, image3 = null;

            try
            {
                var facHeight2 = (int)(height * 0.85);
                var facWidth2  = (int)(width * 0.85);
                var facHeight3 = (int)(height * 0.90);
                var facWidth3  = (int)(width * 0.90);


                image1 = RenderOne(_hexDigest1, width, height);
                image2 = RenderOne(_hexDigest2, facWidth2, facHeight2);
                image3 = RenderOne(_hexDigest3, facWidth3, facHeight3);

                var robo                = RoboHash.Create(Xor(Xor(_hexDigest1, _hexDigest2), _hexDigest3));
                var backgroundColor     = backgroundSet != null && backgroundSet.StartsWith("#") ? backgroundSet : null;
                var backgroundImageName = backgroundColor == null?robo.GetBackgroundImageFileName(backgroundSet) : null;

                var retval = new Bitmap(width, height);
                try
                {
                    using (var canvas = Graphics.FromImage(retval))
                    {
                        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        if (backgroundColor != null)
                        {
                            using (var brush = new SolidBrush(RoboHelper.ConvertHexColor(backgroundColor)))
                                canvas.FillRectangle(brush, 0, 0, width, height);
                        }
                        else if (!string.IsNullOrWhiteSpace(backgroundImageName))
                        {
                            using (var image = Image.FromFile(backgroundImageName))
                            {
                                canvas.DrawImage(image, new Rectangle(0, 0, width, height),
                                                 new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                            }
                        }

                        var x = (width - width) / 2;
                        var y = (height - height) / 2;

                        var left  = (int)(width / 4.0 - width * 0.05);
                        var right = (int)(width / 4.0 + width * 0.1);

                        var offHeight2 = height - facHeight2;
                        var offHeight3 = height - facHeight3;

                        canvas.DrawImage(image2, new Rectangle(x - left, y + offHeight2, image2.Width, image2.Height),
                                         new Rectangle(0, 0, image2.Width, image2.Height), GraphicsUnit.Pixel);

                        canvas.DrawImage(image3, new Rectangle(x + right, y + offHeight3, image3.Width, image3.Height),
                                         new Rectangle(0, 0, image3.Width, image3.Height), GraphicsUnit.Pixel);

                        canvas.DrawImage(image1, new Rectangle(x, y, image1.Width, image1.Height),
                                         new Rectangle(0, 0, image1.Width, image1.Height), GraphicsUnit.Pixel);
                    }


                    if (options.HasFlag(Options.Grayscale))
                    {
                        RoboHelper.MakeBlackAndWhite(ref retval);
                    }
                    if (options.HasFlag(Options.Blur))
                    {
                        RoboHelper.Blur(ref retval, 5);
                    }
                }
                catch
                {
                    retval.Dispose();
                    retval = null;
                }
                return(retval);
            }
            finally
            {
                if (image1 != null)
                {
                    image1.Dispose();
                }
                if (image2 != null)
                {
                    image2.Dispose();
                }
                if (image3 != null)
                {
                    image3.Dispose();
                }
            }
        }