示例#1
0
        private static void drawString(
            string text,
            TextToImageOptions options,
            Font font,
            RectangleF rectangle,
            Graphics graphics,
            SolidBrush fgBrush)
        {
            using var format = new StringFormat
                  {
                      FormatFlags = StringFormatFlags.NoWrap,
                      Alignment   = StringAlignment.Center
                  };

            if (options.DropShadowLevel > 0)
            {
                using var brush = new SolidBrush(options.ShadowColor);
                switch (options.DropShadowLevel)
                {
                case 1:
                    rectangle.Offset(-1, -1);
                    graphics.DrawString(text, font, brush, rectangle, format);
                    rectangle.Offset(+1, +1);
                    break;

                case 2:
                    rectangle.Offset(+1, -1);
                    graphics.DrawString(text, font, brush, rectangle, format);
                    rectangle.Offset(-1, +1);
                    break;

                case 3:
                    rectangle.Offset(-1, +1);
                    graphics.DrawString(text, font, brush, rectangle, format);
                    rectangle.Offset(+1, -1);
                    break;

                case 4:
                    rectangle.Offset(+1, +1);
                    graphics.DrawString(text, font, brush, rectangle, format);
                    rectangle.Offset(-1, -1);
                    break;
                }
            }

            graphics.DrawString(text, font, fgBrush, rectangle, format);
        }
示例#2
0
        public static byte[] TextToImage(this string text, TextToImageOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            using var family = new FontFamily(options.FontName);
            using var font   = new Font(family, options.FontSize, options.FontStyle, GraphicsUnit.Pixel);
            var textSize = MeasureString(text, font);
            int width    = ((int)textSize.Width) + 5;
            int height   = ((int)textSize.Height) + 3;

            var rectangle = new RectangleF(0, 0, width, height);

            using var pic               = new Bitmap(width, height);
            using var graphics          = Graphics.FromImage(pic);
            graphics.SmoothingMode      = SmoothingMode.AntiAlias;
            graphics.SmoothingMode      = SmoothingMode.HighQuality;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode  = InterpolationMode.High;
            if (options.AntiAlias)
            {
                graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
            }

            using var fgBrush = new SolidBrush(options.FontColor);
            using var bgBrush = new SolidBrush(options.BgColor);
            if (options.Rectangle)
            {
                graphics.FillRectangle(bgBrush, rectangle);
            }
            else
            {
                using var brush = new SolidBrush(Color.White);
                graphics.FillRectangle(brush, rectangle);
                graphics.FillEllipse(bgBrush, rectangle);
            }

            using var pen = new Pen(Color.LightGray);
            graphics.DrawRectangle(pen, new Rectangle(0, 0, width - 1, height - 1));
            drawString(text, options, font, rectangle, graphics, fgBrush);

            using var memory = new MemoryStream();
            pic.Save(memory, ImageFormat.Png);
            return(memory.ToArray());
        }
示例#3
0
        /// <summary>
        /// Draws a text on a bitmap and then returns it as a png byte array.
        /// </summary>
        public static byte[] TextToImage(this string text, TextToImageOptions options)
        {
            using (Font font = new Font(new FontFamily(options.FontName), options.FontSize, options.FontStyle, GraphicsUnit.Pixel))
            {
                var textSize = MeasureString(text, font);
                int width    = ((int)textSize.Width) + 5;
                int height   = ((int)textSize.Height) + 3;

                var rectangle = new RectangleF(0, 0, width, height);
                using (Bitmap pic = new Bitmap(width, height))
                {
                    using (Graphics graphics = Graphics.FromImage(pic))
                    {
                        graphics.SmoothingMode      = SmoothingMode.AntiAlias;
                        graphics.SmoothingMode      = SmoothingMode.HighQuality;
                        graphics.CompositingQuality = CompositingQuality.HighQuality;
                        graphics.InterpolationMode  = InterpolationMode.High;
                        if (options.AntiAlias)
                        {
                            graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                        }

                        using (SolidBrush fgBrush = new SolidBrush(options.FontColor))
                        {
                            using (SolidBrush bgBrush = new SolidBrush(options.BgColor))
                            {
                                if (options.Rectangle)
                                {
                                    graphics.FillRectangle(bgBrush, rectangle);
                                }
                                else
                                {
                                    graphics.FillRectangle(new SolidBrush(Color.White), rectangle);
                                    graphics.FillEllipse(bgBrush, rectangle);
                                }

                                graphics.DrawRectangle(new Pen(Color.LightGray), new Rectangle(0, 0, width - 1, height - 1));

                                using (StringFormat format = new StringFormat())
                                {
                                    format.FormatFlags = StringFormatFlags.NoWrap;
                                    format.Alignment   = StringAlignment.Center;

                                    if (options.DropShadowLevel > 0)
                                    {
                                        switch (options.DropShadowLevel)
                                        {
                                        case 1:
                                            rectangle.Offset(-1, -1);
                                            graphics.DrawString(text, font, new SolidBrush(options.ShadowColor), rectangle,
                                                                format);
                                            rectangle.Offset(+1, +1);
                                            break;

                                        case 2:
                                            rectangle.Offset(+1, -1);
                                            graphics.DrawString(text, font, new SolidBrush(options.ShadowColor), rectangle,
                                                                format);
                                            rectangle.Offset(-1, +1);
                                            break;

                                        case 3:
                                            rectangle.Offset(-1, +1);
                                            graphics.DrawString(text, font, new SolidBrush(options.ShadowColor), rectangle,
                                                                format);
                                            rectangle.Offset(+1, -1);
                                            break;

                                        case 4:
                                            rectangle.Offset(+1, +1);
                                            graphics.DrawString(text, font, new SolidBrush(options.ShadowColor), rectangle,
                                                                format);
                                            rectangle.Offset(-1, -1);
                                            break;
                                        }
                                    }

                                    graphics.DrawString(text, font, fgBrush, rectangle, format);

                                    using (var memory = new MemoryStream())
                                    {
                                        pic.Save(memory, ImageFormat.Png);
                                        return(memory.ToArray());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
 /// <summary>
 /// An ASP.NET Core text to image renderer.
 /// </summary>
 public TextToImageResult(string text, TextToImageOptions options)
 {
     _options = options;
     _text    = text;
 }