Draw string with new font and solid brush

image_pdfimage_print


   

  using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class MainForm : System.Windows.Forms.Form
  {
    private System.ComponentModel.Container components;

    public MainForm()
    {
      InitializeComponent();
      BackColor = Color.Tomato;
      Opacity = 0.5d;
      Text = "www.kutayzorlu.com/java2s/com";
      Cursor = Cursors.WaitCursor;
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Form1";
    }
    [STAThread]
    static void Main() 
    {
      Application.Run(new MainForm());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.DrawString("www.kutayzorlu.com/java2s/com", 
        new Font("Times New Roman", 20), 
        new SolidBrush(Color.Black), 40, 10);
    }
  }



           
          


Draw multiline text: auto wrap

image_pdfimage_print


   


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form{
  public Form1()
  {
        InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
        Bounds = new Rectangle(0, 0, 500, 300);
  }

    protected override void OnPaint(PaintEventArgs e) {
         Graphics g = e.Graphics;
   
         g.FillRectangle(Brushes.White, ClientRectangle);


         // Draw multiline text
         Font trFont = new Font("Times New Roman", 12);
         Rectangle rect = new Rectangle(0, 0, 400, trFont.Height * 3);
         g.DrawRectangle(Pens.Blue, rect);
         String longString = "Text Text Text Text Text Text Text Text Text Text  ";
         longString += "Text Text Text Text Text Text Text Text Text Text  ";
         longString += "Text Text Text Text Text Text Text Text Text Text  ";
         longString += "Text Text Text Text Text Text Text Text Text .";
         g.DrawString(longString, trFont, Brushes.Black, rect);

      }

    private void InitializeComponent()
    {
      this.Size = new System.Drawing.Size(300,300);
      this.Text = "Form1";
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
}


           
          


Clip Text

image_pdfimage_print

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;

class FontMenuForm : Form {
protected string strText = “Clip Text”;
protected Font font = new Font(“Times New Roman”, 24);

public static void Main() {
Application.Run(new FontMenuForm());
}
public FontMenuForm() {
ResizeRedraw = true;
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
GraphicsPath path = new GraphicsPath();
float fFontSize = PointsToPageUnits(grfx, font);

path.AddString(strText, font.FontFamily, (int)font.Style,
fFontSize, new PointF(0, 0), new StringFormat());

grfx.SetClip(path);

RectangleF rectfBounds = path.GetBounds();
grfx.TranslateClip(
(cx – rectfBounds.Width) / 2 – rectfBounds.Left,
(cy – rectfBounds.Height) / 2 – rectfBounds.Top);

Random rand = new Random();

for (int y = 0; y < cy; y++) { Pen pen = new Pen(Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255))); grfx.DrawBezier(pen, new Point(0, y), new Point(cx / 3, y + cy / 3), new Point(2 * cx / 3, y - cy / 3), new Point(cx, y)); } } public float GetAscent(Graphics grfx, Font font) { return font.GetHeight(grfx) * font.FontFamily.GetCellAscent(font.Style) / font.FontFamily.GetLineSpacing(font.Style); } public float GetDescent(Graphics grfx, Font font) { return font.GetHeight(grfx) * font.FontFamily.GetCellDescent(font.Style) / font.FontFamily.GetLineSpacing(font.Style); } public float PointsToPageUnits(Graphics grfx, Font font) { float fFontSize; if (grfx.PageUnit == GraphicsUnit.Display) fFontSize = 100 * font.SizeInPoints / 72; else fFontSize = grfx.DpiX * font.SizeInPoints / 72; return fFontSize; } } [/csharp]

Emboss

image_pdfimage_print
   
 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class FontMenuForm: Form
{
     protected string strText = "Emboss";
     protected Font font = new Font("Times New Roman", 24);
   
     public static void Main()
     {
          Application.Run(new FontMenuForm());
     }
     public FontMenuForm()
     {
          ResizeRedraw = true;


     }
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          int iOffset = 2;
          SizeF sizef = grfx.MeasureString(strText, font);
          float x     = (cx - sizef.Width) / 2;
          float y     = (cy - sizef.Height) / 2;
   
          grfx.Clear(Color.White);
          grfx.DrawString(strText, font, Brushes.Gray, x, y);
          grfx.DrawString(strText, font, Brushes.White, x - iOffset, 
                                                        y - iOffset);

                                            
     }
     public float GetAscent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellAscent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float GetDescent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellDescent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float PointsToPageUnits(Graphics grfx, Font font)
     {
          float fFontSize;
   
          if (grfx.PageUnit == GraphicsUnit.Display)
               fFontSize = 100 * font.SizeInPoints / 72;
          else
               fFontSize = grfx.DpiX * font.SizeInPoints / 72;
   
          return fFontSize;
     }
}

    


Engrave

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class FontMenuForm: Form
{
     protected string strText = "Engrave";
     protected Font font = new Font("Times New Roman", 24);
   
     public static void Main()
     {
          Application.Run(new FontMenuForm());
     }
     public FontMenuForm()
     {
          ResizeRedraw = true;
     }
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          int iOffset = -2;
          SizeF sizef = grfx.MeasureString(strText, font);
          float x     = (cx - sizef.Width) / 2;
          float y     = (cy - sizef.Height) / 2;
   
          grfx.Clear(Color.White);
          grfx.DrawString(strText, font, Brushes.Gray, x, y);
          grfx.DrawString(strText, font, Brushes.White, x - iOffset, 
                                                        y - iOffset);

                                            
     }
     public float GetAscent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellAscent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float GetDescent(Graphics grfx, Font font)
     {
          return font.GetHeight(grfx) * 
                    font.FontFamily.GetCellDescent(font.Style) /
                         font.FontFamily.GetLineSpacing(font.Style);
     }
     public float PointsToPageUnits(Graphics grfx, Font font)
     {
          float fFontSize;
   
          if (grfx.PageUnit == GraphicsUnit.Display)
               fFontSize = 100 * font.SizeInPoints / 72;
          else
               fFontSize = grfx.DpiX * font.SizeInPoints / 72;
   
          return fFontSize;
     }
}

    


Tall in the Center

image_pdfimage_print

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class FontMenuForm: Form
{
protected string strText = “Tall in the Center”;
protected Font font = new Font(“Times New Roman”, 24);

public static void Main()
{
Application.Run(new FontMenuForm());
}
public FontMenuForm()
{
ResizeRedraw = true;
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
GraphicsPath path = new GraphicsPath();
float fFontSize = PointsToPageUnits(grfx, font);

path.AddString(strText, font.FontFamily, (int) font.Style,
fFontSize, new PointF(0, 0), new StringFormat());

RectangleF rectf = path.GetBounds();

path.Transform(new Matrix(1, 0, 0, 1,
-(rectf.Left + rectf.Right) / 2,
-(rectf.Top + rectf.Bottom) / 2));
rectf = path.GetBounds();
PointF[] aptf = path.PathPoints;

for (int i = 0; i < aptf.Length; i++) aptf[i].Y *= 2 * (rectf.Width - Math.Abs(aptf[i].X)) / rectf.Width; path = new GraphicsPath(aptf, path.PathTypes); grfx.TranslateTransform(cx / 2, cy / 2); grfx.FillPath(new SolidBrush(clr), path); } public float GetAscent(Graphics grfx, Font font) { return font.GetHeight(grfx) * font.FontFamily.GetCellAscent(font.Style) / font.FontFamily.GetLineSpacing(font.Style); } public float GetDescent(Graphics grfx, Font font) { return font.GetHeight(grfx) * font.FontFamily.GetCellDescent(font.Style) / font.FontFamily.GetLineSpacing(font.Style); } public float PointsToPageUnits(Graphics grfx, Font font) { float fFontSize; if (grfx.PageUnit == GraphicsUnit.Display) fFontSize = 100 * font.SizeInPoints / 72; else fFontSize = grfx.DpiX * font.SizeInPoints / 72; return fFontSize; } } [/csharp]

Draw rectangle and string inside

image_pdfimage_print


   

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;

  public class MainForm : System.Windows.Forms.Form
  {
    public MainForm()
    {
      InitializeComponent();
      CenterToScreen();
      SetStyle(ControlStyles.ResizeRedraw, true);  
    }

    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pens...";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);

    }
    static void Main() 
    {
      Application.Run(new MainForm());
    }

    private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      // And a rect with some text...
      Rectangle r = new Rectangle(150, 10, 130, 60);
      g.DrawRectangle(Pens.Blue, r);
      g.DrawString("www.kutayzorlu.com/java2s/com", 
        new Font("Arial", 12), Brushes.Black, r);

    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
       Invalidate();  
    }
  }