Set WrapMode of TextureBrush

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;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Bitmap bmp = new Bitmap("alphabet.gif");
        TextureBrush tb = new TextureBrush(bmp);

        tb.WrapMode = WrapMode.Tile;
        g.FillRectangle(tb, this.ClientRectangle);
        bmp.Dispose();
        tb.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


new TextureBrush(bmp, new Rectangle(0, 0, 25, 25))

image_pdfimage_print
   
 

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;
    Bitmap bmp = new Bitmap("alphabet.gif");
    TextureBrush tb = new TextureBrush(bmp, new Rectangle(0, 0, 25, 25));

    g.FillRectangle(tb, 45, 45, 70, 150);
    bmp.Dispose();
    tb.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


new TextureBrush(bmp)

image_pdfimage_print
   
 
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;
    Bitmap bmp = new Bitmap("alphabet.gif");
    TextureBrush tb = new TextureBrush(bmp);

    g.FillRectangle(tb, 20, 20, 200, 70);
    bmp.Dispose();
    tb.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


Text Rotate 2

image_pdfimage_print


   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Drawing2D;

namespace Altamira
{
    /// <summary>
    /// Summary description for Altamira.
    /// </summary>
    public class Altamira : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Altamira()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            this.Text = "Text direction";
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // Altamira
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(224, 141);
            this.Name = "Altamira";
            this.Text = "Altamira";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Altamira());
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = CreateGraphics();          
            string txt = "HELLO";
            float alpha = 45.0f;
            int fontSize = 24;
            Point center = new Point(90,20);

            // Vertical text:
            FontFamily ff = new FontFamily("Times New Roman");
            Font f = new Font(ff, fontSize, FontStyle.Regular);
            StringFormat sf = new StringFormat();
            sf.FormatFlags = StringFormatFlags.DirectionVertical;
            g.DrawString(txt, f, new SolidBrush(Color.Blue), center, sf);

            // Global shift of the origin:
            g.TranslateTransform(center.X, center.Y);  // X + fontSize/2
            g.DrawEllipse(Pens.Magenta, new Rectangle(0,0, 1, 1));  // center

            // Local rotation of vertcal text (sf):
            GraphicsPath gp = new GraphicsPath();
            gp.AddString(txt, ff, (int)FontStyle.Bold, fontSize + 4, 
                new Point(0, 0), sf);
            Matrix m = new Matrix();
            m.Rotate(alpha);  // clockwise
            gp.Transform(m);
            g.DrawPath(Pens.Red, gp);  //g.FillPath(Brushes.Black, gp);

            // Global rotation of vertical text (sf):
            g.RotateTransform(-alpha);  // anticlockwise
            g.DrawString(txt, f, new SolidBrush(Color.Black), 0, 0, sf);

            gp.Dispose();  g.Dispose();  m.Dispose();
        }
    }
}


           
          


Draw line and string

image_pdfimage_print


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GDI_Basics
{
    /// <summary>
    /// Summary description for PenCaps.
    /// </summary>
    public class PenCaps1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public PenCaps1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // PenCaps
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(424, 330);
            this.Name = "PenCaps";
            this.Text = "PenCaps";
            this.Resize += new System.EventHandler(this.PenCaps_Resize);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenCaps_Paint);

        }
        #endregion

        private void PenCaps_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Pen myPen = new Pen(Color.Blue, 10);
            int y  = 20;
            
            foreach (LineCap cap in System.Enum.GetValues(typeof(LineCap)))
            {
                myPen.StartCap = cap;
                myPen.EndCap = cap;
                e.Graphics.DrawLine(myPen, 20, y, 100, y);
                e.Graphics.DrawString(cap.ToString(), new Font("Tahoma", 8), Brushes.Black, 120, y - 10);
                y += 30;
            }
        }

        [STAThread]
        static void Main() 
        {
            Application.Run(new PenCaps1());
        }

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



           
          


Text Format

image_pdfimage_print

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/

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

namespace TextFormat_c
{
public class TextFormat : System.Windows.Forms.Form
{

#region Class Local Variables
Rectangle UserRect = Rectangle.Empty;
bool RectStarted = false;
bool RectFinished = false;
bool Typing = false;
bool InsideRect = false;
Cursor ClientCursor = Cursors.Default;
string RectText = string.Empty;
StringFormat BoxFormat = StringFormat.GenericDefault;
#endregion

private System.Windows.Forms.Button cmdNew;
private System.Windows.Forms.Button cmdLeft;
private System.Windows.Forms.Button cmdTop;

private System.ComponentModel.Container components = null;

public TextFormat()
{
InitializeComponent();

//All the mouse move events use the same delegate
this.cmdNew.MouseMove += new MouseEventHandler(this.Button_MouseMove);
this.cmdLeft.MouseMove += new MouseEventHandler(this.Button_MouseMove);
this.cmdTop.MouseMove += new MouseEventHandler(this.Button_MouseMove);

this.KeyPress += new KeyPressEventHandler(this.FormKeyPress);

BoxFormat.Alignment = StringAlignment.Center;
BoxFormat.LineAlignment = StringAlignment.Center;
}

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

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.cmdNew = new System.Windows.Forms.Button();
this.cmdLeft = new System.Windows.Forms.Button();
this.cmdTop = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cmdNew
//
this.cmdNew.Location = new System.Drawing.Point(16, 328);
this.cmdNew.Name = “cmdNew”;
this.cmdNew.Size = new System.Drawing.Size(56, 24);
this.cmdNew.TabIndex = 0;
this.cmdNew.Text = “&New”;
this.cmdNew.Click += new System.EventHandler(this.cmdNew_Click);
//
// cmdLeft
//
this.cmdLeft.Location = new System.Drawing.Point(104, 328);
this.cmdLeft.Name = “cmdLeft”;
this.cmdLeft.Size = new System.Drawing.Size(104, 24);
this.cmdLeft.TabIndex = 1;
this.cmdLeft.Text = “Center Left Align”;
this.cmdLeft.Click += new System.EventHandler(this.cmdLeft_Click);
//
// cmdTop
//
this.cmdTop.Location = new System.Drawing.Point(248, 328);
this.cmdTop.Name = “cmdTop”;
this.cmdTop.Size = new System.Drawing.Size(104, 24);
this.cmdTop.TabIndex = 2;
this.cmdTop.Text = “Top Left Align”;
this.cmdTop.Click += new System.EventHandler(this.cmdTop_Click);
//
// TextFormat
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdTop,
this.cmdLeft,
this.cmdNew});
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = “TextFormat”;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = “TextFormat”;
this.Load += new System.EventHandler(this.TextFormat_Load);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new TextFormat());
}

private void TextFormat_Load(object sender, System.EventArgs e)
{
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics G = e.Graphics;
using(Pen P = new Pen(Brushes.Black, 1))
{
if ( !RectFinished )
P.DashStyle = DashStyle.Dash;

G.DrawRectangle(P,UserRect);
}

if (RectFinished && ClientCursor == Cursors.IBeam)
G.DrawString(RectText, new Font(“Arial”, 16), Brushes.Black,
UserRect, BoxFormat);

base.OnPaint(e);
}

protected override void OnMouseDown(MouseEventArgs e)
{
//If left button then start the rectangle
if ( e.Button == MouseButtons.Left )
{
if (UserRect == Rectangle.Empty)
{
UserRect.X = e.X;
UserRect.Y = e.Y;
RectStarted = true;
RectFinished = false;
}
}
//If right button then start the edit
else if ( e.Button == MouseButtons.Right )
{
if ( UserRect != Rectangle.Empty )
{
ClientCursor = Cursors.IBeam;
this.Cursor = ClientCursor;
Point pos = new Point(UserRect.X+UserRect.Width/2,
UserRect.Y+UserRect.Height/2);
//Translate cursor screen postion to position on form
int Offset = this.Height-this.ClientRectangle.Height;
pos += new Size(this.Location.X, this.Location.Y+Offset);
Cursor.Position = pos;
Typing = true;
this.KeyPreview = true;
}
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);

// A negative rectangle is not allowed.
// Mouse_down then Mouse_up without Mouse_move is not allowed
if (UserRect.Width <= 0 || UserRect.Height <=0 ) UserRect = Rectangle.Empty; //Rectangle has ended RectStarted = false; RectFinished = true; Invalidate(); } protected override void OnMouseMove(MouseEventArgs e) { base.OnMove(e); //Let program know if cursor is inside user rectangle InsideRect = false; if (UserRect != Rectangle.Empty) if (UserRect.Contains(new Point(e.X, e.Y))) InsideRect = true; this.Cursor = ClientCursor; //Increase the size of the rectangle each time the mouse moves. if (RectStarted) { Size s = new Size(e.X-UserRect.X, e.Y-UserRect.Y); UserRect.Size=s; Invalidate(); } } private void cmdNew_Click(object sender, System.EventArgs e) { if ( Typing && InsideRect ) return; //Start a new blank form UserRect = Rectangle.Empty; ClientCursor = Cursors.Default; RectStarted = false; RectFinished = false; RectText = string.Empty; this.KeyPreview=false; Invalidate(); } private void Button_MouseMove(object sender, MouseEventArgs e) { this.Cursor = Cursors.Default; } void FormKeyPress(object sender, KeyPressEventArgs e) { //Handle backspace key if (e.KeyChar == (char)8) { if ( RectText != string.Empty ) RectText = RectText.Remove(RectText.Length-1, 1); } else RectText += e.KeyChar; Invalidate(); } private void cmdLeft_Click(object sender, System.EventArgs e) { //Change horizontal alignement and redraw BoxFormat.Alignment = StringAlignment.Near; Invalidate(); } private void cmdTop_Click(object sender, System.EventArgs e) { //Chnage vertical alignment and redraw BoxFormat.LineAlignment = StringAlignment.Near; Invalidate(); } } } [/csharp]

Text Rotate

image_pdfimage_print


   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TextRotate_c
{
    public class TextRotate : System.Windows.Forms.Form
    {
    Bitmap bmp; 
    Point UL, UR, BL;
    Rectangle InvRect;
    int Direction = -1;
    String s = "ROTATING TEXT";
    Font fnt = new Font("Arial", 12);

    private System.Windows.Forms.VScrollBar Skew;
    private System.Windows.Forms.Timer T1;
    private System.ComponentModel.IContainer components;

        public TextRotate()
        {
            InitializeComponent();

      Skew.Minimum = 50;
      Skew.Maximum = 250;
      Skew.SmallChange = 1;
      Skew.LargeChange = 10;
      Skew.Value = 150;

      using (Graphics G = this.CreateGraphics())
      {
        SizeF sz = G.MeasureString(s, fnt);
        bmp = new  Bitmap((int)sz.Width, (int)sz.Height);
      }

      for ( int k=0; k<bmp.Height; k++ )
      {
        for ( int j=0; j<bmp.Width; j++ )
          bmp.SetPixel(j, k, Color.White);
      }
      bmp.MakeTransparent(Color.White);

      UL = new Point(150, 150);
      UR = new Point(UL.X+bmp.Width, Skew.Value);
      BL = new Point(150, UL.Y+bmp.Height);
      InvRect = new  Rectangle(-UR.X, Skew.Minimum, 2*UR.X, Skew.Maximum);

      using (Graphics G = Graphics.FromImage(bmp))
      {
        G.SmoothingMode = SmoothingMode.AntiAlias;
        G.TextRenderingHint = TextRenderingHint.AntiAlias;
        G.DrawString(s, fnt, Brushes.Black, 0, 0);
      }

      this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      this.SetStyle(ControlStyles.DoubleBuffer, true);

      T1.Interval=10; //milliseconds
      T1.Enabled=false;
        }

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

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
      this.components = new System.ComponentModel.Container();
      this.Skew = new System.Windows.Forms.VScrollBar();
      this.T1 = new System.Windows.Forms.Timer(this.components);
      this.SuspendLayout();
      // 
      // Skew
      // 
      this.Skew.Location = new System.Drawing.Point(352, 54);
      this.Skew.Name = "Skew";
      this.Skew.Size = new System.Drawing.Size(16, 264);
      this.Skew.TabIndex = 1;
      this.Skew.Scroll += new System.Windows.Forms.ScrollEventHandler(this.Skew_Scroll);
      // 
      // T1
      // 
      this.T1.Tick += new System.EventHandler(this.T1_Tick);
      // 
      // TextRotate
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(392, 373);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.Skew});
      this.MaximizeBox = false;
      this.MinimizeBox = false;
      this.Name = "TextRotate";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "TextRotate";
      this.Load += new System.EventHandler(this.TextRotate_Load);
      this.ResumeLayout(false);

    }
        #endregion

        [STAThread]
        static void Main() 
        {
            Application.Run(new TextRotate());
        }

    private void TextRotate_Load(object sender, System.EventArgs e)
    {
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      Point[] dest = {UL, UR, BL};

      // Draw the image mapped to the parallelogram.
      e.Graphics.DrawImage(bmp, dest);
      e.Graphics.DrawLine(Pens.Black, UL, BL+new Size(0, 20));
      if (T1.Enabled==false)
        T1.Enabled=true;
    }

    private void Skew_Scroll(object sender, 
                             System.Windows.Forms.ScrollEventArgs e)
    {
      UR.Y = Skew.Value;
    }

    private void T1_Tick(object sender, System.EventArgs e)
    {
      UR.X += Direction;
      if ( UR.X == UL.X + bmp.Width*Direction )
        Direction *=-1;

      Invalidate(InvRect);
    }

    }
}