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
}
}
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 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));
Is it possible to apply a gradient to label text?
Right now I am taking over a controls OnPaint and drawing the string of text that I want; however, this is to specific. I really want to make it so that the label itself gets applied the gradient colors I want. So in turn each character would have the gradient specified as the text has changed.
So instead of using the ForeColor I would apply a LinearGradientBrush. I am using WinForms at the moment.
EDIT 1
Here is the code that I am currently using. However, this only applies the gradient to all of the characters. I would like to change it so that each character in the string is applied.
// Draw the formatted text string to the DrawingContext of the control.
Font font = new Font("BankGothic Md BT", 48f, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(label1.Location, new Point(label1.Width, label1.Height), Color.Goldenrod, Color.Black);
e.Graphics.DrawString(label1.Text, font, brush, 0,0);
Edit 2
Here is what I did. I just extended the Label class and inherited OnPaint.
public partial class LabelEx : Label {
public LabelEx() {
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e) {
// Draw the formatted text string to the DrawingContext of the control.
//base.OnPaint(e);
Font font = new Font("Tahoma", 48f, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, Width, Height + 5), Color.Gold, Color.Black, LinearGradientMode.Vertical);
e.Graphics.DrawString(Text, font, brush, 0, 0);
}
}
Which gives me a nice gradient text label.
Thanks!
Here is what I did. I just extended the Label class and inherited OnPaint.
public partial class LabelEx : Label {
public LabelEx() {
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e) {
// Draw the formatted text string to the DrawingContext of the control.
//base.OnPaint(e);
Font font = new Font("Tahoma", 48f, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, Width, Height + 5), Color.Gold, Color.Black, LinearGradientMode.Vertical);
e.Graphics.DrawString(Text, font, brush, 0, 0);
}
}
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.
I have the following code. Is there an easy way to put an outline on the text I am writing?
var imageEncoder = Encoder.Quality;
var imageEncoderParameters = new EncoderParameters(1);
imageEncoderParameters.Param[0] = new EncoderParameter(imageEncoder, 100L);
var productImage = GetImageFromByteArray(myViewModel.ProductImage.DatabaseFile.FileContents);
var graphics = Graphics.FromImage(productImage);
var font = new Font("Segoe Script", 24);
var brush = Brushes.Orange;
var container = new Rectangle(myViewModel.ContainerX, myViewModel.ContainerY, myViewModel.ContainerWidth, myViewModel.ContainerHeight);
var stringFormat = new StringFormat {Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center};
graphics.DrawString(customizationText, font, brush, container, stringFormat);
Yes. Instead of DrawString, use the following sequence of calls:
new GraphicsPath (creates an empty GraphicsPath)
GraphicsPath.AddString (the GraphicsPath object now represents the outline of the text)
Graphics.DrawPath (draws the outline in any Pen you want)
If you need to use GraphicsPath.AddString alongside Graphics.DrawString, you need to convert the font sizes, because Graphics.DrawString expects “point size” while GraphicsPath.AddString expects “em size”. The conversion formula is simply emSize = g.DpiY * pointSize / 72.
Here's a code example:
// assuming g is the Graphics object on which you want to draw the text
GraphicsPath p = new GraphicsPath();
p.AddString(
"My Text String", // text to draw
FontFamily.GenericSansSerif, // or any other font family
(int) FontStyle.Regular, // font style (bold, italic, etc.)
g.DpiY * fontSize / 72, // em size
new Point(0, 0), // location where to draw text
new StringFormat()); // set options here (e.g. center alignment)
g.DrawPath(Pens.Black, p);
// + g.FillPath if you want it filled as well