Csharp/C Sharp/2D Graphics/Text Rendering

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Draw string with hatch brush

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; 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", 48, FontStyle.Bold);
   HatchBrush hb = new HatchBrush(HatchStyle.Cross,Color.White, Color.Black);
   g.DrawString("Crazy Crosshatch", f, hb, 0, 0);
   f.Dispose();
   }
   public static void Main() {
       Application.Run(new Form1());
   }

}

</source>


DrawString with solid brush and rectangle

<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;

class HuckleberryFinnHalfHeight: Form {

    public static void Main()
    {
         Application.Run(new HuckleberryFinnHalfHeight());
    }
    public HuckleberryFinnHalfHeight()
    {
         ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         Graphics  grfx = pea.Graphics;
         int       cx   = ClientSize.Width;
         int       cy   = ClientSize.Height;
         Pen       pen  = new Pen(ForeColor);
         Rectangle rect = new Rectangle(0, 0, cx / 2, cy / 2);
  
         grfx.DrawString("some", Font, new SolidBrush(ForeColor), rect);
  
         grfx.DrawLine(pen, 0,      cy / 2, cx / 2, cy / 2);
         grfx.DrawLine(pen, cx / 2, 0,      cx / 2, cy / 2);
    }

}

</source>


DrawString with solid brush, string format

<source lang="csharp"> 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);
    }

}

</source>


Hotkey Prefix

<source lang="csharp">

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

}

</source>


TextRenderingHint: Anti-Aliased Text

<source lang="csharp"> 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);
         }
    }

}

</source>


Text Rendering Hint: AntiAliasGridFit

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Text; public class Form1 : Form {

 public Form1() {
       InitializeComponent();
       
 }
   private void SmoothingText_Paint(object sender, PaintEventArgs e)
   {
     Font TextFont = new Font("Times New Roman", 25, FontStyle.Italic);
     e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
     e.Graphics.DrawString("www.nfex.ru", TextFont, Brushes.Black, 20, 20);
   }
   private void InitializeComponent()
   {
     this.SuspendLayout();
     // 
     // SmoothingText
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     this.ClientSize = new System.Drawing.Size(472, 315);
     this.Name = "SmoothingText";
     this.Text = "SmoothingText";
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingText_Paint);
     this.ResumeLayout(false);
   }
 [STAThread]
 static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

}


      </source>


Text Rendering Hint: ClearTypeGridFit

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Text; public class Form1 : Form {

 public Form1() {
       InitializeComponent();
       
 }
   private void SmoothingText_Paint(object sender, PaintEventArgs e)
   {
     Font TextFont = new Font("Times New Roman", 25, FontStyle.Italic);
     e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
     e.Graphics.DrawString("www.nfex.ru", TextFont, Brushes.Black, 20, 20);
   }
   private void InitializeComponent()
   {
     this.SuspendLayout();
     // 
     // SmoothingText
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     this.ClientSize = new System.Drawing.Size(472, 315);
     this.Name = "SmoothingText";
     this.Text = "SmoothingText";
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingText_Paint);
     this.ResumeLayout(false);
   }
 [STAThread]
 static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

}


      </source>


Text Rendering Hint: SingleBitPerPixelGridFit

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Text; public class Form1 : Form {

 public Form1() {
       InitializeComponent();
       
 }
   private void SmoothingText_Paint(object sender, PaintEventArgs e)
   {
     Font TextFont = new Font("Times New Roman", 25, FontStyle.Italic);
     e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
     e.Graphics.DrawString("www.nfex.ru", TextFont, Brushes.Black, 20, 20);
   }
   private void InitializeComponent()
   {
     this.SuspendLayout();
     // 
     // SmoothingText
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     this.ClientSize = new System.Drawing.Size(472, 315);
     this.Name = "SmoothingText";
     this.Text = "SmoothingText";
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingText_Paint);
     this.ResumeLayout(false);
   }
 [STAThread]
 static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

}


      </source>


Use TexturedBrush to draw string

<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; 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());
   }

}

</source>