Drawstring word wrap or display entire text - c#

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.

Related

Display barcode number below barcode image [duplicate]

I have never worked with drawing before and im having a little issue. I cant seem to get the output of this code to work.
The file is saving but it is not drawing on the text. Can anyone see what i may have done wrong?
EDIT: A silly mistake - the backgrond of the image was white (and the brush colour was!). The text is not centered however as i would have expected. Any ideas why SO? :)
EDIT: Image is below.
Thanks
Bitmap myBitmap = new Bitmap(#"C:\Users\Scott\desktop\blank.bmp");
Graphics g = Graphics.FromImage(myBitmap);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.DrawString("My\nText",
new Font("Tahoma", 20),
Brushes.White,
new PointF(0, 0));
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
g.DrawString("My\nText",
new Font("Tahoma", 20), Brushes.White,
new RectangleF(0, 0, 500, 500),
strFormat);
myBitmap.Save(#"C:\Users\Scott\desktop\blank1.bmp");
I am sure you might be looking for this.
rectf = new RectangleF(655, 460, 535, 90); //rectf for My Text
using(Graphics g = Graphics.FromImage(myBitmap))
{
//g.DrawRectangle(new Pen(Color.Red, 2), 655, 460, 535, 90);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString("My\nText", new System.Drawing.Font("Tahoma", 32, FontStyle.Bold), Brushes.Black, rectf, sf);
}
//g.DrawRectangle(new Pen(Color.Red, 2), 655, 460, 535, 90); Line is used to show where your text will be written. So before you actually make your make your text You can see where this rectanlge will be created on the image. If you want the center of the image you can find the height and width and divide that by 2 to find the center of the image and than can plot the rectangle parameters accordingly.

Text getting clipped when drawing using graphics.DrawString method

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);

Render Text as Small Caps in .NET

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));

How can I set the font colour?

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
}
}

How to scale text when drawing to an Image with c#

I would like to draw some text in a rectangle and have it scale to the maximum size that fits within the rectangle.
So far I have this:
Bitmap bitmapImage = new Bitmap(500, 500);
Graphics graphicImage = Graphics.FromImage(bitmapImage);
graphicImage.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
var rect = new Rectangle(0, 0, 500, 500);
graphicImage.DrawString( "testing testing 123!", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, rect);
bitmapImage.Save("test.png");
it draws the text but doesn't scale up the font size.
Call Graphics.MeasureString in a binary search loop.

Categories