Reputation: 61
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
Upvotes: 5
Views: 18362
Reputation: 21
I would recommend to use:
float widthFirst = g.MeasureString("this is normal", drawFont).Width;
Upvotes: 2
Reputation: 2366
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));
}
Upvotes: 6