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

画出来的效果如下:

像Linq一样来使用Graphics-LMLPHP

下面就是我对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的实例,用来代替其本身构造函数的功能,这样在使用时就显得更加自然一些。

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

05-11 15:15