用C# GDI+画太极图
研究GDI+了,发现这个东东挺不错的,可以实现很多挺cool的效果,今天尝试画了一个太极图,希望看过的朋友的否极泰来 
 
(CSDN的这个博客太垃圾了,经常不能传图,本来想把图给大家看的,只好放到别的地方通过链接来访问了)
废话少说,看代码:
aspx部分:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Taiji.aspx.cs" Inherits="Taiji" ContentType="Image/Jpeg" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
    
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
(注意:第一行的最末尾有:ContentType="Image/Jpeg"语句)
cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/*
*
* 作者:周公
* 日期:2007-8-16
* 说明:这是周公用纯GDI+画出的一个太极图。演示了GraphicsPath的用法。
* */
public partial class Taiji : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap image = new Bitmap(400, 400);//图象尺寸
Graphics graphics = Graphics.FromImage(image);
Brush blue = new SolidBrush(Color.Blue);//定义蓝色笔刷
Brush red = new SolidBrush(Color.Red);//定义红色笔刷
GraphicsPath leftPath = new GraphicsPath();//初始化路径
GraphicsPath rightPath = new GraphicsPath();//初始化路径
//定义左边的路径(蓝色部分)
leftPath.AddArc(0, 0, 400, 400, 0, 180);
leftPath.AddArc(0, 100, 200, 200, 0, -180);
leftPath.AddArc(200, 100, 200, 200, 0, 180);
//定义右边的路径(红色部分)
rightPath.AddArc(0, 0, 400, 400, 0, -180);
rightPath.AddArc(0, 100, 200, 200, 0, -180);
rightPath.AddArc(200, 100, 200, 200, 0, 180);
//填充左边部分
graphics.FillPath(blue, leftPath);
graphics.FillPie(red, new Rectangle(90, 190, 20, 20), 0, 360);//填充眼睛
//填充右边部分
graphics.FillPath(red, rightPath);
graphics.FillPie(blue, new Rectangle(290, 190, 20, 20), 0, 360);//填充眼睛
graphics.Dispose();
image.Save(Response.OutputStream, ImageFormat.Jpeg);//写入到Response输出流中去
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/*
*
* 作者:周公
* 日期:2007-8-16
* 说明:这是周公用纯GDI+画出的一个太极图。演示了GraphicsPath的用法。
* */
public partial class Taiji : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap image = new Bitmap(400, 400);//图象尺寸
Graphics graphics = Graphics.FromImage(image);
Brush blue = new SolidBrush(Color.Blue);//定义蓝色笔刷
Brush red = new SolidBrush(Color.Red);//定义红色笔刷
GraphicsPath leftPath = new GraphicsPath();//初始化路径
GraphicsPath rightPath = new GraphicsPath();//初始化路径
//定义左边的路径(蓝色部分)
leftPath.AddArc(0, 0, 400, 400, 0, 180);
leftPath.AddArc(0, 100, 200, 200, 0, -180);
leftPath.AddArc(200, 100, 200, 200, 0, 180);
//定义右边的路径(红色部分)
rightPath.AddArc(0, 0, 400, 400, 0, -180);
rightPath.AddArc(0, 100, 200, 200, 0, -180);
rightPath.AddArc(200, 100, 200, 200, 0, 180);
//填充左边部分
graphics.FillPath(blue, leftPath);
graphics.FillPie(red, new Rectangle(90, 190, 20, 20), 0, 360);//填充眼睛
//填充右边部分
graphics.FillPath(red, rightPath);
graphics.FillPie(blue, new Rectangle(290, 190, 20, 20), 0, 360);//填充眼睛
graphics.Dispose();
image.Save(Response.OutputStream, ImageFormat.Jpeg);//写入到Response输出流中去
}
}
说明:这里又展示了一种在asp.net中输出图象的方式,在本人的另一篇文章《asp.net中验证码的实现》中是通过程序设置ContentType的,而这里则是直接在aspx页面中设置,然后将画好的图片写入到当前Response对象的输出流中。
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号