Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawstring bold and normal text

Tags:

c#

have code:

using (Graphics g = Graphics.FromImage(pictureBox1.Image))
            {

                Font drawFont = new Font("Arial", 12);
                Font drawFontBold = new Font("Arial", 12, FontStyle.Bold);
                SolidBrush drawBrush = new SolidBrush(Color.Black);
                g.DrawString("this is normal", drawFont, drawBrush, new RectangleF(350f, 250f, 647, 200));
                g.DrawString(" and this is bold text", drawFontBold, drawBrush, new RectangleF(350f, 250f, 647, 200));
            }

I need to get

this is normal and this is bold text

But I receive overlay of second text on first

like image 746
user3740317 Avatar asked Jun 14 '14 12:06

user3740317


People also ask

What are the bold text?

A set of type characters that are darker and heavier than normal. A bold font implies that each character was originally designed with a heavier appearance rather than created on the fly from a normal character. See boldface attribute.

How do I make a bold text?

Type the keyboard shortcut: CTRL+B.

How do I change the font bold in C#?

For example, to bold a label's font: label. Font = BoldFont( label. Font );

Which method draws font on the graphics window?

DrawString(String, Font, Brush, PointF) Draws the specified text string at the specified location with the specified Brush and Font objects.


2 Answers

Try this code, might do the job.!!!

using (Graphics g = Graphics.FromImage(pictureBox1.Image))
{
    Font drawFont = new Font("Arial", 12);
    Font drawFontBold = new Font("Arial", 12, FontStyle.Bold);
    SolidBrush drawBrush = new SolidBrush(Color.Black);

    // find the width of single char using selected fonts
    float CharWidth = g.MeasureString("Y", drawFont).Width;

    // draw first part of string
    g.DrawString("this is normal", drawFont, drawBrush, new RectangleF(350f, 250f, 647, 200));

    // now get the total width of words drawn using above settings
    float widthFirst = ("this is normal").Length() * CharWidth;

    // the width of first part string to start of second part to avoid overlay
    g.DrawString(" and this is bold text", drawFontBold, drawBrush, new RectangleF(350f + widthFirst, 250f, 647, 200));
}
like image 56
H. Mahida Avatar answered Sep 28 '22 04:09

H. Mahida


I would recommend to use:

    float widthFirst = g.MeasureString("this is normal", drawFont).Width;
like image 32
Yoram Snir Avatar answered Sep 28 '22 05:09

Yoram Snir