本文介绍C1FlashCanvas控件绘制Flash单帧画面或背景的方法。C1FlashCanvas是一个与.NET图形类相似的组件,包含了与.NET Graphics类完全相同的绘制图形的方法和属性。
创建C1FlashCanvas
首先从工具箱里拖拽C1FlashCanvas到窗体,也可以使用代码声明,如:
private C1.C1Flash.C1FlashCanvas c1FlashCanvas1 = new C1.C1Flash.C1FlashCanvas();
绘制Flash单帧画面
在绘制Flash单帧画面前,使用C1FlashCanvas的Clear方法清除canvas里的所有内容。
然后使用C1FlashCavas的Draw方法绘制需要的图形。它可以绘制文本、图像、线条、矩形、椭圆、扇形、弧形、圆角矩形、多边形,贝塞尔曲线,以及更多,可以参考C1FlashCanvas下的DrawString,DrawImage,DrawLine等方法。在本文Demo中有详细的代码说明各种图形的画法,下面就用绘制Rectangle举例说明如何绘制图形。
用代码随机生成一个Rectangle,参考下面代码中的RandomRectangle方法,然后通过C1FlashCanvas的DrawRectangle方法在canvas里绘制这个Rectangle。
private Pen _pen = new Pen(Color.Black); _pen.Color = this.panel_DrawColor.BackColor; _pen.Width = (int)this.upd_LineWidth.Value; for(int i = 0; i < 20; i++) this.c1FlashCanvas1.DrawRectangle(_pen, RandomRectangle()); private Rectangle RandomRectangle() { int maxWidth = this.c1FlashCanvas1.Width; int maxHeight = this.c1FlashCanvas1.Height; Point pos = new Point(_rand.Next(_minSize, maxWidth), _rand.Next(_minSize, maxHeight)); int width = _rand.Next(_minSize, maxWidth/2); int height = _rand.Next(_minSize, maxHeight/2); return new Rectangle(pos, new Size(width, height)); }
绘制Flash背景
在绘制Flash背景前,使用C1FlashCanvas的Clear方法清除canvas里的所有内容。
然后使用C1FlashCavas的Fill方法绘制背景。它可以绘制矩形、椭圆、扇形等背景,可以参考C1FlashCanvas下的FillPie,FillEllipse,FillRectangle等方法。在本文Demo中有详细的代码说明各种图形的画法,下面就用Rectangle举例说明如何绘制背景。
用代码随机生成一个SolidBrush,参考下面代码中的RandomColor方法,然后通过C1FlashCanvas的FillRectangle方法在canvas里绘制这个Rectangle背景。代码中的RandomRectangle方法在上一段代码中有定义。
SolidBrush brush = new SolidBrush(Color.Black); for(int i = 0; i < 20; i++) { brush.Color = RandomColor(); this.c1FlashCanvas1.FillRectangle(brush, RandomRectangle()); } brush.Dispose(); private Random _rand = new Random(); private Color RandomColor() { return Color.FromArgb(_rand.Next(255), _rand.Next(255), _rand.Next(255), _rand.Next(255)); }
生成SWF文件
使用C1FlashCanvas的RenderToFile方法可以将Flash内容绘制到SWF文件。代码如下所示:
this.c1FlashCanvas1.RenderToFile(tempdir + @"\c1flash_canvas_bubbles.swf");
最后使用System.Diagnostics.Process.Start方法打开文件。
用Draw方法画出的图形如下所示:
用Fill方法画出的图形如下所示:
本文Demo的源代码如下: