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+2; 

y = this.Top+2; 

w = this.Width-4; 

h = this.Height-4; 

Pen pen = new Pen(Color.Red, 2); 

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, 10, 20, 50, 100); 

} 

和文本有关的三个类: 

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, 20, FontStyle.Bold, GraphicsUnit.Point); 

RectangleF rectF = new RectangleF(30, 20, 180, 205); 

StringFormat sf = new StringFormat(); 

SolidBrush sbrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255)); 

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(20, 20, 300, 200); 

gp.AddPie(50, 100, 300, 100, 45, 200); 

gp.AddRectangle(new Rectangle(100, 30, 100, 80)); 

//在绘图表面上绘制绘图路径 

g.DrawPath(Pens.Blue, gp); 

//平移 

g.TranslateTransform(200, 20); 

//填充绘图路径¶ 

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(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);

}

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, 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);

}

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

一下程序将原定移动到控件中心,并且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);

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

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);

}

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);

}

反走样

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);

}

时间: 2024-08-01 22:42:35

GDI+编程的10个基本技巧(转)的相关文章

程序猿编程的10个有用技巧

以下是我自己在工作中学到的一些简单的技巧和策略,希望能对各位实用. 1. 橡皮鸭调试法 不知道各位有没有这样的经历.当你正在给别人描写叙述问题的时候,突然灵机一动想到了解决方式?这样的情况的产生是有科学根据的.高谈阔论能让我们的大脑又一次有条理地组织问题.在这样的情况下,你的聊天对象就是"橡皮鸭".团队中的每一个人都应该积极主动乐意地成为彼此的"橡皮鸭". 有时候,假设你幸运的话,你的"橡皮鸭"搞不好还能给出有效的建议呢. 2. 高速信息反馈 一

程序员编程的10个实用技巧

下面是我自己在工作中学到的一些简单的技巧和策略,希望能对各位有用. 1.橡皮鸭调试法 不知道各位有没有这样的经历,当你正在给别人描述问题的时候,突然灵机一动想到了解决方案?这种情况的产生是有科学依据的.高谈阔论能让我们的大脑重新有条理地组织问题.在这种情况下,你的聊天对象就是“橡皮鸭”.团队中的每个人都应该积极主动乐意地成为彼此的“橡皮鸭”.有时候,如果你幸运的话,你的“橡皮鸭”搞不好还能给出有效的建议呢. 2.快速信息反馈 一旦写好代码就要尽快得到反馈.当你和我们在HubSpot一样,收到大量

GDI编程小结

图形设备接口(GDI)是一个可运行程序,它接受Windows应用程序的画图请求(表现为GDI函数调用),并将它们传给对应的设备驱动程序,完毕特定于硬件的输出,象打印机输出和屏幕输出.GDI负责Windows的全部图形输出,包含屏幕上输出像素.在打印机上输出硬拷贝以及绘制Windows用户界面.也就是Windows的图形编程. 一.GDI体系结构 1.  GDI32.DLL导出的函数 GDI提供几百个Windows程序中能够调用的函数.这些函数大多数是从Win32的子系统DLL GDI32.DLL

程序员在编程开发中的10个实用技巧

程序员在编程开发中的10个实用技巧,下面是我自己在工作中学到的一些简单的技巧和策略,希望能对各位有用.1.     橡皮鸭调试法不知道各位有没有这样的经历,当你正在给别人描述问题的时候,突然灵机一动想到了解决方案?这种情况的产生是有科学依据的.高谈阔论能让我们的大脑重新有条理地组织问题.在这种情况下,你的聊天对象就是“橡皮鸭”.团队中的每个人都应该积极主动乐意地成为彼此的“橡皮鸭”.有时候,如果你幸运的话,你的“橡皮鸭”搞不好还能给出有效的建议呢.2.     快速信息反馈一旦写好代码就要尽快得

GDI+编程小结

GDI+(Graphics Device Interface Plus图形设备接口加)是Windows XP和Windows Server 2003操作系统的子系统,也是.NET框架的重要组成部分,负责在屏幕和打印机上绘制图形图像和显示信息. GDI+不但在功能上比GDI 要强大很多,而且在代码编写方面也更简单,因此会很快成为Windows图形图像程序开发的首选. 一.              GDI+的特点和新增功能 GDI+与GDI一样,都具有设备无关性.应用程序的程序员可利用GDI+这样

Win32中GDI+应用(五)--GDI与GDI+编程模型的区别

在GDI里面,你要想开始自己的绘图工作,必须先获取一个device context handle,然后把这个handle作为绘图复方法的一个参数,才能完成任务.同时,device context handle是同一定的绘图属性绑定在一起的,诸如画笔.话刷等等,你必须在画线之前创建自己的画笔,然后使用selectObject方法把这个画笔同已经获取的device context handle绑定,才能使用LineTo等方法开始画线.不然,你画出来的线使用的是默认的属性:宽度(1),颜色(黑色).但

C#GDI+编程基础(二)

pen类:绘制指定宽度和样式的直线.使用DashStyle属性绘制几种虚线,可以使用各种填充样式(包括纯色和纹理)来填充Pen绘制的直线,填充模式取决于画笔或用作填充对象的纹理. 创建画笔: //用指定的颜色实例化一只画笔的方法如下: public Pen(Color); //用指定的画刷实例化一只画笔的方法如下: public Pen(Brush); //用指定的画刷和宽度实例化一只画笔的方法如下: public Pen(Brush , float); //用指定的颜色和宽度实例化一只画笔的方

提升编程能力的11个技巧

提升编程能力的11个技巧 1.首先仔细分析问题 2.接着好好想想如何解决这个问题 3.收集整理所有需求 花点时间将最终产品要实现的目标写下来,并且明确哪些是我们的目标用户群.如果这一步能做好的话,将会给后面节约大量的时间,正所谓磨刀不误砍柴工. 4.写一个全面的实施计划(或模型) 如果是个小项目,这一步出来的可能只是一个基本的流程或者一个简单的等式. 如果是个比较大的项目,这一步有助于我们将它切割成几个模块,然后再按下面的问题思考: ●各个模块需要执行什么任务 ●模块之间如何传递数据 ●如何调用

VC.NET的GDI+编程入门教程之图形

基于直线的图形 一.等边图形 (一)长方形和正方形 长方形是由四条边组成的具有四个直角的几何图形,为了绘制一个长方形,可以定义围成长方形的矩形值,或定义它的位置和尺寸.为了画一个矩形围成的长方形,可以使用Graphics::DrawRectangle()方法. public: void DrawRectangle(Pen *pen, Rectangle rect); 类似的长方形可以按照如下说明: 图一.长方形说明图示 定义过一个矩形变量后,可以将它传递给上述的方法,例子代码如下: privat