C#代码实现矢量画图

原文: C#代码实现矢量画图

版权声明:本文为博主原创文章,转载请附上链接地址。 https://blog.csdn.net/ld15102891672/article/details/80275969

    要实现C#代码画矢量图,其基本原理是先创建一个容器作为画板,然后创建Line(直线)、PolyLine(多段线)、Rectangle(矩形)或者Ellipse(椭圆)基本绘图对象生成各种矢量图形,最后把这些图形对象添加到画板中即可,一般用Canvas容器作为画板。下面以在Canvas容器控件中绘制Line(直线)、PolyLine(多段线)、Rectangle(矩形)或者Ellipse(椭圆)等基本图形对矢量绘图进行简单的介绍,希望对大家有所帮助。

    创建一个C#项目,在项目中添加Canvas并把Canvas属性的旋转角度设置为-90度,然后添加绘制各种基本图形的按钮


      绘制坐标系效果图及代码


        private void PaintGrid()//画坐标系
		{
		    Line l=new Line();
			l.X1=0;
			l.Y1=10;
			l.X2=0;
			l.Y2=this.canvas.Height-10;
			l.StrokeThickness=1;
			l.Stroke=new SolidColorBrush(Color.FromRgb(0, 0, 0));
			Canvas.SetLeft(l,this.canvas.Width/2);
			this.canvas.Children.Add(l);
			l=new Line();
			l.X1=10;
			l.Y1=0;
			l.X2=this.canvas.Width-10;;
			l.Y2=0;
			l.StrokeThickness=1;
			l.Stroke=new SolidColorBrush(Color.FromRgb(0, 0, 0));
			Canvas.SetTop(l,this.canvas.Height/2);
			this.canvas.Children.Add(l);
			for(int i=-10;i<=10;i++)
			{
			  l=new Line();
			  Line ly=new Line();
			  l.X1=i*15;
			  l.X2=i*15;
			  ly.Y1=i*15;
			  ly.Y2=i*15;
			 if(i%2==0)
			  {
				l.Y1=-5;
				l.Y2=5;
				ly.X1=-5;
				ly.X2=5;
			   }
				else
			   {
			    l.Y1=-10;
				l.Y2=10;
				ly.X1=-10;
				ly.X2=10;
			    }
			  l.StrokeThickness=1;
			  l.Stroke=new SolidColorBrush(Color.FromRgb(0, 0, 0));
			  Canvas.SetLeft(l,this.canvas.Width/2);
			  Canvas.SetTop(l,this.canvas.Height/2);
			  ly.StrokeThickness=1;
			  ly.Stroke=new SolidColorBrush(Color.FromRgb(0, 0, 0));
			  Canvas.SetLeft(ly,this.canvas.Width/2);
			  Canvas.SetTop(ly,this.canvas.Height/2);
			  this.canvas.Children.Add(ly);
			  this.canvas.Children.Add(l);
			}
			 Label lb=new Label();
			  lb.Content="X";
			  RotateTransform rotateTransform = new RotateTransform(90);//90度
			  lb.RenderTransform=rotateTransform;
			  Canvas.SetRight(lb,5);
			  Canvas.SetTop(lb,canvas.Height/2-20);
			  this.canvas.Children.Add(lb);
			  lb=new Label();
			  lb.Content="Y";
			  lb.RenderTransform=rotateTransform;
			  Canvas.SetRight(lb,canvas.Width/2-15);
			  Canvas.SetBottom(lb,10);
			  this.canvas.Children.Add(lb);
			  Polyline pl=new Polyline();
			  pl.Points.Add(new Point(this.canvas.Width/2-20,-5));
			  pl.Points.Add(new Point(this.canvas.Width/2-10,0));
			  pl.Points.Add(new Point(this.canvas.Width/2-20,5));
			  pl.Stroke=new SolidColorBrush(Color.FromRgb(0,0,0));
			  pl.StrokeThickness=1;
			  Canvas.SetLeft(pl,this.canvas.Width/2);
			  Canvas.SetTop(pl,this.canvas.Height/2);
			   this.canvas.Children.Add(pl);
			  pl=new Polyline();
			  pl.Points.Add(new Point(-5,this.canvas.Height/2-20));
			  pl.Points.Add(new Point(0,this.canvas.Height/2-10));
			  pl.Points.Add(new Point(5,this.canvas.Height/2-20));
			  pl.Stroke=new SolidColorBrush(Color.FromRgb(0,0,0));
			  pl.StrokeThickness=1;
			  Canvas.SetLeft(pl,this.canvas.Width/2);
			  Canvas.SetTop(pl,this.canvas.Height/2);
			   this.canvas.Children.Add(pl);
		}

      绘制直线代码

private void bth_paint_Line(object sender, System.Windows.RoutedEventArgs e)//画直线
		{
			this.canvas.Children.Clear();//清空画板
			this.PaintGrid();//画坐标系
			Line l=new Line();//直线
			l.X1=0;
			l.Y1=0;
			l.X2=200;
			l.Y2=200;
			l.StrokeThickness=1;//直线宽度
			l.Stroke=new SolidColorBrush(Color.FromRgb(0, 0, 255));//直线颜色(蓝色)
			Canvas.SetLeft(l,this.canvas.Width/2);//X的原点平移到canvas容器中间
			Canvas.SetTop(l,this.canvas.Height/2);//Y的原点平移到canvas容器中间
			this.canvas.Children.Add(l);//在容器中添加该直线
		}

效果图


     画多段线代码

		private void bth_paint_Polyline(object sender, System.Windows.RoutedEventArgs e)//画多段线
		{
			this.canvas.Children.Clear();//清空画板
			this.PaintGrid();//画坐标系
			Polyline pl=new Polyline();
			pl.Points.Add(new Point(0,0));
			pl.Points.Add(new Point(50,50));
			pl.Points.Add(new Point(0,100));
			pl.Points.Add(new Point(50,150));
			pl.Stroke=new SolidColorBrush(Color.FromRgb(0,0,255));
			pl.StrokeThickness=1;
			Canvas.SetLeft(pl,this.canvas.Width/2);//X的原点平移到canvas容器中间
			Canvas.SetTop(pl,this.canvas.Height/2);//Y的原点平移到canvas容器中间
			this.canvas.Children.Add(pl);//在容器中添加该多段线
		}

效果图


画矩形代码

		private void bth_paint_Rectangle(object sender, System.Windows.RoutedEventArgs e)//画矩形
		{
			this.canvas.Children.Clear();//清空画板
			this.PaintGrid();//画坐标系
            Rectangle rect=new Rectangle();
			rect.Width=100;
			rect.Height=200;
			rect.Stroke=new SolidColorBrush(Color.FromRgb(0,0,255));
			rect.StrokeThickness=1;
			Canvas.SetLeft(rect,this.canvas.Width/2-rect.Width/2);
			Canvas.SetTop(rect,this.canvas.Height/2-rect.Height/2);
			this.canvas.Children.Add(rect);	
		}

效果图


画圆代码

	private void bth_paint_Circle(object sender, System.Windows.RoutedEventArgs e)//画圆
		{
			this.canvas.Children.Clear();//清空画板
			this.PaintGrid();//画坐标系
			Ellipse ep=new Ellipse();
			ep.Height=300;
			ep.Width=300;
			ep.Stroke=new SolidColorBrush(Color.FromRgb(0, 0, 255));
			ep.StrokeThickness=1;
			Canvas.SetLeft(ep,this.canvas.Width/2-ep.Width/2);
			Canvas.SetTop(ep,this.canvas.Height/2-ep.Height/2);
			this.canvas.Children.Add(ep);
		}

效果图


画椭圆代码

private void bth_paint_Ellipse(object sender, System.Windows.RoutedEventArgs e)//画椭圆
		{
			this.canvas.Children.Clear();//清空画板
			this.PaintGrid();//画坐标系
            Ellipse ep=new Ellipse();
			ep.Height=300;
			ep.Width=50;
			ep.Stroke=new SolidColorBrush(Color.FromRgb(0, 0, 255));
			ep.StrokeThickness=1;
			Canvas.SetLeft(ep,this.canvas.Width/2-ep.Width/2);
			Canvas.SetTop(ep,this.canvas.Height/2-ep.Height/2);
			this.canvas.Children.Add(ep);
		}

效果图


清空画板代码

private void btn_Clear(object sender, System.Windows.RoutedEventArgs e)
		{
			this.canvas.Children.Clear();//清空画板
			this.PaintGrid();//画坐标系
		}
本次矢量画图编程就介绍到这里,如果还有不明白的地方,可以加入扣扣群234035436进行技术交流,希望大家多多支持!


posted on 2018-10-24 17:57 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/9844983.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
21世纪什么最重要?通用!想让你的图形控件和主流软件共享同一种编辑结果吗,那就选择我吧!国内唯一全面基于国际矢量图形标准SVG的专业开发控件,提供全面的矢量编辑能力,能适应组态软件和其他图形软件开发需要。能够和其他主流的编辑软件如illustrator,Visio等共享最终编辑结果。使用VectorControl.Net,您可以:> 基本形状绘制,支持圆、椭圆、矩形、正方形、直线、多边形、折线的绘制> 图像和文本构造,支持导入图像和文本构造> 扩展形状绘制,支持星形形状和扇形的构造> 钢笔工具绘制,支持任意形状的曲线构造(贝赛尔曲线)> 二维变换,支持缩放、扭曲、平移和旋转等二维变换,并可以启用或禁用这些操作。> 图形的成组和解组> 图形的层次调整> 支持多个图形之间的对齐> 支持多个图形之间的分布功能> 调整多个图形的尺寸以达到相同高度或宽度> 网格、参考线、标尺等完整视图环境并支持吸附。> 支持外部自定义形状和图元文件,给与用户自由扩展功能的能力,并能将当前绘图内容导出为形状和图元> 支持连接线,可以在对象之间保持联系> 无限制的撤销/重作> 支持SVG文件的导入> Bmp,Jpg,Gif,Tiff等多种栅格图像格式的导出> SVG文件代码预览> VectorControl.Net还提供了许多Windows界面开发组件,包括弹出菜单时容器,形状和组件库选择器等> 打印支持> 两套完整的示例程序以及源代码(C#和VB.Net),全面全面展现了如何利用VectorControl.Net控件实现简单的矢量应用(不需要任何手工编码实现SVG浏览器)和利用VectorControl实现一个完整的矢量编辑环境(具备所有矢量图形编辑功能)的能力,您甚至可以直接将我们提供的示例程序用作您默认的矢量图形的编辑器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值