Exemple #1
0
        /// <summary>
        /// Renders the files.
        /// </summary>
        /// <param name="srcFiles">The source files.</param>
        /// <param name="backgroundColor">Color of the background.</param>
        /// <param name="srcWidth">Width of the source.</param>
        /// <param name="srcHeight">Height of the source.</param>
        /// <param name="destWidth">Width of the dest.</param>
        /// <param name="destHeight">Height of the dest.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        protected override Image RenderFiles(IEnumerable <string> srcFiles, string backgroundColor, int srcWidth, int srcHeight, int destWidth, int destHeight, Options options)
        {
            var retval = new Bitmap(srcWidth, srcHeight);

            using (var canvas = Graphics.FromImage(retval))
            {
                canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                if (!string.IsNullOrWhiteSpace(backgroundColor))
                {
                    var color = RoboHelper.ConvertHexColor(backgroundColor);
                    using (var brush = new SolidBrush(color))
                        canvas.FillRectangle(brush, 0, 0, srcWidth, srcHeight);
                }

                foreach (var imageFile in srcFiles)
                {
                    using (var image = Image.FromFile(imageFile))
                        canvas.DrawImage(image, new Rectangle(0, 0, srcWidth, srcHeight),
                                         new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                }
                canvas.Save();
            }

            if (srcWidth != destWidth || srcHeight != destHeight)
            {
                var resizedImage = new Bitmap(retval, destWidth, destHeight);
                retval.Dispose();

                retval = resizedImage;
            }

            if (options.HasFlag(Options.Grayscale))
            {
                RoboHelper.MakeBlackAndWhite(ref retval);
            }
            if (options.HasFlag(Options.Blur))
            {
                RoboHelper.Blur(ref retval, 5);
            }

            return(retval);
        }
Exemple #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();
                }
            }
        }