GDI+编程(画笔/画刷/路径/区域)
构造Graphics对象
Graphics类是GDI+程序设计的核心,Graphics类能够完成大部分的绘图,文本输出,几何图形的填充及坐标系统的转换等各种操作.在功能上,它与GDI的设备环境(DC)一致.
Graphics类有下列几种构造函数:
- static Graphics* FromHDC(HDG hdc);
- static Graphics* FromHDC(HDG hdc, HANDLE hDevice);
- static Graphics* FromHWND(HWND hWnd, BOOL icm);
- static Graphics* FromImage(Image* image);
hdc:设备环境句柄.
hWnd:窗口句柄.
icm:是否使用色彩配置文件校正色彩.
image:图像对象.
Hdevice:设备句柄.
--------------------------------------------------------------------------------------------------
画笔
GDI+通过Pen类来定义一个画笔,Pen类的构造函数有以下两种:
- Pen(color, width)
- Pen(brush, width)
第二种为使用指定的画刷来构造一个画笔.
画笔的线型
Pen类成员函数SetDashStyle(设置画笔线程)可以设置画笔的线型.GDI+在DashStyle(线程)枚举中提供了5个常见的画笔线型风格.
- enum DashStyle{
- DashStyleSolid = 0, // 实线
- DashStyleDash = 1, // 虚线
- DashStyleDot = 2, // 点线
- DashStyleDashDot = 3, // 点划线
- DashStyleDashDotDot = 4, // 双点划线
- DashStyleCustom = 5 // 自定义线(略...)
- };
画笔的对齐方式
Pen类成员函数SetAlignment(画笔对齐)可以设置画笔的对齐方式.PenAlignment枚举有两种对齐方式:
- enum PenAlignment{
- PenAlignmentCenter = 0, // 居中(默认)
- PenAlignmentInset = 1, // 嵌入
- };
画笔的缩放及旋转
例子:
- Pen pen(Color(255,0,0,0),4); // 定义一支宽度为4的画笔
- pen.ScaleTransform(1,6); // 将画笔垂直方向上扩充6倍,水平不变.
- graphics.DrawEllipse(&pen,10,10,100,100); // 画一个圆
- pen.RotateTransform(60,MatrixOrderAppend); // 将画笔旋转60度
- graphics.DrawEllipse(&pen,120,10,100,100); // 画一个圆
- pen.RotateTransform(60,MatrixOrderAppend); // 将画笔再旋转60度
- graphics.DrawEllipse(&pen,230,10,100,100); // 画一个圆
画笔的线帽属性
线帽(LineCap)就是线条首尾的外观,默认情况下为方形.设置线帽函数有两个:
- SetSTARTCap(起点线帽)
- SetEndCap(终点线帽)
不同外观的线帽由LineCap枚举列出:
- enum LineCap{
- LineCapFlat = 0,
- LineCapSquare = 1,
- LineCapRound = 2,
- LineCapTriangle = 3,
- LineCapNoAnchor = 0x10,
- LineCapSquareAnchor = 0x11,
- LineCapRoundAnchor = 0x12,
- LineCapDiamondAnchor = 0x13,
- LineCapArrowAnchor = 0x14,
- LineCapCustom = 0xff
- };
线帽图:http://photo.blog.sina.com.cn/photo/7c773cc5gbcfd89aa2dcc
直线的连接点属性
直线连接点是两条端点重合或重叠的直线形成的公共区域.
函数为SetLineJoin(连接点),连接点LineJoin枚举为:
- enum LineJoin{
- LineJoinMiter = 0, // 斜接
- LineJoinBevel = 1, // 斜切
- LineJoinRound = 2, // 圆形
- LineJoinMiterClipped = 3, // 剪裁斜接
- };
--------------------------------------------------------------------------------------------------
直线
6种绘制直线函数:
- DrawLine(Pen* pen, Point& pt1, Point& pt2)
- DrawLine(Pen* pen, PointF& pt1, PointF& pt2)
- DrawLine(Pen* pen, INT x1, INT y1, INT x2, INT y2)
- DrawLine(Pen* pen, REAL x1, REAL y1, REAL x2, REAL y2)
- DrawLines(Pen* pen, Point* points, INT count)
- DrawLines(Pen* pen, PointF* points, INT count)
pt1,pt2:线段起止点位置.
x1,y1,x2,y2:线段起止点坐标.
points:直线端点数组.
count:线段定义点总数.
例子:
- Graphics graphics(hWnd); // 在窗口中创建Graphics对象
- Pen blackPen(Color(255,0,0,0),3); // 创建画笔
- graphics.DrawLine(&blackPen,10,10,100,100); // 绘制线条
-------------------------------------------------
矩形
6种绘制矩形函数:
- DrawRectangle(Pen* pen, Rect& rect)
- DrawRectangle(Pen* pen, RectF& rect)
- DrawRectangle(Pen* pen, INT x, INT y, INT width, INT height)
- DrawRectangle(Pen* pen, REAL x, REAL y, REAL width, REAL height)
- DrawRectangles(Pen* pen, Rect& rect, INT count)
- DrawRectangles(Pen* pen, RectF& rect, INT count)
-------------------------------------------------
椭圆
4种绘制椭圆函数:
- DrawEllipse(Pen* pen, Rect& rect)
- DrawEllipse(Pen* pen, RectF& rect)
- DrawEllipse(Pen* pen, INT x, INT y, INT width, INT height)
- DrawEllipse(Pen* pen, REAL x, REAL y, REAL width, REAL height)
-------------------------------------------------
简单曲线和贝塞尔曲线
普通曲线分DrawCurve和DrawClosedCurve(封闭曲线)两种.
常见的调用方法:
- DrawCurve(Pen* pen, Point* points, INT count)
- DrawCurve(Pen* pen, Point* points, INT count, REAL tension)
tension:曲线的弯曲强度,弯曲强度越小,曲线的弯曲程度也越小.
例子:
- Graphics graphics(hWnd);
- Pen blackPen(Color(255,0,0,0),3);
- Point point1(50, 50);
- Point point2(200, 50);
- Point point3(80, 150);
- Point point4(125, 0);
- Point point5(155, 150);
- Point point6(50, 50);
- Point points[6] = {point1,point2,point3,point4,point5,point6};
- graphics.DrawCurve(&blackPen,points,6);
贝塞尔曲线(由1个起点2个控制点和1点终点组成):
- DrawBezier(Pen* pen, Point& pt1, Point& pt2, Point& pt3, Point& pt4)
-------------------------------------------------
多边形
3条或更多条直边组成的闭合图形,函数如下:
- DrawPolygon(Pen* pen, Point* points, INT count)
- DrawPolygon(Pen* pen, PointF* points, INT count)
-------------------------------------------------
孤线
- DrawArc(Pen* pen, Rect& rect, REAL startAngle, REAL sweepAngle)
- DrawArc(Pen* pen, RectF& rect, REAL startAngle, REAL sweepAngle)
- DrawArc(Pen* pen, INT x, INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
- DrawArc(Pen* pen, REAL x, REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
startAngle:从x轴到孤线的起始点沿顺时针方向度量的角(以度为单位).
sweepAngle:从startAngle参数到孤线的结束点沿顺时针方向度量的角(以度为单位).
-------------------------------------------------
扇形
- DrawPie(Pen* pen, Rect& rect, REAL startAngle, REAL sweepAngle)
- DrawPie(Pen* pen, RectF& rect, REAL startAngle, REAL sweepAngle)
- DrawPie(Pen* pen, INT x, INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
- DrawPie(Pen* pen, REAL x, REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
--------------------------------------------------------------------------------------------------
画刷
通过SolidBrush类来定义一个单色画刷,只有一种构造函数:
- SolidBrush greenBrush(Color(255,0,255,0));
使用单色画刷填充图形区域时,GDI+提供了以下几种填充函数:
- FillClosedCurve:填充闭合曲线.
- FillEllipse:填充椭圆.
- FillPath:填充路径.
- FillPie:填充扇形.
- FillPolygon:填充多边形.
- FillRectangle:填充矩形.
- FillRectangles:填充矩形集.
- FillRegion:填充区域.
上述8个填充函数同样适用于其他类型的画刷.
-------------------------------------------------
影线画刷
通过HatchBrush类来定义一个影线画刷,其构造函数为:
- HatchBrush(HatchStyle hatchStyle, const Color& foreColor, const Color& backColor);
hatchStyle:影线画刷的类型,HatchStyle枚举提供了53种风格的影线画刷类型.
foreColor:影线画刷线条的前景色.
backColor:影线画刷线条的背景色.
例子:
- Color black(255,0,0,0); // 前景为黑色.
- Color white(255,255,255,255); // 背景为白色.
- for(int i=0;i<53;i++)
- HatchBrush brush(HatchStyle(i), black, white); // 创建53种影线画刷
画刷绘制原点
- SetRenderingOrigin(INT x, INT y);
-------------------------------------------------
纹理画刷
创造一个纹理画刷,需要进行以下3步工作.
(1)定制纹理画刷使用的基本图案(图形).
(2)设定画刷图片的大小.
(3)确定基本图案的排列方式.
使用TextureBrush类来构造纹理画刷.TextureBrush类的构造函数有以下7种:
- TextureBrush(Image* image, Rect& dstRect, ImageAttributes* imageAttributes)
- TextureBrush(Image* image, RectF& dstRect, ImageAttributes* imageAttributes)
- TextureBrush(Image* image, WrapMode wrapMode)
- TextureBrush(Image* image, WrapMode wrapMode, Rect& dstRect)
- TextureBrush(Image* image, WrapMode wrapMode, RectF& dstRect)
- TextureBrush(Image* image,WrapMode wrapMode,INT dstX,INT dstY,INT dstWidth,INT dstHeight)
- TextureBrush(Image* image,WrapMode wrapMode,REAL dstX,REAL dstY,REAL dstWidth,REAL dstHeight)
image:纹理画刷使用的基本图案.
dstRect:用于指定图案中用于画刷中的矩形区域.它的大小不能超过基本图案的范围.
wrapMode:指定在画刷中如何排列基本图案.
imageAttributes:用于指定基本图案的附加特征参数.
对画刷图像缩放为1.5倍:
- SetTransform(&Matrix(1.5f,0.0f,0.0f,1.5f,0.0f,0.0f));
纹理画刷的排列方式
GDI+的WrapMode枚举提供了5种不同的图案排列方式:
- enum WrapMode{
- WrapModeTile = 0, // 简单的平铺方式(默认)
- WrapModeTileFlipX = 1, // 每隔一列的图像会左右翻转
- WrapModeTileFlipY = 2, // 每隔一行的图像会上下翻转
- WrapModeTileFlipXY = 3, // 在水平和垂直方向上同时翻转图片
- WrapModeClamp = 4 // 不平铺,只包含一幅位于左上角的图像
- };
可以在构造函数中指定,也可以通过SetWrapMode(排列方式)函数修改画刷的图片排列方式.
纹理画刷的变换
- RotateTransform(REAL angle); // 旋转变换(度)
- ScaleTransform(REAL sx,REAL sy); // 缩放变换(倍)
- TranslateTransform(REAL dx,REAL dy); // 平移变换(像素)
-------------------------------------------------
线性渐变画刷
线性渐变画刷LinearGradientBrush类的构造函数有以下6种:
- LinearGradientBrush(Point& point1, Point& point2, Color& color1, Color& color2)
- LinearGradientBrush(PointF& point1, PointF& point2, Color& color1, Color& color2)
- LinearGradientBrush(Rect& rect,Color& color1,Color& color2,REAL angle,BOOL isAngleScalable)
- LinearGradientBrush(RectF& rect,Color& color1,Color& color2,REAL angle,BOOL isAngleScalable)
- LinearGradientBrush(Rect& rect ,Color& color1, Color& color2, LinearGradientMode mode)
- LinearGradientBrush(RectF& rect ,Color& color1, Color& color2, LinearGradientMode mode)
point1,point2:起点,终点的坐标.
color1,color2:起点,终点的色彩.
rect:画刷的矩形定义区间.
isAngleScalable:画刷是否需要旋转.
angle:旋转角度,isAngleScalable为真时,该参数才有效.
mode:渐变线的填充方向.
线性渐变画刷填充方式
与纹理画刷的排列方式SetWrapMode(排列方式)相似.
多色线性渐变画刷的实现
- SetInterpolationColors(Color* presetColors, Real* blendPositions, INT count);
presetColors:包含多种渐变色的数组.第一及最后一个变量定义了渐变的起止点色彩.
blendPositions:包含色彩合成位置的数组.第一及最后一个变量必须是0.0f(0%)和1.0f(100%).
count:参与渐变的色彩总数.
3色渐变例子:
- LinearGradientBrush brush(Point(0,0),Point(50,50),Color(255,0,0,0),Color(255,255,255,255));
- Color color[3] = {Color(255,255,0,0),Color(255,0,255,0),Color(255,0,0,255)};
- REAL real[3] = {0.0f,0.3f,1.0f};
- brush.SetInterpolationColors(color,real,3);
- graphics.FillRectangle(&brush,0,0,200,200);
-------------------------------------------------
路径渐变画刷
路径渐变画刷PathGradientBrush类的构造函数有以下3种:
- PathGradientBrush(GraphicsPath* path)
- PathGradientBrush(Point* points, INT count, WrapMode wrapMode)
- PathGradientBrush(PointF* points, INT count, WrapMode wrapMode)
path:由GraphicsPath类定义的路径.
points:用数组形式表示的组成路径的各个端点的坐标.
count:构造路径的端点总数.
wrapMode:渐变画刷在目标区域中的排列方式.
同线性渐变画刷相比,路径渐变画刷的渐变方向是从路径中央到路径边缘呈发散壮的渐变.所以,除了指定路径对象外,还要指定路径的中心点色彩和路径的边界色彩.
4边形例子:
- Point points[]={Point(0,0),Point(100,0),Point(100,100),Point(0,100)};
- GraphicsPath path;
- path.AddLines(points, 4);
- // 构造路径渐变画刷
- PathGradientBrush PGBrush(&path);
- // 设置中心点色彩
- PGBrush.SetCenterColor(Color(255,255,0,0));
- // 设置每个端点的色彩
- Color colors[]={Color(255,0,0,255),Color(255,0,0,255),Color(255,0,0,255),Color(255,0,0,255)};
- int count = 4;
- PGBrush.SetSurroundColors(colors, &count);
- // 填充目标路径
- graphics.FillPath(&PGBrush, &path);
更改路径渐变画刷的中心点
默认情况下,路径的中心点是定义路径的各个点坐标的平均值.调用PathGradientBrush类的成员函数SetCenterPoint可以对路径渐变画刷的中心点进行调整.
圆形例子:
- GraphicsPath path;
- path.AddEllipse(0,0,200,200);
- // 构造路径渐变画刷
- PathGradientBrush PGBrush(&path);
- // 设置中心点色彩
- PGBrush.SetCenterColor(Color(255,255,255,255));
- // 设置每个端点的色彩
- Color colors[]={Color(255,0,0,0)};
- int count = 1;
- PGBrush.SetSurroundColors(colors, &count);
- // 设置中心点
- PGBrush.SetCenterPoint(Point(50,50));
- // 填充目标路径
- graphics.FillEllipse(&PGBrush, 0,0,200,200);
多色路径渐变与多色线性渐变方法一样.
路径渐变的3种变换与"纹理画刷的变换"一样.
--------------------------------------------------------------------------------------------------
输出文本
- DrawString(string, length, font, layoutRect, stringFormat, brush)
- DrawString(string, length, font, origin, brush)
- DrawString(string, length, font, origin, stringFormat, brush)
string:输出的文本内容,双字节型.
length:文本长度.该值如果设成-1,表明string的最后一个字符是NULL.
font:字体.
layoutRect:文本输出的矩形区域.
origin:文本输出的起点,类型为PointF.
stringFormat:文本输出格式.
brush:输出文本时使用的画刷.
例子:
- Graphics graphics(hWnd);
- // 双字节数组
- WCHAR string[20];
- wcscpy(string,L"文本");
- // 字体
- Gdiplus::Font font(L"宋体",16);
- // 文本输出的矩形区域
- RectF rect(0.0f,0.0f,200.0f,50.0f);
- // 文本输出格式
- StringFormat format;
- format.SetAlignment(StringAlignmentNear);
- // 文本画刷
- SolidBrush brush(Color(255,0,0,0));
- // 文本显示
- graphics.DrawString(string,-1,&font,rect,&format,&brush);
测量字符串(略)
分栏显示文本(略)
字符串的去尾(略)
文本的剪裁输出(略)
测量文本的局部输出区域(略)
格式化文本输出(略)
控制文本输出方向(略)
设置文本对齐方式(略)
使用制表位(略)
显示快捷键前导字符(略)
--------------------------------------------------------------------------------------------------
路径
GDI+使用GraphicsPath类来定义路径,有3种构造函数:
- GraphicsPath(fillMode)
- GraphicsPath(Point* points, BYTE* types, INT count, FillMode fillMode)
- GraphicsPath(PointF* points, BYTE* types, INT count, FillMode fillMode)
fillMode:填充模式.该参数为FillMode枚举列出的两个成员FillModeAlternate,FillModeWinding之一.
points:定义子路径的点.
types:points数组成员的类型描述.这些点的类型有PathPointType枚举列出的点,直线的端点,曲线的端点,控制点3种.
- enum PathPointType{
- PathPointTypeStart = 0, // 子路径的起点
- PathPointTypeLine = 1, // 直线的起点或终点
- PathPointTypeBezier = 3, // 贝塞尔曲线的端点或控制点
- PathPointTypePathTypeMode = 0x07, // 路径点类型掩码
- PathPointTypeDashMode = 0x10, //
- PathPointTypePathMarker = 0x20, // 标记点
- PathPointTypeCloseSubpath = 0x80, // 封闭子路径的最后一点
- PathPointTypeBezier3 = 3, //
- };
向路径中添加几何图形
路径轮廓的定义除了可以在构造函数中通过定义端点坐标及类型实现之外,还可以在路径建立完成后,通过向路径追加线条或几何图形的方式实现.
- 成员函数名 函数功能
- AddArc // 添加孤线
- AddBezier,AddBeziers // 添加贝塞尔曲线
- AddClosedCurve // 添加封闭曲线
- AddCurve // 添加曲线
- AddEllipse // 添加椭圆
- AddLine,AddLines // 添加直线
- AddPath // 添加路径
- AddPie // 添加扇形
- AddPolygon // 添加多边形
- AddRectangle,AddRectangles // 添加矩形
- AddString // 添加字符串
开放图形与封闭图形
- CloseFigure() // 封闭图形
- CloseAllFigures() // 封闭所有图形
添加子路径(略)
提取子路径的信息(略)
--------------------------------------------------------------------------------------------------
区域
--------------------------------------------------------------------------------------------------
转自:http://blog.sina.com.cn/s/blog_7c773cc50100yvye.html
GDI+编程(画笔/画刷/路径/区域)的更多相关文章
- VS2010/MFC编程入门之五十一(图形图像:GDI对象之画刷CBrush)
上一节中鸡啄米主要讲的是画笔CPen的用法,前面也说了,GDI对象中最常用的就是画笔和画刷,本节就讲讲画刷CBrush. 鸡啄米依然是通过实例的方式来说明画刷的用法.此实例要实现的功能是,对话框上有一 ...
- VS2010-MFC(图形图像:GDI对象之画刷CBrush)
转自:http://www.jizhuomi.com/software/248.html 上一节讲的是画笔CPen的用法,前面也说了,GDI对象中最常用的就是画笔和画刷,本节就讲讲画刷CBrush. ...
- [游戏模版3] Win32 画笔 画刷 图形
>_<:introduce the functions of define\create\use pen and brush to draw all kinds of line and s ...
- {Reship}{C#}{GDI+}GDI+画笔,线,区域类型
=================================================================================== This article is ...
- 简述WPF中的画刷(Brush)
原文:简述WPF中的画刷(Brush) -------------------------------------------------------------------------------- ...
- WPF 动画(形状、画刷)
一:形状 在WPF用户界面中,可以通过形状(Shape)来绘制直线.椭圆.矩形及一些多边形的类.通过这些基本的图像,组合成为复杂的图形. Shape类中,主要的形状有Rectangle(),Ellip ...
- 深入WPF中的图像画刷(ImageBrush)之2——ImageBrush的铺设方式
原文:深入WPF中的图像画刷(ImageBrush)之2--ImageBrush的铺设方式 ------------------------------------------------------ ...
- 【WPF学习】第四十章 画刷
画刷填充区域,不管是元素的背景色.前景色以及边框,还是形状的内部填充和笔画(Stroke).最简单的画刷类型是SolidColorBrush,这种画刷填充一种固定.连续的颜色.在XAML中设置形状的S ...
- C# GDI绘图之——画笔和画刷
绘制图形需要画笔和画刷: Pen(画笔类): Pen为C#编程语言中专门的画笔类 使用方式: // 用系统颜色来初始化我们的画笔类,使用Color静态类中的颜色 1. Pen p1 = new Pen ...
随机推荐
- unittest单元测试框架简单说明
unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果.今天笔者 ...
- Webflux快速入门
SpringWebflux是SpringFramework5.0添加的新功能,WebFlux本身追随当下最火的Reactive Programming而诞生的框架,那么本篇就来简述一下这个框架到底是做 ...
- Windows下的Jdk 1.7*安装并配置(图文详解)
不多说,直接上干货! 很多人很少去想,为什么在windows下,安装完Jdk的安装包之后,还需要去配置环境变量,只是知道要这么去做,没有想过为什么要去这么做? 答:由于java是平台无关的 ,安装jd ...
- Python -- 多媒体编程 -- 音乐播放
使用win32库的WMPlayer.OCX开发一个简易的音乐播放器 import sys from PyQt4 import QtGui, QtCore from win32com.client im ...
- 使用秘钥ssh登录远程服务器
一.使用公钥远程登录ssh服务器方式 1.1 在客户端使用ssh-keygen 生成密匙 steven:~ admin$ ssh-keygen //客户端生成秘钥 Generating public ...
- mac 比较两个文件
比较两个文件,输出两个文件都有的行,可以 1.使用comm命令 如下例: ------------------->$ cat 1s1.txt line line line line line l ...
- a no-risk path to IEEE P1687
You’ve heard all about IJTAG (IEEE P1687) [1,2,3], a new standard for accessing embedded test and d ...
- 第2章 Selenium2-java 测试环境搭建
2.1 Window下环境搭建 2.1.1 安装Java 2.1.2 安装Eclipse (网上资源很多,就不详将了). 2.1.3 下载Java版的Selenium包. 下载地址:http://d ...
- 文档对象模型DOM(一)
当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. HTML DOM 树 每加载一个标记 注释 或者属性,就将其当 ...
- Java 8 新特性-菜鸟教程 (2) -Java 8 方法引用
Java 8 方法引用 方法引用通过方法的名字来指向一个方法. 方法引用可以使语言的构造更紧凑简洁,减少冗余代码. 方法引用使用一对冒号 :: . 下面,我们在 Car 类中定义了 4 个方法作为例子 ...