Live Chat Icon For mobile
Live Chat Icon

WinForms FAQ

Find answers for the most frequently asked questions
Expand All Collapse All

The Brushes Sample shows you how to use four different brushes in several shapes. Below is a code snippet showing how to use hatched and gradient brushes.


Rectangle rect = new Rectangle(35, 190, 100, 100);       
 LinearGradientBrush brush2 = new LinearGradientBrush(rect, 
                           Color.DarkOrange, Color.Aquamarine, 
                          LinearGradientMode.ForwardDiagonal); 
 g.FillEllipse(brush2, 35, 190, 100, 100); 
 
 HatchBrush brush1 = new HatchBrush(HatchStyle.DiagonalCross, 
                          Color.DarkOrange, Color.Aquamarine); 
 g.FillEllipse(brush1, 35, 190, 100, 100);
Permalink

GDI+ features a TextureBrush that lets you draw repeating patterns. You can specify any bitmap to be drawn repeatedly. In this example we create bitmaps using code and attach them to a TextureBrush.


		static object[] patternSpecs = new object[] 
		{
			new short[] { 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 },  // horizontal
			new short[] { 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc },  // vertical
			new short[] { 0x77, 0xbb, 0xdd, 0xee, 0x77, 0xbb, 0xdd, 0xee },   // \\\ Down
			new short[] { 0xee, 0xdd, 0xbb, 0x77, 0xee, 0xdd, 0xbb, 0x77 },   // /// Up
			new short[] { 0xee, 0x00, 0xee, 0xee, 0xee, 0x00, 0xee, 0xee },   // +++ 
		}

		public static Bitmap CreateBitmapFromPattern(int pattern)
		{
			Bitmap patternBitmap;
			IntPtr hBitmap = NativeMethods.CreateBitmap(8, 8, 1, 1, (short[]) patternSpecs[pattern]);
			if (hBitmap != IntPtr.Zero)
				patternBitmap = Image.FromHbitmap(hBitmap);
			NativeMethods.DeleteObject(hBitmap);
			return patternBitmap;
		}

		public static void FillRectangle(Graphics g, Rectangle r, int pattern, Color foreColor, Color backColor) 
		{
			Bitmap bm = CreateBitmapFromPattern(pattern);
			FillRectangle(g, r, bm, foreColor, backColor);
		}
		public static void FillRectangle(Graphics g, Rectangle r, Bitmap bm, Color foreColor, Color backColor) 
		{
			if (bm != null)
			{
				Size size = bm.PhysicalDimension.ToSize();
				TextureBrush br = new TextureBrush(bm, new Rectangle(new Point(0, 0), size), ia);
				br.WrapMode = WrapMode.Tile;
                
				g.FillRectangle(br, r);
				br.Dispose();
			}             
		}

	public class NativeMethods
	{
		[DllImport('gdi32', CharSet=CharSet.Auto, ExactSpelling=true)]
		extern public static IntPtr CreateBitmap(int nWidth, int nHeight, int nPlanes, int nBitsPerPixel, 
			[MarshalAs(UnmanagedType.LPArray)] short[] lpvBits);

		[DllImport('gdi32')]
		extern public static bool DeleteObject(IntPtr hObject)  ;

	}	
Permalink

The Graphics.RenderingOrigin property lets you specify a Point structure that represents the dither origin for 8-bits-per-pixel and 16-bits-per-pixel dithering and is also used to set the origin for hatch brushes.

The following example shows how to use RenderingOrigin:


namespace Scrollable1
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    /// 
    ///    Summary description for ScrollableControl1.
    /// 
    public class ScrollableControl1 : System.Windows.Forms.ScrollableControl
    {
        private void InitializeComponent ()
        {
        }
    
        public ScrollableControl1()
        {
            InitializeComponent ();

            this.AutoScrollMinSize = new Size(500, 500);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
	pe.Graphics.RenderingOrigin = AutoScrollPosition;
            HatchBrush br = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Blue, Color.White);
            pe.Graphics.FillRectangle(br, ClientRectangle /*or pe.ClipRectangle*/);
            br.Dispose();
        }
    }
}

Permalink

Share with

Share on twitter
Share on facebook
Share on linkedin

Couldn't find the FAQs you're looking for?

Please submit your question and answer.