I want to print using Graphics.DrawString() method but I want to print that up to down.
For example I have string "Hello" and I want to be printed
H
e
l
l
o
With only one Graphics.DrawString()
Is it possible or not? because I have dynamic length of text.
There is an overload for the drawstring method that takes a stringformat. Create one of those and set its format flag to vertical.
ex.
e.Graphics.DrawString("this", new Font("Arial", 16),
Brushes.Black, new RectangleF(), new StringFormat()
{ FormatFlags = StringFormatFlags.DirectionVertical });
EDIT:
based on the information in your comment, it seems all you have to do is make your rectangle for the drawstring slightly larger than your font, and height needs to be large enough to hold your text. The text will wrap
ex:
e.Graphics.DrawString("this", new Font("Arial", 16),
Brushes.Black, new RectangleF(0,0,16,150));
you could make the rectangle the height of your form. Whats important is that the width allows for only 1 character to fit.
Here is code to show Text vertically, not rotated. In the 1st panel the text is left aligned, in the 2nd & 3rd Panel it is centered. The Text to be shown is simply the Panel.Text and the Font is the Panel's font.
The 1st solution uses only one DrawString; it simply inserts linefeeds between all characters.
The 2nd Panel looks much nicer and you can specify a positive or negative leading but the code is a bit more involved - you get what you pay for.
(Except here on SO ;-)
private void panel1_Paint(object sender, PaintEventArgs e)
{
string s = "";
foreach(char c in panel1.Text) s += c.ToString() + "\r\n";
e.Graphics.DrawString(s, panel1.Font, Brushes.Black, Point.Empty);
SizeF sf = e.Graphics.MeasureString(s, panel1.Font); //**
panel1.Size = new Size((int)sf.Width, (int)sf.Height); //**
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
float leading = -1.75f; // <-- depends on font and taste
float maxWidth = 0f; //**
Dictionary<char, Size> charSizes = new Dictionary<char, Size>();
foreach (char c in panel2.Text)
if (!charSizes.ContainsKey(c))
{
SizeF sf = e.Graphics.MeasureString(c.ToString(), panel2.Font);
charSizes.Add(c, new Size((int)sf.Width, (int)sf.Height) );
if (maxWidth < (int)sf.Width) maxWidth = (int)sf.Width; //**
}
panel2.Width = (int)(maxWidth * 2); // for panel size //**
float y = 0f;
foreach (char c in panel2.Text)
{
e.Graphics.DrawString(c.ToString(), panel2.Font, Brushes.Black,
new Point( ( panel2.Width - charSizes[c].Width) / 2, (int)y) );
y += charSizes[c].Height + leading;
}
panel2.Height = (int)y; //**
}
Edit: //** added code to resize the panels
Here is a screenshot; the middle panel has a leading of +2.75f the right one of +5f:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With