使用C1FlashCanvas绘制Flash单帧画面或背景

发布时间:2014/05/28 00:05 发布者:Alice

返回博客中心

本文介绍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方法画出的图形如下所示:

Flash-draw

用Fill方法画出的图形如下所示:

Flash-Fill

 

本文Demo的源代码如下:

Canvas_Bubbles.7z (254.36 kb)


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网