GDI+编程的10个基本技巧(转)
创建绘图表面 创建绘图表面有两种常用的方法。下面设法得到PictureBox的绘图表面。
private void Form1_Load(object sender, System.EventArgs e)
{
//得到pictureBox1的绘图表面
Graphics g = this.pictureBox1.CreateGraphics();
} private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//得到pictureBox1的绘图表面
Graphics g = e.Graphics;
} 可以利用Graphics对象绘制出各种图形图案。控件的Paint事件和OnPaint方法都可以绘图都是好时机。
在OnPaint方法里绘制图案一定从参数e里面得到Graphics属性。下面是两个例子。
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
float x, y, w, h;
x = this.Left+;
y = this.Top+;
w = this.Width-;
h = this.Height-;
Pen pen = new Pen(Color.Red, );
e.Graphics.DrawRectangle(pen, x, y, w, h);
base.OnPaint (e);
} private void PictureBoxII_Resize(object sender, EventArgs e)
{
this.Invalidate();
} private void button1_Click(object sender, System.EventArgs e)
{
this.pictureBoxII1.CreateGraphics().FillEllipse(
Brushes.Blue, , , , );
}
和文本有关的三个类:
FontFamily——定义有着相似的基本设计但在形式上有某些差异的一组字样。无法继承此类。
Font——定义特定的文本格式,包括字体、字号和字形属性。无法继承此类。
StringFormat——封装文本布局信息(如对齐方式和行距),显示操作(如省略号插入和国家标准 (National) 数字位替换)和 OpenType 功能。无法继承此类。
下面的程序显示了一段文字。
private void button2_Click(object sender, System.EventArgs e)
{
Graphics g = this.pictureBoxII1.CreateGraphics();
g.FillRectangle(Brushes.White, this.pictureBoxII1.ClientRectangle); string s = "aaaaaaaaaaaaaaaaaaaaaaaaaa";
FontFamily fm = new FontFamily("ËÎÌå");
Font f = new Font(fm, , FontStyle.Bold, GraphicsUnit.Point);
RectangleF rectF = new RectangleF(, , , );
StringFormat sf = new StringFormat();
SolidBrush sbrush = new SolidBrush(Color.FromArgb(, , , ));
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.DirectionVertical;
g.DrawString(s, f, sbrush, rectF, sf);
}
GDI+的路径——GraphicsPath类 GraphicsPath类提供了一系列属性和方法,利用它可以获取路径上的关键点,可以添加直线段、圆等几何元素。可以获得包围矩形,进行拾取测试。这些功能都怎么用,要仔细看一下。
private void button3_Click(object sender, System.EventArgs e)
{
//绘图表面
Graphics g = this.pictureBoxII1.CreateGraphics();
//填充成白色
g.FillRectangle(Brushes.White, this.ClientRectangle);
//弄一个绘图路径¶
GraphicsPath gp = new GraphicsPath();
//添加一些集合图形
gp.AddEllipse(, , , );
gp.AddPie(, , , , , );
gp.AddRectangle(new Rectangle(, , , ));
//在绘图表面上绘制绘图路径
g.DrawPath(Pens.Blue, gp);
//平移
g.TranslateTransform(, );
//填充绘图路径¶
g.FillPath(Brushes.GreenYellow, gp);
gp.Dispose();
}
区域——Region类 从已有的矩形和路径可以创建Region。使用Graphics.FillRegion方法绘制Region。该类指示由矩形和由路径构成的图形形状的内部。无法继承此类。
渐变色填充 需要使用两个刷子:
线性梯度刷子(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(, ),
new Point(, ),
Color.FromArgb(, , ),
Color.FromArgb(, , ));
Pen pen = new Pen(lgbrush); //用线性笔刷梯度效果的笔绘制一条直线段并填充一个矩形 g.DrawLine(pen, , , , );
g.FillRectangle(lgbrush, , , , ); //定义路径并添加一个椭圆
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(, , , ); //用该路径定义路径梯度刷子
PathGradientBrush brush =
new PathGradientBrush(gp); //颜色数组
Color[] colors = {
Color.FromArgb(, , ),
Color.FromArgb(, , ),
Color.FromArgb(, , ),
Color.FromArgb(, , )}; //定义颜色渐变比率
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, , , , ); //用添加了椭圆的路径定义第二个路径梯度刷子
GraphicsPath gp2 = new GraphicsPath();
gp2.AddEllipse(, , , );
PathGradientBrush brush2 = new PathGradientBrush(gp2); //设置中心点位置和颜色
brush2.CenterPoint = new PointF(, );
brush2.CenterColor = Color.FromArgb(, , ); //设置边界颜色
Color[] color2 = {Color.FromArgb(, , )};
brush2.SurroundColors = color2; //用第二个梯度刷填充椭圆
g.FillEllipse(brush2, , , , );
}
GDI+的坐标系统
通用坐标系——用户自定义坐标系。
页面坐标系——虚拟坐标系。
设备坐标系——屏幕坐标系。
当页面坐标系和设备坐标系的单位都是象素时,它们相同。
private void button10_Click(object sender, System.EventArgs e)
{
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
this.Draw(g);
}
private void Draw(Graphics g)
{
g.DrawLine(Pens.Black, , , , );
g.DrawEllipse(Pens.Black, , , , );
g.DrawArc(Pens.Black, , , , , , );
g.DrawRectangle(Pens.Green, , , , );
} private void button5_Click(object sender, System.EventArgs e)
{
//左移
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
g.TranslateTransform(-, );
this.Draw(g);
} private void button6_Click(object sender, System.EventArgs e)
{
//右移
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
g.TranslateTransform(, );
this.Draw(g);
} private void button7_Click(object sender, System.EventArgs e)
{
//旋转
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
g.RotateTransform(-);
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);
}
全局坐标——变换对于绘图表面上的每个图元都会产生影响。通常用于设定通用坐标系。
一下程序将原定移动到控件中心,并且Y轴正向朝上。
//先画一个圆 Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
g.DrawEllipse(Pens.Black, -, -, , ); //使y轴正向朝上,必须做相对于x轴镜像
//变换矩阵为[1,0,0,-1,0,0]
Matrix mat = new Matrix(, , , -, , );
g.Transform = mat;
Rectangle rect = this.ClientRectangle;
int w = rect.Width;
int h = rect.Height;
g.TranslateTransform(w/, -h/); //以原点为中心,做一个半径为100的圆
g.DrawEllipse(Pens.Red, -, -, , );
g.TranslateTransform(, );
g.DrawEllipse(Pens.Green, -, -, , );
g.ScaleTransform(, );
g.DrawEllipse(Pens.Blue, -, -, , );
局部坐标系——只对某些图形进行变换,而其它图形元素不变。
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//客户区设置为白色
g.FillRectangle(Brushes.White, this.ClientRectangle); //y轴朝上
Matrix mat = new Matrix(, , , -, , );
g.Transform = mat; //移动坐标原点到窗体中心
Rectangle rect = this.ClientRectangle;
int w = rect.Width;
int h = rect.Height;
g.TranslateTransform(w/, -h/); //在全局坐标下绘制椭圆
g.DrawEllipse(Pens.Red, -, -, , );
g.FillRectangle(Brushes.Black, -, , , );
g.FillRectangle(Brushes.Black, , , , );
g.FillRectangle(Brushes.Black, , , , );
g.FillRectangle(Brushes.Black, , -, , ); //创建一个椭圆然后在局部坐标系中进行变换
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(-, -, , );
Matrix mat2 = new Matrix(); //平移
mat2.Translate(, ); //旋转
mat2.Rotate();
gp.Transform(mat2);
g.DrawPath(Pens.Blue, gp);
PointF[] p = gp.PathPoints;
g.FillRectangle(Brushes.Black, p[].X-, p[].Y+, , );
g.FillRectangle(Brushes.Black, p[].X-, p[].Y+, , );
g.FillRectangle(Brushes.Black, p[].X-, p[].Y-, , );
g.FillRectangle(Brushes.Black, p[].X-, p[].Y-, , );
gp.Dispose();
//base.OnPaint (e);
}
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, , , , ); //创建一个位图,其中两个位图之间有透明效果
Bitmap bm1 = new Bitmap(, );
Graphics bg1 = Graphics.FromImage(bm1);
SolidBrush redBrush = new SolidBrush(Color.FromArgb(, , , ));
SolidBrush greenBrush = new SolidBrush(Color.FromArgb(, , , ));
bg1.FillRectangle(redBrush, , , , );
bg1.FillRectangle(greenBrush, , , , );
g.DrawImage(bm1, , ); //创建一个位图,其中两个位图之间没有透明效果
Bitmap bm2 = new Bitmap(, );
Graphics bg2 = Graphics.FromImage(bm2);
bg2.CompositingMode = CompositingMode.SourceCopy;
bg2.FillRectangle(redBrush, , , , );
bg2.FillRectangle(greenBrush, , , , );
g.CompositingQuality = CompositingQuality.GammaCorrected;
g.DrawImage(bm2, , );
//base.OnPaint (e);
}
反走样
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//放大8倍
g.ScaleTransform(, );
//没有反走样的图形和文字
Draw(g);
//设置反走样
g.SmoothingMode = SmoothingMode.AntiAlias;
//右移40
g.TranslateTransform(, );
//再绘制就是反走样之后的了
Draw(g);
//base.OnPaint (e);
}
private void Draw(Graphics g)
{
//绘制图形和文字
g.DrawLine(Pens.Gray, , , , );
g.DrawEllipse(Pens.Gray, , , , );
string s = "反走样测试";
Font font = new Font("宋体", );
SolidBrush brush = new SolidBrush(Color.Gray);
g.DrawString(s, font, brush, , );
}
GDI+编程的10个基本技巧(转)的更多相关文章
- GDI编程
图形设备接口(GDI)是一个可执行程序,它接受Windows应用程序的绘图请求(表现为GDI函数调用),并将它们传给相应的设备驱动程序,完成特定于硬件的输出,象打印机输出和屏幕输出.GDI负责Wind ...
- GDI编程小结
图形设备接口(GDI)是一个可运行程序,它接受Windows应用程序的画图请求(表现为GDI函数调用),并将它们传给对应的设备驱动程序,完毕特定于硬件的输出,象打印机输出和屏幕输出.GDI负责Wind ...
- GDI+编程说明及小结
原文地址:http://blog.csdn.net/byxdaz/article/details/5972759 GDI+(Graphics Device Interface Plus图形设备接口加) ...
- GDI+编程小结
GDI+(Graphics Device Interface Plus图形设备接口加)是Windows XP和Windows Server 2003操作系统的子系统,也是.NET框架的重要组成部分,负 ...
- MFC控件GDI编程
MFC控件GDI编程 一丶学习内容 1.了解常用的GDI函数绘图. 2.使用常用的画笔画刷. 二丶常用的GDI函数绘图 上方则为我们常用的GDI函数了. 画线 矩形. 以及圆 等等. 2.1 画线代码 ...
- 10 个实用技巧,让 Finder 带你飞
Finder 是 Mac 电脑的系统程序,有的功能类似 Windows 的资源管理器.它是我们打开 Mac 首先见到的「笑脸」,有了它,我们可以组织和使用 Mac 里的几乎所有东西,包括应用程序.文件 ...
- [.net 面向对象编程基础] (10) 类的成员(字段、属性、方法)
[.net 面向对象编程基础] (10) 类的成员(字段.属性.方法) 前面定义的Person的类,里面的成员包括:字段.属性.方法.事件等,此外,前面说的嵌套类也是类的成员. a.类的成员为分:静态 ...
- Visual Studio 原生开发的10个调试技巧(二)
原文:Visual Studio 原生开发的10个调试技巧(二) 我以前关于 Visual Studio 调试技巧的文章引起了大家很大的兴趣,以至于我决定分享更多调试的知识.以下的列表中你可以看到写原 ...
- ★10 个实用技巧,让Finder带你飞~
10 个实用技巧,让 Finder 带你飞 Finder 是 Mac 电脑的系统程序,有的功能类似 Windows 的资源管理器.它是我们打开 Mac 首先见到的「笑脸」,有了它,我们可以组织和使用 ...
随机推荐
- playframework简单介绍
官方网站: https://www.playframework.com/documentation/2.5.x/Home 简介 编辑 Play!是一个full-stack(全栈的)Java Web应用 ...
- (转)WCF入门教程(一)简介
原文系列来自http://www.cnblogs.com/yank/p/3653160.html 1.WCF是什么? WCF( Windows Communication Foundation), 是 ...
- Ueditor使用方法
1.到百度下载文件,有各种版本.下载.net版本 2.将所需文件导入工程中 分别是:themes文件夹.third-party文件夹.ueditor.all.min.js.ueditor.config ...
- grunt之dev-pro环境切换
在项目开发过程中和发布阶段需要在开发环境(dev)和生产环境(pro)之间切换,静态文件引用的切换等等. 使用grunt要如何解决上述问题,这里提供一个案列供参考. 用到的grunt插件: 文件合并: ...
- Web项目中JSP页面的一种调试方法与出现的问题 -- SpringMVC架构测试
在前端开发中,尤其是MVC架构多人开发,负责前端的童鞋总是需要做静态页面,再和后台连接前无法使用变量如EL表达式等测试功能,所以本人引入了一个模板jsp数据测试专用文件,专门配置所有的变量,然后在待测 ...
- 练习使用css3实现3d按钮
网上有很多漂亮的用css3实现的3d按钮,如'这个'.‘糖果色按钮’, 今天练习了一下,喏,下面这样,兼容性不好. 小黑子小虎子 3d效果和发光效果都是利用box-shadow,也经常利用伪元素:af ...
- 今日分享一点干货。PHP中课程表的实现。
首先贴代码,代码贴完再细说: 前段HTML: <div id="studentRead" class="reading" style="z-in ...
- 校门外的树 - Grids2808
校门外的树 问题描述: 某校大门外长度为 L 的马路上有一排树,每两棵相邻的树之间的间隔都是1 米.我们 可以把马路看成一个数轴,马路的一端在数轴0 的位置,另一端在L 的位置:数轴上的每 个整数点, ...
- rsync相关整理
第一部分 rsync服务端配置 1.下载安装 a. yum安装. yum install rsync b. 下载rsync安装文件安装 #tar zxvf rsync-2.6.9 ...
- HTML&CSS基础学习笔记1.19-DIV标签1
div标签 这里我们要认识一下HTML里使用非常多的的一个标签:<div>. <div>标签定义文档中的分区或节(division/section),他可以把文档分割为独立的. ...