-1

CodeA:

Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight);

StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;

Graphics graphics = Graphics.FromImage(imageChipsetName);
graphics.DrawString(stringA + "\n",
                    new Font("Tahoma", 14, FontStyle.Underline), Brushes.Black,
                    new RectangleF(0, 0, photoWidth, photoHeight), strFormat);
graphics.DrawString( stringB,
                    new Font("Tahoma", 14), Brushes.Black,
                    new RectangleF(0, 0, photoWidth, photoHeight), strFormat);

CodeB:

Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight);

StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;

Graphics graphics = Graphics.FromImage(imageChipsetName);
graphics.DrawString(stringA + "\n"+stringB,
                    new Font("Tahoma", 14, FontStyle.Underline), Brushes.Black,
                    new RectangleF(0, 0, photoWidth, photoHeight), strFormat);

I need to draw 2 string within a box. StringA with underline style while StringB don't.

CodeB almost achieve what I want but stringA and stringB sharing the same style. So I tested with CodeA but the program with it is that both string overlapping each other. May I know

SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
  • `DrawString()` will only handle line-breaks in a single call. It carries no state from one call to the next. So if you have a need to draw with different styles on multiple lines, you need to specify the location to draw the string correctly for each call to `DrawString()`. See marked duplicate for relevant details and links to additional information. – Peter Duniho Nov 01 '16 at 05:40

1 Answers1

0

The problem with codeA is that both stringA and stringB are drawn at the exact same position.

graphics.DrawString turns string into image and print it on the paper. "\n" doesn't have any meaning once the string is turned into image. It will not be printed nor will it create a new line. In fact, there is no "lines" on the paper. Just image.

You need to give stringB different position. Use Graphics.MeasureString (String, Font) to measure the size of stringA, then adjust stringB's position according to the result.

Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight);

StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
Font strFontA = new Font("Tahoma", 14, FontStyle.Underline);//Font used by stringA


Graphics graphics = Graphics.FromImage(imageChipsetName);
graphics.DrawString(stringA + "\n",
                    strFont_A, Brushes.Black,
                    new RectangleF(0, 0, photoWidth, photoHeight), strFormat);

SizeF stringSizeA = new SizeF();
stringSizeA = Graphics.MeasureString(stringA, strFont_A);//Measuring the size of stringA

graphics.DrawString(stringB,
                    new Font("Tahoma", 14), Brushes.Black,
                    new RectangleF(0, stringSizeA.Height, photoWidth, photoHeight - stringSizeA.Height), strFormat);
Jonathan Tsai
  • 224
  • 1
  • 7