I draw a string into a larger graphic in C# using DrawString:
g.DrawString("Re", new Font("PT Sans Narrow", 35, FontStyle.Bold), Brushes.Black, new Point(5, 0));
Now I want to draw the text in Small Caps as it is possible in many word processors or graphic tools ... and I don't mean the faked small caps like "Draw 1st letter larger than rest".
Is this possible in C# / .NET?
Why not just render capitalized string?
If you need strings perfectly fit to echa other, you can measure size of rendered string, like this:
string s1 = "Simple text";
string s2 = "Capitalized text";
Font font1 = new Font("PT Sans Narrow", 35, FontStyle.Bold);
Font font2 = new Font("PT Sans Narrow", 25, FontStyle.Bold);
SizeF size1 = g.MeasureString(s1, font1);
SizeF size2 = g.MeasureString(s2, font2);
Point point1 = new Point(5, 0);
Point point2 = new Point(point1.X + size1.Width, point1.Y + size1.Height - size2.Height);
g.DrawString(s1, font1, Brushes.Black, point1);
g.DrawString(s2.ToUpper(), font2, Brushes.Black, point2);
font1.Dispose();
font2.Dispose();
How about something like:
string output = "Re";
g.DrawString(output.ToLower(), new Font("PT Sans Narrow", 35, FontStyle.Bold), Brushes.Black, new Point(5, 0));
Related
I am using the graphics.MeasureString method to measure the text size. And based on the measured text size, I was the draw the string using the graphics.MeasureString. But in that I was using the StringFormat to measure and draw the string. But I found the text clipping problem in some text like "left".
Please find the code snippet below,
string text = "Left";
Font font = new System.Drawing.Font("Segoe UI Semibold", 9F);
StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
SizeF size = e.Graphics.MeasureString(text, font, 100, format);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new RectangleF(10, 10, size.Width, size.Height), format);
Please find the text clipping while drawing in below screen shot,
Can you please suggest how to solve this issue?
Try the following. I have removed the StringFormat from the code. It works.
string text = "Left";
Font font = new Font("Segoe UI Semibold", 9F);
//StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
SizeF size = e.Graphics.MeasureString(text, font, 100);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new RectangleF(10, 10, size.Width, size.Height));
Edit1
I have modified the answer as per OP's request to use StringFormat enum.
string text = "Left";
Font font = new Font("Segoe UI Semibold", 9F);
StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
SizeF size = e.Graphics.MeasureString(text, font, 100, format);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new RectangleF(10, 10, size.Width + 5, size.Height), format);
I am Working with Visual C#. When overlay a text on the Picture, How can I set the font color?
graphicsImage.DrawString(textBox1.Text,
new Font("Arial", 12, FontStyle.Bold)
SystemBrushes.WindowText, new Point(10, 210));
try to use SolidBrush like this,you will get red font:
graphicsImage.DrawString(textBox1.Text, new Font("Arial", 12, FontStyle.Bold), new SolidBrush(Color.Red), new Point(10, 210));
There's no Font Color property, instead you should use the right Brush.
Do not forget disposing IDisposable, in your case:
using (Brush brush = new SolidBrush(Color.Red)) { // <- Let your text be red
using (Font font = new Font("Arial", 12, FontStyle.Bold)) {
// Paint the text with selected
// font - Arial 12 Bold
// brush - solid red
graphicsImage.Graphics.DrawString(
textBox1.Text, // <- what (text)
font, // <- font
brush, // <- color
new Point(10, 210)); // <- where
}
}
I want to align the dynamic text on my progress bar.
Here is the code
using (var sf = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
}
)
progressBar1.CreateGraphics()
.DrawString(message, new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new Rectangle(0, 0, 600, 480), sf);
The values 600,480 is the size of my application.
My progress bar location is 0,430
Progress bar dimensions are 600,11
Can anyone please help me?
using (var sf = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
})
{
progressBar1.CreateGraphics()
.DrawString(message,
new Font("Arial",
(float) 20, FontStyle.Regular),
Brushes.Black,
new Rectangle(0, 0, 600, 480), sf);
}
Use this to measure the size of the string: Graphics.MeasureString
Then use this simple formula:
containerWidth // width in pixels of the container
textWidth // width of your string in pixels
margin = (containerWidth - textWidth) / 2;
You must offset the text from the left by margin pixels.
Code below is used to print order.
Order can have variable number of lines written out by first DrawString call.
Text
Bottom line1
Bottom line2
must appear in bottom left corner in page.
Code below uses hard coded value e.MarginBounds.Top + 460 to print this.
How to remove this hard-coded value so that text is printed in bottom of page ?
var doc = new PrintDocument();
doc.PrinterSettings.PrinterName = "PDFCreator";
doc.PrintPage += new PrintPageEventHandler(ProvideContent);
doc.Print();
void ProvideContent(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("string containing variable number of lines", new Font("Arial", 12),
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top);
e.Graphics.DrawString("Bottom line1\r\nBottom line2", new Font("Courier", 10), Brushes.Black,
e.MarginBounds.Left, e.MarginBounds.Top + 460);
}
Measure your string, then move up that height from the bottom?
void ProvideContent(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("string containing variable number of lines", new Font("Arial", 12),
Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top);
string bottom = "Bottom line1\r\nBottom line2";
Font courier = new Font("Courier", 10);
Size sz = TextRenderer.MeasureText(bottom, courier);
e.Graphics.DrawString(bottom, courier, Brushes.Black,
e.MarginBounds.Left, e.MarginBounds.Bottom - sz.Height);
}
This is the output i get when i use DrawString.
I=Smith,John II=Johnson,Mark III=Anderson,James IV=William,Craig
V=Ford,He...
page is a float datatype which value is based on e.PageSettings.Margins.Left;
e.Graphics.DrawString(Text, new System.Drawing.Font("Arial", 8F, FontStyle.Regular), Brushes.Black, page, 30);
In the above example, it is
e.Graphics.DrawString(Text, new System.Drawing.Font("Arial", 8F, FontStyle.Regular), Brushes.Black, page, 30);
I tried using this
StringFormat format = new StringFormat();
format.FormatFlags = StringFormatFlags.FitBlackBox;
e.Graphics.DrawString(Text, new System.Drawing.Font("Arial", 8F, FontStyle.Regular), Brushes.Black, page, 30, format);
How do i expand/word wrap so that i can have the entire words instead of '...' at the end?
I=Smith,John II=Johnson,Mark III=Anderson,James IV=William,Craig
V=Ford,Henry
You can "word wrap" the text by using a bounding rectangle.
Use Graphics.DrawString Method (String, Font, Brush, RectangleF, StringFormat)
The RectangleF specifies the draw area and it will automatically "wrap" your text for you.