Graphics.DrawString is a method in the .Net Framework's System.Drawing namespace, by which code can draw text onto an image in .Net code. This is mainly used in ASP.Net, as TextRenderer.DrawText (from System.Windows.Forms) provides more reliable functionality in Windows Forms applications.
Questions tagged [drawstring]
313 questions
47
votes
5 answers
How to Change Font Size in drawString Java
How to make the font size bigger in g.drawString("Hello World",10,10); ?

zbz.lvlv
- 3,597
- 6
- 34
- 38
34
votes
4 answers
Bad text rendering using DrawString on top of transparent pixels
When rendering text into a bitmap, I find that text looks very bad when rendered on top of an area with non-opaque alpha. The problem is progressively worse as the underlying pixels become more transparent. If I had to guess I'd say that when…

mackenir
- 10,801
- 16
- 68
- 100
20
votes
2 answers
Using custom TTF font for DrawString image rendering
I am using GDI+ on the server-side to create an image which is streamed to the user's browser. None of the standard fonts fit my requirements and so I want to load a TrueType font and use this font for drawing my strings to the graphics…

Llyle
- 5,980
- 6
- 39
- 56
18
votes
4 answers
Drawing text in .NET
I'm doing some tests about drawing text in .Net and I had the following results.
The first string is a native Label with the FlatStyle set to System
The second string is drawn using Graphics.DrawString() method
The last one is drawn using…

Daniel Peñalba
- 30,507
- 32
- 137
- 219
18
votes
5 answers
MeasureString() pads the text on the left and the right
I'm using GDI+ in C++. (This issue might exist in C# too).
I notice that whenever I call Graphics::MeasureString() or Graphics::DrawString(), the string is padded with blank space on the left and right.
For example, if I am using a Courier font,…

Tim Cooper
- 10,023
- 5
- 61
- 77
17
votes
3 answers
How to align text drawn by SpriteBatch.DrawString?
Is there an easy way to align text to the right and center (instead of default left)?

user1306322
- 8,561
- 18
- 61
- 122
16
votes
2 answers
Java - How to visually center a specific string (not just a font) in a rectangle
I am trying to visually center an arbitrary user-supplied string on a JPanel. I have read dozens of other similar questions and answers here on SO but haven't found any that directly address the problem I am having.
In the code sample below,…

MikeW
- 175
- 1
- 1
- 7
14
votes
2 answers
Monogame rendering text using Graphics.DrawString (instead of SpriteBatch.DrawString)
Is there any downside to using Graphics.DrawString to render a (rather static) bunch of text to an offscreen bitmap, convert it to a Texture2D once, and then simply call SpriteBatch.Draw, instead of using the content pipeline and rendering text…

Lou
- 4,244
- 3
- 33
- 72
13
votes
2 answers
Fill text inside rectangle
I'm using GDI+ to draw a string on a Graphics object.
I want the string to fit inside a pre-defined rectangle (without breaking any lines)
Is there's anyway of doing this besides using TextRenderer.MeasureString() in a loop until the desirable size…

Nissim
- 6,395
- 5
- 49
- 74
13
votes
2 answers
Java center text in rectangle
I use drawString() method to draw string using Graphics, but I want to center my text in a rectangle. How should I do that?

Luka Tiger
- 298
- 2
- 4
- 10
11
votes
2 answers
Why does DrawString look so crappy?
I am trying to add a text scale to a color image.
The agcScale.jpg image (below) is 2 winform labels on the top and bottom and 2 winform pictureboxes on the left and right.
The exact same code was used to produce the strings in the right and left…

jacknad
- 13,483
- 40
- 124
- 194
10
votes
3 answers
How do I make drawString() text bold?
I have a drawString() method in my paintComponent method. Is there a way to make the text drawn by the drawString() bold? Also, is there a way to make the text bigger? I would like to avoid using JLabels, unless it is absolutely necessary.

reesjones
- 704
- 3
- 9
- 28
8
votes
5 answers
How does Affine Transform really work in Java?
I have been using Affine Transform to rotate a String in my java project, and I am not an experienced programmer yet, so it has taking me a long time to do a seemingly small task.. To rotate a string.
Now I have finally gotten it to work more or…

Simon Langhoff
- 1,395
- 3
- 18
- 28
8
votes
2 answers
Rotated text align in C#
I need to be able to rotate text in a label and align it to the left, right or center. So far I am able to do rotation with this code in the derived label's onPaint method:
float width = graphics.MeasureString(Text, this.Font).Width;
float height…

Adrian Serafin
- 7,665
- 5
- 46
- 67
8
votes
5 answers
Graphics.MeasureCharacterRanges giving wrong size calculations
I'm trying to render some text into a specific part of an image in a Web Forms app. The text will be user entered, so I want to vary the font size to make sure it fits within the bounding box.
I have code that was doing this fine on my…

Owen Blacker
- 4,117
- 2
- 33
- 70