C# 画图方法总线
新二到新公司上班,加入一个项目中,要我做绘图模块。
所以就在网上看了好多的绘图方法。现给段代码分析一下!
代码
//定义一张图片
Bitmap MyImage = new Bitmap(950, 500);
//定义一个画板
Graphics graphics = Graphics.FromImage(MyImage);
//画一个带边框的矩形
Rectangle rect = new Rectangle(1, 1, MyImage.Width - 2, MyImage.Height - 2);
graphics.FillRectangle(new SolidBrush(Color.FromArgb(255, 255, 229)), rect);
//声明一个画笔
Pen p = new Pen(Color.Blue, 3);
graphics.DrawLine(p,10,10,100,100);
graphics.DrawRectangle(p, 10, 10, 100, 100);
graphics.DrawEllipse(p, 10, 10, 100, 100);
Pen PDash = new Pen(Color.FromArgb(108,108,82),2);
//这里是自定义虚线的长度
PDash.DashPattern = new float[] { 4, 1 };
//自定义虚线
PDash.DashStyle = DashStyle.Custom;
//自定义箭头,这里是自定义箭头的大小是否为实心
AdjustableArrowCap aac=new AdjustableArrowCap(6,6,true);
PDash.CustomStartCap = aac;
PDash.CustomEndCap = aac;
graphics.DrawLine(PDash, 20,150,20,400);
//园头,这里是画一个带圆头的直线,其实可以按需求画成别的!
Brush bRound = new SolidBrush(Color.Red);
Pen pRound = new Pen(bRound, 15);
pRound.StartCap = LineCap.Round;
pRound.EndCap = LineCap.Round;
graphics.DrawLine(pRound, 50, 30, 50, 380);
//下面为在页面是输也图片
MyImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
MyImage.Dispose();
graphics.Dispose();
Response.End();
已上是一段画图的代码,注释中已写的比较清楚,有些内容,日后再补!
这里附上一个教程,我是在百度文库中找来的。还不错:三层架构示意图.rar