📄 gdippens.cs
字号:
namespace GDIPlus.GDIPPens {
using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Reflection;
public class PensSample : Form {
/// <summary>
/// 必需的设计器变量
/// </summary>
private System.ComponentModel.Container components;
private Brush backgroundBrush;
private Brush penTextureBrush;
public PensSample() {
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
//从可执行文件的资源分支中加载要用于背景的图像
Image backgroundImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("colorbars.jpg"));
//现在创建要用于绘制背景的画笔
backgroundBrush = new TextureBrush(backgroundImage);
//从可执行文件的资源分支中加载粗画笔要使用的图像
Image penImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("BoilingPoint.jpg"));
penTextureBrush = new TextureBrush(penImage);
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
//用粗画笔填充背景
//然后应用白色涂料
g.FillRectangle(backgroundBrush, ClientRectangle);
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);
//创建一支部分透明的、紫色的 20 像素宽的笔
Pen penExample1 = new Pen(Color.FromArgb(150, Color.Purple), 20);
//将其做成虚线笔
penExample1.DashStyle = DashStyle.Dash;
//使末端成为圆角
penExample1.StartCap = LineCap.Round;
penExample1.EndCap = LineCap.Round;
//现在用该笔绘制一条曲线
g.DrawCurve(penExample1, new Point[] {
new Point(200, 140),
new Point(700, 240),
new Point(500, 340),
new Point(140, 140),
new Point(40, 340),
});
//现在使用作为填充的位图绘制一条直线
Pen penExample2 = new Pen(penTextureBrush, 25);
penExample2.DashStyle = DashStyle.DashDotDot;
penExample2.StartCap = LineCap.Triangle;
penExample2.EndCap = LineCap.Round;
g.DrawLine(penExample2, 10,450,550,400);
}
/// <summary>
/// 清理正在使用的所有资源
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// 设计器支持所必需的方法,不要使用
/// 代码编辑器修改此方法的内容
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Container ();
this.Size = new System.Drawing.Size(750, 500);
this.Text = "GDI+ 画笔示例";
}
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
public static void Main() {
Application.Run(new PensSample());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -