应用笔记 / 经验分享 · 2023年6月3日

C# Brush的五种子类示例

 

Brush笔刷类,可以用颜色和图像填充图形,是 抽象类,不可以实例化。

实例:
1、SolidBrushTest

 

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SolidBrushTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush brush = new SolidBrush(Color.Orange);
g.FillEllipse(brush, 10, 10, 200, 120);
g.Dispose();

}
}
}

2、 TextureBrushTest

 

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace TextureBrushTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
string path = @”D:\CS\GDIPlusTest\TextureBrushTest\img\微信图片_20170817213231.jpg”;
Graphics g=e.Graphics;
if (File.Exists(path))
{
Bitmap map = new Bitmap(path);
Brush brush = new TextureBrush(map);
g.FillEllipse(brush, 10, 10, 500, 500);
brush.Dispose();
}
else
{
MessageBox.Show(“image is not exists”);
}
g.Dispose();
}
}
}

3、LinearGradientBrushTest

 

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace LinearGradientBrushTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
LinearGradientBrush lgb = new LinearGradientBrush(new Point(10,10),new Point(290,90),Color.White,Color.FromArgb(255,0,0,0));
g.FillEllipse(lgb,10,10,280,120);
lgb.Dispose();
g.Dispose();
}
}
}

4、 PathGradientBrushTest

 

using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace PathGradientBrushTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
//绘画路径
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(0,80,240,120);

//路径渐变画刷
PathGradientBrush pgb = new PathGradientBrush(gp);
pgb.CenterColor = Color.Orange;
Color[] colors = { Color.FromArgb(255,0,255,0)};
pgb.SurroundColors = colors;

//绘制椭圆
e.Graphics.FillEllipse(pgb,0,80,240,120);
pgb.Dispose();
}
}
}

5、 HatchBrushTest

using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace HatchBrushTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
HatchBrush hatchBrush = new HatchBrush(HatchStyle.HorizontalBrick,Color.Red,Color.Yellow);
e.Graphics.FillEllipse(hatchBrush,0,80,240,120);
hatchBrush.Dispose();
}
}
}

————————————————
版权声明:本文为CSDN博主「彭世瑜」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/mouday/article/details/109661898