C#画图——Graphics
C#要实现简单的画图功能可以利用Graphics这个类,要使用Graphics必需using命名空间System.Drawing(此名明空间下都是关于图形的操作)。首先创建画布:
Bitmap bmp = new Bitmap(, );
Graphics g = Graphics.FromImage(bmp);
清除画布的背景色,并且指定颜色填充:
g.Clear(Color.White);
开始画图:
//画矩形
g.DrawRectangle(new Pen(Color.Red), new Rectangle(, , , ));
//填充扇形
g.FillPie(new SolidBrush(Color.Red), new Rectangle(, , , ), , );
//在画布上写文字
g.DrawString("A", new Font("Times New Roman", ), new SolidBrush(Color.Black), , );
下面给出完整代码:
public class GraphicsController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult CreateGraphics()
{
Bitmap bmp = new Bitmap(, );
//画布
Graphics g = Graphics.FromImage(bmp);
//清除画布背景色,并填充指定色
g.Clear(Color.White);
//画矩形
g.DrawRectangle(new Pen(Color.Red), new Rectangle(, , , ));
//画刷
Brush bs = new SolidBrush(Color.Blue);
//填充扇形
g.FillPie(new SolidBrush(Color.Red), new Rectangle(, , , ), , );
g.FillPie(bs, new Rectangle(, , , ), , );
g.DrawPie(new Pen(bs), new Rectangle(, , , ), , );//画扇形
g.FillPie(new SolidBrush(Color.HotPink), new Rectangle(, , , ), , );
g.DrawString("A", new Font("Times New Roman", ), new SolidBrush(Color.Black), , );
//抗锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Gif);
return File(ms.ToArray(), @"image/Gif");
}
catch (Exception)
{
return null;
}
}
}
后台
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<img src="/Bitmap/CreateGraphics" width="1000" height="800" />
</div>
</body>
</html>
效果图(好像有点简陋(╯▽╰ )):

最后推荐一些前辈的总结(比我强太多了):
http://www.cnblogs.com/Jerry-Chou/archive/2012/03/20/2408064.html
http://www.cnblogs.com/Jerry-Chou/archive/2012/03/21/2409590.html
http://www.cnblogs.com/beyond0309/archive/2008/04/15/1155003.html验证码也可以用这种方式生成
C#画图——Graphics的更多相关文章
- java 画图 Graphics 文字自动换行
/** * 文字超出限定长度自动换行 * * @param g 画布 * @param font 字体样式 * @param text 文字 * @param widthLength 最大长度 (多少 ...
- java画图之曲线拖动
目标:在窗体上按下鼠标按键.然后拖动鼠标,在按下和拖动之间绘制曲线 事件机制 事件源对象:窗体 事件监听方法:addMouseListener(MouseListener l);addMouseMot ...
- java画图之初体验
1.实现画图程序所需的API类 JFrame JButton ActionListener 动作事件接口 ActionEvent ...
- C#后台画图保存为ipg/png的文件
public void Exec1() { string imgurl = @"http://api.senwoo.com/Content/HeadPortrait/" ...
- NET简单的一个画图程序
using System; using System.Drawing; //HttpUtility.UrlEncode /// <summary> ///Curve 的摘要说明 /// & ...
- Java -- AWT 画图,图像处理
1. AWT画图 Graphics类 提供绘制简单图形的方法 更新图片时用到 repaint , update , 程序不应该主动调用paint和update, 这两个方法都应该是由AWT系统负责 ...
- java制作验证码
建立一个web工程
- [C# 开发技巧]实现属于自己的截图工具
[C# 开发技巧]实现属于自己的截图工具 一.引言 之前一直都是写一些C#基础知识的内容的,然而有些初学者可能看完了这些基础知识之后,会有这样一个疑惑的——我了解了这些基础知识之后,我想做一些工具怎么 ...
- 【程序员的吃鸡大法】利用OCR文字识别+百度算法搜索,玩转冲顶大会、百万英雄、芝士超人等答题赢奖金游戏
[先上一张效果图]: 一.原理: 其实原理很简单: 1.手机投屏到电脑: 2.截取投屏画面的题目部分,进行识别,得到题目和三个答案: 3.将答案按照一定的算法,进行搜索,得出推荐答案: 4.添加了一些 ...
随机推荐
- Exchanger使用
Exchanger使用
- [NPM] npm check to update the dependencies
To update the dependencies in the project, we can run: npx npm-check -u
- form怎样正确post文件
form在HTML中,是用于收集用户输入的,基本全部浏览器都支持form.给form加入method属性.就能实现将用户在form内控件输入的信息POST到制定地址.或发送GET请求. 写了以下一段代 ...
- UITableViewController的子控件不随着滑动
UITableViewController的子控件不随着滑动 我们知道有时候使用UITableViewController简单便捷,省事,但是如果我们使用了addSubview,无论是[self.vi ...
- BestCoder #49 Untitled HDU 5339
BestCoder #49 Untitled HDU 5339 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5339 本题採用深搜, 数据量小,先做 ...
- Supporting IPv6-only Networks
Supporting IPv6-only Networks - Support - Apple Developer https://developer.apple.com/support/ipv6/ ...
- jQuery 1.x and 2.x , which is better?
1. jQuery 1.x和2.x的区别 或者可以说是jQuery 2.x有什么新特征? jQuery官方发布2.x原话 不再支持IE6/7/8,如果在IE9/10里只用“兼容性视图”模式也将会受到影 ...
- solr入门之多线程操作solr中索引字段的解决
涉及的问题: 建索引时有一个字段是该词语出现的次数,这个字段是放在solr里的 而我用的是多线程来进行全量导入的,这里就涉及到了多线程问题 多个线程操作同一个变量时怎样处理? 我是这样子做的 : 首 ...
- 什么是PMU(PMIC)【转】
本文转载自:http://blog.csdn.net/zhenwenxian/article/details/7614537 什么是PMU(PMIC) PMU(power management uni ...
- POJ - 3177 Redundant Paths(边双连通分支)(模板)
1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...