Exemplo n.º 1
0
        /// <summary>
        /// Draw the text onto a new bitmap at the specified point, with the specified font and text colours
        /// </summary>
        /// <param name="text">The text to be drawn</param>
        /// <param name="font">The font to use</param>
        /// <param name="textColor">The colour of the text</param>
        /// <param name="backgroundColor">The colour of the background</param>
        /// <param name="offset">The amount by which to offset the text</param>
        /// <returns>A new bitmap containing the text</returns>
        public static Bitmap Convert(string text, Font font, Color textColor, Color backgroundColor, Point offset)
        {
            Size size = FontFunctions.MeasureText(text, font);

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

            using (Graphics g = Graphics.FromImage(result))
            {
                g.Clear(backgroundColor);

                TextRenderer.DrawText(
                    g,
                    text,
                    font,
                    offset,
                    textColor,
                    backgroundColor,
                    TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix);
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Draw the text onto a new bitmap with the specified font and colours
        /// </summary>
        /// <param name="text">The text to be drawn</param>
        /// <param name="font">Font to use</param>
        /// <param name="textColors">2d array of colours to use (must have one for each character)</param>
        /// <param name="backgroundColor">Color to use behind the text</param>
        /// <param name="scale">Percentage scale of the image, 1.0-100.0</param>
        /// <returns>Image containing the coloured text</returns>
        public static Image Convert(string[] text, Font font, Color[][] textColors, Color backgroundColor, float scale)
        {
            if (!FontFunctions.IsFixedWidth(font) || text == null || textColors == null)
            {
                return(null);
            }

            // size the output image must be to fit the text
            Size size = FontFunctions.MeasureText(FontFunctions.StringArrayToString(text), font);

            // size of one character in the font
            Size characterSize = FontFunctions.GetFixedPitchFontSize(font);

            Bitmap fullSize = new Bitmap(size.Width, size.Height);

            using (Graphics g = Graphics.FromImage(fullSize))
            {
                g.Clear(backgroundColor);

                int width  = textColors[0].Length;
                int height = textColors.Length;

                for (int y = 0; y < height; y++)
                {
                    string line = text[y];

                    for (int x = 0; x < width; x++)
                    {
                        if (textColors[y][x] == backgroundColor)
                        {
                            continue;
                        }

                        Point offset = new Point(x * characterSize.Width, y * characterSize.Height);

                        using (SolidBrush brush = new SolidBrush(textColors[y][x]))
                        {
                            g.DrawString(
                                line[x].ToString(),
                                font,
                                brush,
                                offset,
                                StringFormat.GenericTypographic);
                        }
                    }
                }
            }

            if (scale == 100f)
            {
                return(fullSize);
            }

            float magnification = scale / 100f;

            Size newSize = new Size(
                (int)((fullSize.Width * magnification) + 0.5),
                (int)((fullSize.Height * magnification) + 0.5));

            newSize.Width  = Math.Max(newSize.Width, 1);
            newSize.Height = Math.Max(newSize.Height, 1);

            Bitmap resized = new Bitmap(newSize.Width, newSize.Height);

            using (Graphics g = Graphics.FromImage(resized))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                g.DrawImage(fullSize, new Rectangle(0, 0, newSize.Width, newSize.Height));
            }

            fullSize.Dispose();

            return(resized);
        }