Csharp/CSharp Tutorial/2D/Texture Brush

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

Draw string with Texture Brush

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class StringTextureFill : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  
  private Brush brush;
  public StringTextureFill()
  {
    InitializeComponent();
    try
    {
      Image img = new Bitmap("YourFile.bmp");
      brush = new TextureBrush(img);
    }
    catch(Exception e)
    { MessageBox.Show(e.Message);}
  }
  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.Resize += new System.EventHandler(this.StringTextureFill_Resize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.StringTextureFill_Paint);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new StringTextureFill());
  }
  private void StringTextureFill_Resize (object sender, System.EventArgs e)
  {
    Invalidate();
  }
  private void StringTextureFill_Paint (object sender, PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    Rectangle r = ClientRectangle;
    g.DrawString("Bitmaps as brushes!",
      new Font("Arial", 60, 
      FontStyle.Bold | FontStyle.Italic), 
      brush,
      r);
  }
}

Fill a rectangle with Texture Brush

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class RectangleTextureFill : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  
  private Brush brush;
  public RectangleTextureFill()
  {
    InitializeComponent();
    try
    {
      Image img = new Bitmap("YourFile.bmp");
      brush = new TextureBrush(img);
    }
    catch(Exception e)
    { MessageBox.Show(e.Message);}
  }
  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.Resize += new System.EventHandler(this.RectangleTextureFill_Resize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.RectangleTextureFill_Paint);
  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new RectangleTextureFill());
  }
  private void RectangleTextureFill_Resize (object sender, System.EventArgs e)
  {
    Invalidate();
  }
  private void RectangleTextureFill_Paint (object sender, PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    Rectangle r = ClientRectangle;
    g.FillRectangle(brush, r);
  }
}

Texture Brush based on Image file

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
  class Form1 : Form
  {
    private Image theImage;
    private Image smallImage;
    public Form1()
    {
      InitializeComponent();
      theImage = new Bitmap("Person.bmp");
      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));
      g.FillEllipse(tBrush, ClientRectangle);
      tBrush.Dispose();
    }
    private void InitializeComponent()
    {
      this.Text = "Form1";
    }
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
  }

TextureBrush for drawing string

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
  class Form1 : Form
  {
    private Image theImage;
    public Form1()
    {
      SetStyle(ControlStyles.Opaque, true);
      theImage = new Bitmap("tile.bmp");
    }
    protected override void OnPaint(PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.White, ClientRectangle);
      Brush tBrush = new TextureBrush(theImage, new Rectangle(0, 0,theImage.Width, theImage.Height));
      Font trFont = new Font("Times New Roman", 32,FontStyle.Bold | FontStyle.Italic);
      g.DrawString("Hello from Beginning Visual C#",trFont, tBrush, ClientRectangle);
      tBrush.Dispose();
      trFont.Dispose();
    }
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
  }

Texture Brush with Image

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class TextureBrushDemo: Form
{
     MenuItem     miChecked;
     TextureBrush tbrush;
   
     public static void Main()
     {
          Application.Run(new TextureBrushDemo());
     } 
     public TextureBrushDemo()
     {
          ResizeRedraw = true; 
   
          Image image = Image.FromFile("your.jpg");
   
          tbrush = new TextureBrush(image, new Rectangle(95, 0, 50, 55));
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&Wrap-Mode");
   
          foreach (WrapMode wm in Enum.GetValues(typeof(WrapMode)))
          {
               MenuItem mi = new MenuItem();
               mi.Text     = wm.ToString(); 
               mi.Click   += new EventHandler(MenuWrapModeOnClick);
               Menu.MenuItems[0].MenuItems.Add(mi);
          }
          miChecked = Menu.MenuItems[0].MenuItems[0];
          miChecked.Checked = true;
     }
     void MenuWrapModeOnClick(object obj, EventArgs ea)
     {
          miChecked.Checked = false;
          miChecked = (MenuItem) obj;
          miChecked.Checked = true;
   
          tbrush.WrapMode = (WrapMode)miChecked.Index;
          Invalidate();
     }
     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)
     {
          grfx.FillEllipse(tbrush, 0, 0, 2 * cx / 3, 2 * cy / 3);
          grfx.FillEllipse(tbrush, cx / 3, cy / 3, 2 * cx / 3, 2 * cy / 3);
     }
}