GDI+的基本使用

Graphics g = e.Graphics; // 创建画板
Pen p = new Pen(Color.Blue, 1); // 画笔颜色及宽度

g.DrawLine(p, 10, 10, 110, 110); // 绘制直线
g.DrawLine(p, 210, 10, 110, 110);
g.DrawRectangle(p, 10, 10, 100, 100); // 绘制矩形
g.DrawRectangle(p, 110, 10, 100, 100);
g.DrawEllipse(p, 10, 10, 200, 100); // 根据外接矩形绘制椭圆

g.Dispose();
p.Dispose();

Result:

Pen的属性主要有: Color(颜色),DashCap(短划线终点形状),DashStyle(虚线样式),EndCap(线尾形状), StartCap(线头形状),Width(粗细)等。

Graphics g = e.Graphics; // 创建画板
Pen p = new Pen(Color.Blue, 5); // 画笔颜色及宽度

p.DashStyle = DashStyle.Dot; // 定义虚线的样式为点
g.DrawLine(p, 10, 10, 200, 10);

p.DashPattern = new float[] { 2, 1 }; // 设置短划线和空白部分的数组
g.DrawLine(p, 10, 20, 200, 20);

p.DashStyle = DashStyle.Solid; // 恢复实线
p.EndCap = LineCap.ArrowAnchor; // 实现箭头,只对非封闭线有效
g.DrawLine(p, 10, 30, 200, 30);

g.Dispose();
p.Dispose();

Result:

下面简单实现一下Brush的使用,

Graphics g = this.CreateGraphics();
Rectangle rect = new Rectangle(10, 10, 50, 50);//定义矩形,参数为起点横纵坐标以及其长和宽

SolidBrush b1 = new SolidBrush(Color.Blue);//定义单色画刷          
g.FillRectangle(b1, rect); //填充矩形

g.DrawString("字符串", new Font("宋体", 10), b1, new PointF(90, 10)); // 字符串

TextureBrush b2 = new TextureBrush(Image.FromFile(@"D:\b.jpg")); // 使用图片填充
rect.Location = new Point(10, 70); // 更改矩形的起点坐标
rect.Width = 200; // 更改矩形的宽
rect.Height = 200; // 更改矩形的高
g.FillRectangle(b2, rect);

rect.Location = new Point(230, 10);
LinearGradientBrush b3 = new LinearGradientBrush(rect, Color.Yellow, Color.Red, LinearGradientMode.Horizontal); // 用渐变色填充
g.FillRectangle(b3, rect);

Result:

下面实现对坐标轴的变换,默认坐标轴原点为左上角,正X轴向右,正Y轴向下。

using (Graphics g = this.CreateGraphics())
{
    Pen p = new Pen(Color.OrangeRed, 1);

    //转变坐标轴角度
    for (float i = 1; i < 90; i += 0.1f)
    {
        g.RotateTransform(i); // 每旋转0.1度画一条线
        g.DrawLine(p, 0, 0, i, 0);
    }
    g.ResetTransform();

    g.TranslateTransform(100, 100); // 平移坐标轴
    g.DrawLine(p, 0, 0, 100, 0);
    g.ResetTransform();

    g.TranslateTransform(100, 200); // 先平移到指定坐标,然后进行度旋转
    for (int i = 0; i < 8; i++)
    {
        g.RotateTransform(45);
        g.DrawLine(p, 0, 0, 100, 0);
    }
}

Result:

GDI+ 图形设备接口普拉斯(Graphic Device Interface Plus)属于非托管资源,应手动释放。

转载于:https://www.cnblogs.com/jizhiqiliao/p/9962214.html

Visual C++6.0使用GDI+的一般方法 1. 载解压GDI+开发包; 2. 正确设置include & lib 目录; 3. stdafx.h 添加: #ifndef ULONG_PTR #define ULONG_PTR unsigned long* #endif #include 4. 程序中添加GDI+的包含文件gdiplus.h以及附加的类库gdiplus.lib。 通常gdiplus.h包含文件添加在应用程序的stdafx.h文件中,而gdiplus.lib可用两种进行添加: 第一种是直接在stdafx.h文件中添加下列语句: #pragma comment( lib, "gdiplus.lib" ) 另一种方法是: 在VC.net中添加库文件在:项目菜单->属性->链接器->输入 举个例子: (1)在应用程序项目的应用类中,添加一个成员变量,如下列代码: ULONG_PTR m_gdiplusToken; 其中,ULONG_PTR是一个DWORD数据类型,该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标识,以便能在应用程序退出后,引用该标识来调用Gdiplus:: GdiplusShutdown来关闭GDI+。 (2)在应用类中添加ExitInstance的重载,并添加下列代码用来关闭GDI+: int CGDITestApp::ExitInstance() { Gdiplus::GdiplusShutdown(m_gdiplusToken); return CWinApp::ExitInstance(); } (3)在应用类的InitInstance函数中添加GDI+的初始化代码: 注意:下面这些GDI+的初始化代码必须放在m_pMainWnd->UpdateWindow();之前。 CWinApp::InitInstance(); Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); (4)在需要绘图的窗口或视图类中添加GDI+的绘制代码。 下面分别就单文档和基于对话框应用程序为例,说明使用GDI+的一般过程和方法。 1. 在单文档应用程序中使用GDI+ 在上面的过程中,我们就是以一个单文档应用程序Ex_GDIPlus作为示例的。下面列出第4步所涉及的代码: void CGDITestView::OnDraw(CDC* pDC) { CGDITestDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here usingnamespace Gdiplus; Graphics graphics(pDC->m_hDC); Pen newPen(Color(255,0,0),3); HatchBrush newBrush(HatchStyleCross,Color(255,0,255,0),Color(255,0,0,255));//创建一个填充画刷,前景色为绿色,背景色为蓝色 graphics.DrawRectangle(&newPen,50,50,100,60);// 在(50,50)处绘制一个长为100,高为60的矩形 graphics.FillRectangle(&newBrush,50,50,100,60); // 在(50,50)处填充一个长为100,高为60的矩形区域 } 编译并运行,结果如图:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值