Csharp/C Sharp/2D Graphics/Texture

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

Create Pen from Texture Brush

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
   private System.ruponentModel.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.ruponents = new System.ruponentModel.Container();
        this.Size = new System.Drawing.Size(300,300);
        this.Text = "Form1";
     }
     static void Main() {
         Application.Run(new Form1());
     }
}


Create repeatable Texture Brush

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  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();
    }
  }


Create Texture Brush from image

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
   private System.ruponentModel.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));
         Font trFont = new Font("Times New Roman", 64,
            FontStyle.Bold| FontStyle.Italic );
         g.DrawString("www.nfex.ru", trFont, tBrush, ClientRectangle);
         tBrush.Dispose();
         trFont.Dispose();
     }

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


Easy to create Texture based on Image

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  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();
    }
  }


new TextureBrush(bmp)

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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());
    }
}


new TextureBrush(bmp, new Rectangle( 25))

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
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());
    }
}


Set WrapMode of TextureBrush

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


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


Texture Brushes

/*
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.ruponentModel;
using System.Windows.Forms;
namespace GDI_Basics
{
    /// <summary>
    /// Summary description for TextureBrushes.
    /// </summary>
    public class TextureBrushes : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public TextureBrushes()
        {
            //
            // 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()
        {
            // 
            // TextureBrushes
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "TextureBrushes";
            this.Text = "TextureBrushes";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.TextureBrushes_Paint);
        }
        #endregion
        private void TextureBrushes_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            TextureBrush myBrush = new TextureBrush(Image.FromFile("tile.bmp"));
            e.Graphics.FillRectangle(myBrush, e.Graphics.ClipBounds);
        }
        [STAThread]
        static void Main() 
        {
            Application.Run(new TextureBrushes());
        }
    }
}


Texture Brush Wrap mode

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  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();
    }
  }


Use Texture Brush to fill Rectangle

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  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();
    }
  }