Find answers for the most frequently asked questions
How do I use hatched and gradient brush types
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);
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.
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:
namespaceScrollable1
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
///<summary>/// Summary description for ScrollableControl1.///</summary>publicclassScrollableControl1 : System.Windows.Forms.ScrollableControl
{
privatevoidInitializeComponent ()
{
}
publicScrollableControl1()
{
InitializeComponent ();
this.AutoScrollMinSize = new Size(500, 500);
}
protectedoverridevoidOnPaint(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();
}
}
}
Share with