0

0

C#中GDI+编程10个基本技巧二

高洛峰

高洛峰

发布时间:2016-12-17 09:53:12

|

1231人浏览过

|

来源于php中文网

原创

5、渐变色填充

需要使用两个刷子:

线性梯度刷子(LinearGradientBrush)

路径梯度刷子(PathGuadientBrush)

private void button4_Click(object sender,System.EventArgs e)

{

//绘图表面

Graphics g =this.pictureBoxII1.CreateGraphics();

g.FillRectangle(Brushes.White,this.pictureBoxII1.ClientRectangle);

//定义一个线性梯度刷子

LinearGradientBrush lgbrush =

new LinearGradientBrush(

new Point(0, 10),

new Point(150, 10),

Color.FromArgb(255, 0, 0),

Color.FromArgb(0, 255, 0));

Pen pen = new Pen(lgbrush);

 
//用线性笔刷梯度效果的笔绘制一条直线段并填充一个矩形

g.DrawLine(pen, 10, 130, 500, 130);

g.FillRectangle(lgbrush, 10, 150, 370, 30);

 
//定义路径并添加一个椭圆

GraphicsPath gp = new GraphicsPath();

gp.AddEllipse(10, 10, 200, 100);

//用该路径定义路径梯度刷子

PathGradientBrush brush =

new PathGradientBrush(gp);

//颜色数组

Color[] colors = {

Color.FromArgb(255, 0, 0),

Color.FromArgb(100, 100, 100),

Color.FromArgb(0, 255, 0),

Color.FromArgb(0, 0, 255)};

//定义颜色渐变比率

float[] r = {0.0f, 0.3f, 0.6f, 1.0f};

ColorBlend blend = new ColorBlend();

blend.Colors = colors;

blend.Positions = r;

brush.InterpolationColors = blend;

//在椭圆外填充一个矩形

g.FillRectangle(brush, 0, 0, 210, 110);

 
//用添加了椭圆的路径定义第二个路径梯度刷子

GraphicsPath gp2 = new GraphicsPath();

gp2.AddEllipse(300, 0, 200, 100);

PathGradientBrush brush2 = new PathGradientBrush(gp2);

//设置中心点位置和颜色

brush2.CenterPoint = new PointF(450, 50);

brush2.CenterColor = Color.FromArgb(0, 255, 0);

//设置边界颜色

Color[] color2 = {Color.FromArgb(255, 0, 0)};

brush2.SurroundColors = color2;

//用第二个梯度刷填充椭圆

g.FillEllipse(brush2, 300, 0, 200, 100);

}

 
6、GDI+的坐标系统

 
通用坐标系——用户自定义坐标系。

页面坐标系——虚拟坐标系。
设备坐标系——屏幕坐标系。

 
当页面坐标系和设备坐标系的单位都是象素时,它们相同。

private void button10_Click(object sender, System.EventArgse)

{

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

this.Draw(g);

}


private void Draw(Graphics g)

{

g.DrawLine(Pens.Black, 10, 10, 100, 100);

g.DrawEllipse(Pens.Black, 50, 50, 200, 100);

g.DrawArc(Pens.Black, 100, 10, 100, 100, 20, 160);

g.DrawRectangle(Pens.Green, 50, 200, 150, 100);

}

 
private void button5_Click(object sender, System.EventArgs e)

{

//左移

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.TranslateTransform(-50, 0);

this.Draw(g);

}

 
private void button6_Click(object sender, System.EventArgs e)

{

//右移

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.TranslateTransform(50, 0);

this.Draw(g);

}

 
private void button7_Click(object sender, System.EventArgs e)

{

//旋转

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.RotateTransform(-30);

this.Draw(g);

}

 
private void button8_Click(object sender, System.EventArgs e)

{

//放大

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.ScaleTransform(1.2f, 1.2f);

this.Draw(g);

}

 
private void button9_Click(object sender, System.EventArgs e)

{

//缩小

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.ScaleTransform(0.8f, 0.8f);

this.Draw(g);

}

 
7、全局坐标——变换对于绘图表面上的每个图元都会产生影响。通常用于设定通用坐标系。

EZIBI! 商城(原维C商城)
EZIBI! 商城(原维C商城)

前身是vitcie(维C商城),各种特性介绍: 1. 稳定、安全、高效的系统平台 EZIBI!基于PHP+MYSQL技术编写,PHP自1995发布第一个版本,经过近10年的发展,已经成为目前最流行的网络编程语言之一,其强大的数据库支持使得开发人员很轻易的就可以完成C/S架构电子商务平台的构建;MYSQL则是成熟的数据库系统。 2. 安装向导 EZIBI!提供支持多语言版的安装脚本,只需按照提

下载

下程序将原定点移动到控件中心,并且Y轴正向朝上。

//先画一个圆

Graphics g = e.Graphics;

g.FillRectangle(Brushes.White, this.ClientRectangle);

g.DrawEllipse(Pens.Black, -100, -100, 200, 200);

 
//使y轴正向朝上,必须做相对于x轴镜像

//变换矩阵为[1,0,0,-1,0,0]

Matrix mat = new Matrix(1, 0, 0, -1, 0, 0);

g.Transform = mat;

Rectangle rect = this.ClientRectangle;

int w = rect.Width;

int h = rect.Height;

g.TranslateTransform(w/2, -h/2);

 
//以原点为中心,做一个半径为100的圆

g.DrawEllipse(Pens.Red, -100, -100, 200, 200);
g.TranslateTransform(100, 100);

g.DrawEllipse(Pens.Green, -100, -100, 200, 200);

g.ScaleTransform(2, 2);

g.DrawEllipse(Pens.Blue, -100, -100, 200, 200);

 

8、局部坐标系——只对某些图形进行变换,而其它图形元素不变。

 
protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

//客户区设置为白色

g.FillRectangle(Brushes.White, this.ClientRectangle);

//y轴朝上

Matrix mat = new Matrix(1, 0, 0, -1, 0, 0);

g.Transform = mat;

//移动坐标原点到窗体中心

Rectangle rect = this.ClientRectangle;

int w = rect.Width;

int h = rect.Height;

g.TranslateTransform(w/2, -h/2);

//在全局坐标下绘制椭圆

g.DrawEllipse(Pens.Red, -100, -100, 200, 200);

g.FillRectangle(Brushes.Black, -108, 0, 8, 8);

g.FillRectangle(Brushes.Black, 100, 0, 8, 8);

g.FillRectangle(Brushes.Black, 0, 100, 8, 8);

g.FillRectangle(Brushes.Black, 0, -108, 8, 8);


//创建一个椭圆然后在局部坐标系中进行变换

GraphicsPath gp = new GraphicsPath();

gp.AddEllipse(-100, -100, 200, 200);

Matrix mat2 = new Matrix();

//平移

mat2.Translate(150, 150);

//旋转

mat2.Rotate(30);

gp.Transform(mat2);

g.DrawPath(Pens.Blue, gp);

 
PointF[] p = gp.PathPoints;

g.FillRectangle(Brushes.Black, p[0].X-2, p[0].Y+2, 4, 4);

g.FillRectangle(Brushes.Black, p[3].X-2, p[3].Y+2, 4, 4);

g.FillRectangle(Brushes.Black, p[6].X-4, p[6].Y-4, 4, 4);

g.FillRectangle(Brushes.Black, p[9].X-4, p[9].Y-4, 4, 4);
gp.Dispose();
//base.OnPaint (e);

}

 
9、Alpha混合


Color.FromArgb()的A就是Alpha。Alpha的取值范围从0到255。0表示完全透明,255完全不透明。


当前色=前景色×alpha/255+背景色×(255-alpha)/255


protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

//创建一个填充矩形

SolidBrush brush = new SolidBrush(Color.BlueViolet);

g.FillRectangle(brush, 180, 70, 200, 150);

//创建一个位图,其中两个位图之间有透明效果

Bitmap bm1 = new Bitmap(200, 100);

Graphics bg1 = Graphics.FromImage(bm1);

SolidBrush redBrush =

new SolidBrush(Color.FromArgb(210, 255, 0, 0));

SolidBrush greenBrush =

new SolidBrush(Color.FromArgb(210, 0, 255, 0));

bg1.FillRectangle(redBrush, 0, 0, 150, 70);

bg1.FillRectangle(greenBrush, 30, 30, 150, 70);

g.DrawImage(bm1, 100, 100);

//创建一个位图,其中两个位图之间没有透明效果

Bitmap bm2 = new Bitmap(200, 100);

Graphics bg2 = Graphics.FromImage(bm2);

bg2.CompositingMode = CompositingMode.SourceCopy;

bg2.FillRectangle(redBrush, 0, 0, 150, 170);

bg2.FillRectangle(greenBrush, 30, 30, 150, 70);

g.CompositingQuality = CompositingQuality.GammaCorrected;

g.DrawImage(bm2, 300, 200);
//base.OnPaint (e);

}

 

10、反走样


protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

//放大8倍

g.ScaleTransform(8, 8);

//没有反走样的图形和文字

Draw(g);

 
//设置反走样

g.SmoothingMode = SmoothingMode.AntiAlias;

 
//右移40

g.TranslateTransform(40, 0);

//再绘制就是反走样之后的了

Draw(g);
//base.OnPaint (e);

}
private void Draw(Graphics g)

{

//绘制图形和文字

g.DrawLine(Pens.Gray, 10, 10, 40, 20);

g.DrawEllipse(Pens.Gray, 20, 20, 30, 10);

string s = "反走样测试";

Font font = new Font("宋体", 5);

SolidBrush brush = new SolidBrush(Color.Gray);

 
g.DrawString(s, font, brush, 10, 40);

}

更多 C#中GDI+编程10个基本技巧二相关文章请关注PHP中文网!

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
苹果官方查询网站 苹果手机正品激活查询入口
苹果官方查询网站 苹果手机正品激活查询入口

苹果官方查询网站主要通过 checkcoverage.apple.com/cn/zh/ 进行,可用于查询序列号(SN)对应的保修状态、激活日期及技术支持服务。此外,查找丢失设备请使用 iCloud.com/find,购买信息与物流可访问 Apple (中国大陆) 订单状态页面。

0

2026.01.26

npd人格什么意思 npd人格有什么特征
npd人格什么意思 npd人格有什么特征

NPD(Narcissistic Personality Disorder)即自恋型人格障碍,是一种心理健康问题,特点是极度夸大自我重要性、需要过度赞美与关注,同时极度缺乏共情能力,背后常掩藏着低自尊和不安全感,影响人际关系、工作和生活,通常在青少年时期开始显现,需由专业人士诊断。

1

2026.01.26

windows安全中心怎么关闭 windows安全中心怎么执行操作
windows安全中心怎么关闭 windows安全中心怎么执行操作

关闭Windows安全中心(Windows Defender)可通过系统设置暂时关闭,或使用组策略/注册表永久关闭。最简单的方法是:进入设置 > 隐私和安全性 > Windows安全中心 > 病毒和威胁防护 > 管理设置,将实时保护等选项关闭。

0

2026.01.26

2026年春运抢票攻略大全 春运抢票攻略教你三招手【技巧】
2026年春运抢票攻略大全 春运抢票攻略教你三招手【技巧】

铁路12306提供起售时间查询、起售提醒、购票预填、候补购票及误购限时免费退票五项服务,并强调官方渠道唯一性与信息安全。

3

2026.01.26

个人所得税税率表2026 个人所得税率最新税率表
个人所得税税率表2026 个人所得税率最新税率表

以工资薪金所得为例,应纳税额 = 应纳税所得额 × 税率 - 速算扣除数。应纳税所得额 = 月度收入 - 5000 元 - 专项扣除 - 专项附加扣除 - 依法确定的其他扣除。假设某员工月工资 10000 元,专项扣除 1000 元,专项附加扣除 2000 元,当月应纳税所得额为 10000 - 5000 - 1000 - 2000 = 2000 元,对应税率为 3%,速算扣除数为 0,则当月应纳税额为 2000×3% = 60 元。

1

2026.01.26

oppo云服务官网登录入口 oppo云服务登录手机版
oppo云服务官网登录入口 oppo云服务登录手机版

oppo云服务https://cloud.oppo.com/可以在云端安全存储您的照片、视频、联系人、便签等重要数据。当您的手机数据意外丢失或者需要更换手机时,可以随时将这些存储在云端的数据快速恢复到手机中。

1

2026.01.26

抖币充值官方网站 抖币性价比充值链接地址
抖币充值官方网站 抖币性价比充值链接地址

网页端充值步骤:打开浏览器,输入https://www.douyin.com,登录账号;点击右上角头像,选择“钱包”;进入“充值中心”,操作和APP端一致。注意:切勿通过第三方链接、二维码充值,谨防受骗

3

2026.01.26

Java Spring Security 与认证授权
Java Spring Security 与认证授权

本专题系统讲解 Java Spring Security 框架在认证与授权中的应用,涵盖用户身份验证、权限控制、JWT与OAuth2实现、跨站请求伪造(CSRF)防护、会话管理与安全漏洞防范。通过实际项目案例,帮助学习者掌握如何 使用 Spring Security 实现高安全性认证与授权机制,提升 Web 应用的安全性与用户数据保护。

25

2026.01.26

c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

76

2026.01.23

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
C# 教程
C# 教程

共94课时 | 7.5万人学习

C 教程
C 教程

共75课时 | 4.2万人学习

C++教程
C++教程

共115课时 | 13.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号