System.Drawing.Drawing2D examples

D

Dylan Parry

Hi,

I'm looking for examples of the different brush and fill styles that can
be made using System.Drawing.Drawing2D. I haven't actually used this
class before, so any example code accompanying visual style examples
would be an ideal thing to help me learn.

Does anyone know of a website that has such examples?

Cheers,

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

Programming, n: A pastime similar to banging one's head
against a wall, but with fewer opportunities for reward.
 
D

Dave Sexton

Hi Dylan,

Here's an example:

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

public class TestForm : Form
{
public TestForm()
{
DoubleBuffered = true;
ResizeRedraw = true;
BackColor = Color.White;
}

protected override void OnPaint(PaintEventArgs e)
{
int height = ClientRectangle.Height / 3;
int width = ClientRectangle.Width;
int y = 0;

Rectangle hatchArea = new Rectangle(0, y, width, height);
y += height;
Rectangle gradientArea = new Rectangle(0, y, width, height);
y += height;
Rectangle pathArea = new Rectangle(0, y, width, height);

using (HatchBrush hatchBrush = new HatchBrush(
HatchStyle.Cross, Color.Red, Color.Blue))
{
e.Graphics.FillRectangle(hatchBrush, hatchArea);
}

using (LinearGradientBrush gradientBrush = new LinearGradientBrush(
gradientArea, Color.Green, Color.White,
LinearGradientMode.ForwardDiagonal))
{
e.Graphics.FillRectangle(gradientBrush, gradientArea);
}

using (GraphicsPath path = new GraphicsPath())
{
path.AddBezier(0, y, width, y, width, y + height, 0, y + height);

using (PathGradientBrush pathBrush = new PathGradientBrush(path))
{
pathBrush.CenterColor = Color.HotPink;
pathBrush.CenterPoint = new PointF(width / 4, y + height / 2);
pathBrush.SurroundColors = new Color[] {
Color.Yellow,
Color.Lavender,
Color.Ivory,
Color.Indigo
};

e.Graphics.FillRectangle(pathBrush, pathArea);
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top