用C# GDI+画太极图

本文介绍了使用GDI+在ASP.NET中绘制太极图的方法,展示了如何利用GraphicsPath创建复杂的图形,并将绘制好的图片输出到Response对象的输出流中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

研究GDI+了,发现这个东东挺不错的,可以实现很多挺cool的效果,今天尝试画了一个太极图,希望看过的朋友的否极泰来
(优快云的这个博客太垃圾了,经常不能传图,本来想把图给大家看的,只好放到别的地方通过链接来访问了)

废话少说,看代码:
aspx部分:

<% @PageLanguage = " C# " AutoEventWireup = " true " CodeFile = " Taiji.aspx.cs " Inherits = " Taiji " ContentType = " Image/Jpeg " %>

<! DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//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,EventArgse)
{
Bitmapimage
= new Bitmap( 400 , 400 ); // 图象尺寸
Graphicsgraphics = Graphics.FromImage(image);
Brushblue
= new SolidBrush(Color.Blue); // 定义蓝色笔刷
Brushred = new SolidBrush(Color.Red); // 定义红色笔刷
GraphicsPathleftPath = new GraphicsPath(); // 初始化路径
GraphicsPathrightPath = 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对象的输出流中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值