Texture Brush with Image : Texture Brush « 2D « C# / CSharp Tutorial






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








27.30.Texture Brush
27.30.1.Fill a rectangle with Texture BrushFill a rectangle with Texture Brush
27.30.2.Draw string with Texture BrushDraw string with Texture Brush
27.30.3.Texture Brush with Image
27.30.4.Texture Brush based on Image file
27.30.5.TextureBrush for drawing string