Solid Texture Brush


   

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

Publisher: Apress
ISBN: 159059035X
*/

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

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

        public SolidTextureBrush_c()
        {
            //
            // 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()
        {
              // 
              // SolidTextureBrush_c
              // 
              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
              this.ClientSize = new System.Drawing.Size(292, 273);
              this.Name = "SolidTextureBrush_c";
              this.Text = "SolidTextureBrush_c";
              this.Load += new System.EventHandler(this.SolidTextureBrush_c_Load);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new SolidTextureBrush_c());
        }

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


        protected override void OnPaint(PaintEventArgs e) 
        {
          Graphics G  = e.Graphics;
    
          //Brushes class
          G.Clear(Color.BurlyWood);
          Rectangle r  = new Rectangle(new Point(50, 50), 
                              new Size((int)(this.Width - 100), (int)(this.Height - 100)));
          Brush b  = Brushes.Crimson;
          G.FillRectangle(b, r);
          G.FillRectangle(Brushes.Crimson, r);
    
          b.Dispose();
        }
    }
}


           
          


Create Pen from Texture Brush


   

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{
   private System.ComponentModel.Container components = null;
   private Image theImage;
   private Image smallImage;

   public Form1() {
      InitializeComponent();
      SetStyle(ControlStyles.Opaque, true);
      theImage = new Bitmap("Winter.jpg");
      smallImage = new Bitmap(theImage,new Size(theImage.Width / 2, theImage.Height / 2));
   }

   protected override void OnPaint(PaintEventArgs e) {
       Graphics g = e.Graphics;

       g.FillRectangle(Brushes.White, ClientRectangle);

       Brush tBrush = new TextureBrush(smallImage, new Rectangle(0, 0,smallImage.Width, smallImage.Height));
       Pen tPen = new Pen(tBrush, 40);
       g.DrawRectangle(tPen, 0, 0, ClientRectangle.Width, ClientRectangle.Height);
       tPen.Dispose();
       tBrush.Dispose();
   }

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



           
          


Texture Brush Wrap mode


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      Bitmap bmp = new Bitmap("winter.jpg");
      TextureBrush tb = new TextureBrush(bmp);

      tb.WrapMode = WrapMode.Tile;
      g.FillRectangle(tb, this.ClientRectangle);
      bmp.Dispose();
      tb.Dispose();

    }

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


           
          


Create repeatable Texture Brush


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      

      Graphics g = e.Graphics;
      Bitmap bmp = new Bitmap("winter.jpg");
      TextureBrush tb = new TextureBrush(bmp, new Rectangle(0, 0, 25, 25));

      g.FillRectangle(tb, 20, 20, 200, 70);
      g.FillRectangle(tb, 45, 45, 70, 150);
      bmp.Dispose();
      tb.Dispose();
    }

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


           
          


Use Texture Brush to fill Rectangle


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      Bitmap bmp = new Bitmap("winter.jpg");
      TextureBrush tb = new TextureBrush(bmp);

      g.FillRectangle(tb, 20, 20, 200, 70);
      g.FillRectangle(tb, 45, 45, 70, 150);
      bmp.Dispose();
      tb.Dispose();
    }

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


           
          


Easy to create Texture based on Image


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

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

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      Bitmap bmp = new Bitmap("Winter.jpg");
      TextureBrush tb = new TextureBrush(bmp);

      g.FillRectangle(tb, 20, 20, 200, 70);
      bmp.Dispose();
      tb.Dispose();
    }

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


           
          


Set WrapMode of TextureBrush

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