Using GDI To Draw Outlined Text

by Bar Zohan 5. April 2010 23:02

Here is the code for drawing outlined text over gdi supported surfaces like forms, buttons etc. In this example I'm going to draw outlined text over a bmp file and save it to the same directory as our application runs.

Start a new windows forms application and use this code inside form's load event.

BlurAmt is the parameter that determines the outline thickness of the text.

            string text = "Hi Guys";
            string fontFamily = "Tahoma";
            float fontSize = 200;
            bool fontBold = true;
            bool fontItalic = true;
            bool fontUnderline = false;
            Color fontColor = Color.White;
            Color b_Color = Color.Black;
            int BlurAmt = 30;
            SizeF size = new SizeF();
            Font font = null;
            Bitmap bitmap = new Bitmap(1, 1);
            Graphics g = Graphics.FromImage(bitmap);

            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            g.SmoothingMode = SmoothingMode.HighQuality;


            FontStyle fontStyle = FontStyle.Regular;
            if (fontBold) fontStyle |= FontStyle.Bold;
            if (fontItalic) fontStyle |= FontStyle.Italic;
            if (fontUnderline) fontStyle |= FontStyle.Underline;

            font = new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
            StringFormat sf = new StringFormat();

            sf.Alignment = StringAlignment.Near;
            sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
            sf.LineAlignment = StringAlignment.Near;
            sf.Trimming = StringTrimming.Word;
            size = g.MeasureString(text, font, new PointF(0, 0), sf);
            bitmap = new Bitmap((int)size.Width, (int)size.Height, PixelFormat.Format32bppPArgb);

            g = Graphics.FromImage(bitmap);
            g.Clear(Color.Transparent);

            sf.Trimming = StringTrimming.None;
            g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;

            GraphicsUnit gUnit = GraphicsUnit.Pixel;

            SolidBrush br = new SolidBrush(Color.White);

          
            br = new SolidBrush(fontColor);
            SolidBrush b_color = new SolidBrush(Color.White);
            b_color = new SolidBrush(b_Color);
            for (int x = 0; x <= BlurAmt; x++)
            {
                for (int y = 0; y <= BlurAmt; y++)
                {
                    g.DrawString(text, font, b_color, new Point(x, y), sf);
                }
            }
            g.DrawString(text, font, br, new Point(BlurAmt / 2, BlurAmt / 2), sf);

            MemoryStream m = new MemoryStream();
            Graphics gm = this.CreateGraphics();
            g.DrawImage(bitmap, 0, 0);
            bitmap.Save(m, ImageFormat.Png);
            bitmap.Save(Application.StartupPath + "/text.bmp");

Tags: , ,

.Net Framework | C# | GDI & Image Processing

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen