using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
class BoldAndItalicTighter: Form
{
public static void Main()
{
Application.Run(new BoldAndItalicTighter());
}
public BoldAndItalicTighter()
{
Text = "Bold and Italic (Tighter)";
Font = new Font("Times New Roman", 24);
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
string str = "text.";
Brush brush = new SolidBrush(clr);
Font fontRegular = Font;
Font fontBold = new Font(fontRegular, FontStyle.Bold);
Font fontItalic = new Font(fontRegular, FontStyle.Italic);
PointF ptf = new PointF(0, 0);
StringFormat strfmt = StringFormat.GenericTypographic;
strfmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
grfx.DrawString(str, fontRegular, brush, ptf, strfmt);
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
TextRenderingHint: Anti-Aliased Text
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
class AntiAliasedText: Form
{
public static void Main()
{
Application.Run(new AntiAliasedText());
}
public AntiAliasedText()
{
Font = new Font(“Times New Roman”, 12);
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Brush brush = new SolidBrush(clr);
string str = “A “;
int cxText = (int) grfx.MeasureString(str, Font).Width;
for (int i = 0; i < 6; i++) { grfx.TextRenderingHint = (TextRenderingHint)i; grfx.DrawString(str, Font, brush, i * cxText, 0); } } } [/csharp]
Use TexturedBrush to draw string
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class MainForm : Form {
private Brush texturedTextBrush;
private Brush texturedBGroundBrush;
public MainForm() {
Image bGroundBrushImage = new Bitmap("Clouds.bmp");
texturedBGroundBrush = new TextureBrush(bGroundBrushImage);
Image textBrushImage = new Bitmap("Soap Bubbles.bmp");
texturedTextBrush = new TextureBrush(textBrushImage);
}
protected void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
Rectangle r = ClientRectangle;
g.FillRectangle(texturedBGroundBrush, r);
g.DrawString("Bitmaps as brushes",
new Font("Arial", 30,
FontStyle.Bold | FontStyle.Italic),
texturedTextBrush,
r);
}
public static void Main(){
Application.Run(new MainForm());
}
}
Hotkey Prefix
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
class UnderlinedText: Form
{
public static void Main()
{
Application.Run(new UnderlinedText());
}
public UnderlinedText()
{
Text = "Underlined Text Using HotkeyPrefix";
Font = new Font("Times New Roman", 14);
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
string str = "This is some &u&n&d&e&r&l&i&n&e&d text!";
StringFormat strfmt = new StringFormat();
strfmt.HotkeyPrefix = HotkeyPrefix.Show;
grfx.DrawString(str, Font, new SolidBrush(clr), 0, 0, strfmt);
}
}
Draw Rectangle surrounding the Text
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;
Pen p = new Pen(Color.Black, 1 / 96f);
Font f = new Font("Times New Roman", 16);
String s = "Abc";
SizeF sf = g.MeasureString(s, f);
g.DrawRectangle(p, 1, 1, sf.Width, sf.Height);
g.DrawString(s, f, Brushes.Black, 1, 1);
f.Dispose();
p.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
Rotate the text 45 degrees clockwise and translate the text 150 pixels horizontally
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Font f = new Font("Times New Roman", 24);
g.DrawString("Rotate then Translate", f, Brushes.Black, 0, 0);
g.RotateTransform(45);
g.TranslateTransform(150, 0);
g.DrawString("Rotate then Translate ", f, Brushes.Black, 0, 0);
}
public static void Main() {
Application.Run(new Form1());
}
}
With a 200px-width rectangle, and a 12pt font, it requires six lines to display the string in its entirety
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "This string is long enough to wrap. ";
Font f = new Font("Arial", 12);
Rectangle r = new Rectangle(20, 20, 200, f.Height * 6);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, f, Brushes.Black, r);
f.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}