Linq的链式编程用起来总是那样畅快淋漓,可惜在C#中并不是每时每刻都能有这么畅快的感觉,其中使用Graphics的时候就是,每次用Graphics绘制大量图形时尤其如此。GDI+的API功能很强大,但是在实际编码中,很多重复性的工作总是让我感觉到用起来很繁琐,于是我就设计了这样一个类库,将C#中的Graphics类进行了二次封装,让其可以和Linq一样,用起来“如沐春风”。

先来看一段简单的示例代码吧。下面代码就是在一个窗体上绘制一系列图形,可以看出和原来的Graphics相比,编码量更小,代码也更优雅。

 private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Ex()
.DrawLine(, , , )
.DrawLine(, , , ,Pens.Red)
.DrawLine(, , , )
.DrawLine(, , , , new Pen(Color.Blue,3f))
.DrawLine(, , , )
.DrawRectangle(, , , )
.FillRectangle(, , , , Brushes.Red)
.DrawEllipse(, , , , new Pen(Color.Yellow, 3f))
.FillEllipse(, , , ,Brushes.Green)
.DrawString("haha",new PointF(200f,200f))
.DrawString("leilei", new PointF(100f, 200f),new Font("微软雅黑",30f));
}

画出来的效果如下:

下面就是我对Graphics二次封装的具体代码,目前还只能绘制Line、Rectangle、Ellipse和string

 public static class GDIEx
{
public static GraphicsEx Ex(this Graphics g)
{
return new GraphicsEx(g);
}
}
public class GraphicsEx : IDisposable
{
readonly Graphics g;
Pen pen = Pens.Black;
Brush brush = Brushes.Black;
Font font = new Font(FontFamily.GenericSerif,);
internal GraphicsEx(Graphics g)
{
this.g = g;
}
public void Dispose()
{
g.Dispose();
pen = null;
brush = null;
font = null;
}
public GraphicsEx DrawLine(int x1, int y1, int x2, int y2,[Optional]Pen pen)
{
if (pen != null)
this.pen = pen;
g.DrawLine(this.pen, x1, y1, x2, y2);
return this;
}
public GraphicsEx DrawLine(Point p1, Point p2,[Optional]Pen pen)
{
if (pen != null)
this.pen = pen;
g.DrawLine(this.pen, p1, p2);
return this;
}
public GraphicsEx DrawRectangle(Rectangle rect,[Optional]Pen pen)
{
if (pen != null)
this.pen = pen;
g.DrawRectangle(this.pen, rect);
return this;
}
public GraphicsEx DrawRectangle(int left,int top,int width,int height,[Optional]Pen pen)
{
if(pen != null)
this.pen = pen;
g.DrawRectangle(this.pen, left, top, width, height);
return this;
}
public GraphicsEx FillRectangle(Rectangle rect, [Optional]Brush brush)
{
if(brush != null)
this.brush = brush;
g.FillRectangle(this.brush, rect);
return this;
}
public GraphicsEx FillRectangle(int left, int top, int width, int height, [Optional]Brush brush)
{
if (brush != null)
this.brush = brush;
g.FillRectangle(this.brush, left, top, width, height);
return this;
}
public GraphicsEx DrawEllipse(Rectangle rect, [Optional]Pen pen)
{
if (pen != null)
this.pen = pen;
g.DrawEllipse(this.pen, rect);
return this;
}
public GraphicsEx DrawEllipse(int left, int top, int width, int height, [Optional]Pen pen)
{
if (pen != null)
this.pen = pen;
g.DrawEllipse(this.pen, left, top, width, height);
return this;
}
public GraphicsEx FillEllipse(Rectangle rect, [Optional]Brush brush)
{
if (brush != null)
this.brush = brush;
g.FillEllipse(this.brush, rect);
return this;
}
public GraphicsEx FillEllipse(int left, int top, int width, int height, [Optional]Brush brush)
{
if (brush != null)
this.brush = brush;
g.FillEllipse(this.brush, left, top, width, height);
return this;
}
public GraphicsEx DrawString(string str, RectangleF rect,[Optional]Font font, [Optional]Brush brush)
{
if (font != null)
this.font = font;
if (brush != null)
this.brush = brush;
g.DrawString(str, this.font, this.brush, rect);
return this;
}
public GraphicsEx DrawString(string str, PointF p, [Optional]Font font, [Optional]Brush brush)
{
if (font != null)
this.font = font;
if (brush != null)
this.brush = brush;
g.DrawString(str, this.font, this.brush, p);
return this;
}
}

封装思想其实比较简单,封装的主体就是类GraphicsEx,该类根据构造函数中传入的Graphics进行绘图,并且绘制函数的签名尽量和Graphics的接口保持一致,以增加易用性,并且每个绘制函数都会返回实例本身,以供不断的调用。

所有的画笔、画刷或者其它与绘制内容无关的绘制参数都采用可选参数,这样做的目的很简单,从文章开始的示例中可以看出,在绘制一些Line的步骤中并没有指明所用的画笔,这时Graphics绘制时会自动采用上一次使用的画笔或者初始画笔进行绘制,这样在使用同一种画笔绘制多条直线,或者绘制多种不同图形时,可以省去每一步都必须要指定画笔的工作,减少编码量。

我对GraphicsEx的构造函数访问级别进行了控制,设置为internal级别,只让其在程序集内可见,并且通过构建一个Graphics的扩展方法,用来创建GraphicsEx的实例,用来代替其本身构造函数的功能,这样在使用时就显得更加自然一些。

就写这么多了,不知道大家看完我这样的封装有什么自己的看法,希望不吝赐教,在回帖中和我交流,谢谢!

像Linq一样来使用Graphics的更多相关文章

  1. [C#] Timer + Graphics To Get Simple Animation (简单的源码例子,适合初学者)

    >_<" 这是一个非常简单的利用C#的窗口工程创立的程序,用来做一个简单的动画,涉及Timer和Graphics,适合初学者,高手略过~

  2. WinForms 使用Graphics绘制字体阴影

    C#以两种方法实现文字阴影效果,同时还实现了简单的动画效果: 一种是对文本使用去锯齿的边缘处理,在两个不同的位置绘制文本,形成阴影: 另一个是以阴影为线条,构造影线画刷,先画背景再画前景,使用grap ...

  3. GDI+画图类Graphics的使用

    一:基础定义 #region 定义线尾.线头为箭头.字体和笔刷 Pen p = );//定义画笔 蓝色,宽度为1(坐标显示颜色) p.EndCap = LineCap.ArrowAnchor;//定义 ...

  4. MVC+Spring.NET+NHibernate .NET SSH框架整合 C# 委托异步 和 async /await 两种实现的异步 如何消除点击按钮时周围出现的白线? Linq中 AsQueryable(), AsEnumerable()和ToList()的区别和用法

    MVC+Spring.NET+NHibernate .NET SSH框架整合   在JAVA中,SSH框架可谓是无人不晓,就和.NET中的MVC框架一样普及.作为一个初学者,可以感受到.NET出了MV ...

  5. 【转】WinForms 使用Graphics绘制字体阴影

    转自:http://www.cnblogs.com/LonelyShadow/p/3893743.html C#以两种方法实现文字阴影效果,同时还实现了简单的动画效果: 一种是对文本使用去锯齿的边缘处 ...

  6. c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)

    c#封装DBHelper类   public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...

  7. 模仿ArcGIS用Graphics重绘的直方图分级调节器

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  8. .net 反射访问私有变量和私有方法 如何创建C# Closure ? C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密 C#中的foreach和yield 数组为什么可以使用linq查询 C#中的 具名参数 和 可选参数 显示实现接口 异步CTP(Async CTP)为什么那样工作? C#多线程基础,适合新手了解 C#加快Bitmap的访问速度 C#实现对图片文件的压

    以下为本次实践代码: using System; using System.Collections.Generic; using System.ComponentModel; using System ...

  9. Winform中使用zxing和Graphics实现自定义绘制二维码布局

    场景 zxing.dll下载 https://download.csdn.net/download/badao_liumang_qizhi/11623214 效果 实现 根据上面文章中将简单的二维码生 ...

随机推荐

  1. 转: utf16编码格式(unicode与utf16联系)

    转自: http://www.cnblogs.com/dragon2012/p/5020259.html UTF-16是Unicode字符集的一种转换方式,即把Unicode的码位转换为16比特长的码 ...

  2. 让footer固定在页面(视口)底部(CSS-Sticky-Footer)

    让footer固定在页面(视口)底部(CSS-Sticky-Footer) 这是一个让网站footer固定在浏览器(页面内容小于浏览器高度时)/页面底部的技巧.由HTML和CSS实现,没有令人讨厌的h ...

  3. Arnold+Shave 渲染毛发

    Arnold是一款基于真实物理光照算法和光线追踪算法的照片级渲染器,参与过多部好莱坞大片的制作,公司官网是:www.solidangle.com,官网上有很多效果图: 这里自己用一个球体测试了一下效果 ...

  4. SQL Server的三种物理连接之Loop Join(一)

    Sql Server有三种物理连接Loop Join,Merge Join,Hash Join, 当表之间连接的时候会选择其中之一,不同的连接产生的性能不同,理解这三种物理连接对性能调优有很大帮助. ...

  5. asp.net select Case条件语句的使用方法

    原文:http://www.111cn.net/net/vb-net/38548.htm 如果 testexpression 与任何 Case expressionlist 表达式匹配 ,则执行此 C ...

  6. Swift类与结构体

    类和结构体有很多共性: 定义属性存储数据 定义方法执行功能处理 定义下标,通过下标访问他们的值 初始化他们的状态 通过扩展(Extension)扩展其功能 遵守协议(Protocol),协议提供一种特 ...

  7. 事件[event]_C#

    事件(event): 1.       事件是类在发生其关注的事情时用来提供通知的方式.例如,封装用户界面控件的类可以定义一个在单击该控件时发生的事件.控件类不关心单击按钮时发生了什么,但它需要告知派 ...

  8. eclispe 出现超内纯错误

    刚开始以为只要修改tomcat的最大最小内存就可以,结果还是报错,后来才懂需要在eclipse.ini文件中修改 -Xms256m-Xmx512m的值改大些,增加虚拟机运行的内存空间 刚开始最小值只有 ...

  9. GIS初学者

    学习编程一直以来没有什么好的思路,感觉就是学了忘,忘了再重复,效率特别低下.大概是从大三第一学期才有意识的转向c#的学习,来熟悉VS2010平台,在这之前我都不知道自己是怎么学习的. 大一第二学期开的 ...

  10. ZStack之ZDApp_Init解析

    [注:本文源自博客园http://www.cnblogs.com/cherishui/,为尊重劳动者成果,如需转载请保留此行] 以下代码分析基于ZStack-CC2530-2.5.1a,开发环境为 I ...