asp.net实现C#绘制太极图的方法
本文实例讲述了asp.net实现c#绘制太极图的方法。分享给大家供大家参考。具体如下:
成品图如下所示:
html页面:
注意设置:
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
后台代码:
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.imaging;
public partial class taijitu : system.web.ui.page
{
private encoder myencoder;
private encoderparameter myencoderparameter;
private encoderparameters myencoderparameters;
protected void page_load(object sender, eventargs e)
{
int imgwidth = 400; //图象尺寸
int eyeradius = imgwidth / 20; //鱼眼半径
int headdiameter = imgwidth / 2; //鱼头直径
bitmap image = new bitmap(imgwidth, imgwidth);
image.setresolution(300, 300);
graphics graphics = graphics.fromimage(image);
//设置图像质量
graphics.compositingquality = compositingquality.highquality;
graphics.smoothingmode = smoothingmode.antialias;
graphics.interpolationmode = interpolationmode.highqualitybicubic;
//底色填充为白色
brush white = new solidbrush(color.white);
graphics.fillrectangle(white, new rectangle(0, 0, imgwidth, imgwidth));
brush blue = new solidbrush(color.blue);//定义蓝色笔刷
brush red = new solidbrush(color.red);//定义红色笔刷
//整个圆形填充蓝色
graphics.fillpie(blue, 0, 0, imgwidth, imgwidth, 0, 360);
//定义右边的路径(红色部分)
graphicspath redpath = new graphicspath();//初始化路径
redpath.addarc(0, 0, imgwidth, imgwidth, 0, -180);
redpath.addarc(0, headdiameter / 2, headdiameter, headdiameter, 0, -180);
redpath.addarc(headdiameter, headdiameter / 2, headdiameter, headdiameter, 0, 180);
//填充右边部分
graphics.fillpath(red, redpath);
//填充红色眼睛
graphics.fillpie(red, new rectangle(headdiameter / 2 - eyeradius, headdiameter - eyeradius, eyeradius * 2, eyeradius * 2), 0, 360);
//填充蓝色眼睛
graphics.fillpie(blue, new rectangle(headdiameter + headdiameter / 2 - eyeradius, headdiameter - eyeradius, eyeradius * 2, eyeradius * 2), 0, 360);
graphics.dispose();
//写入到response输出流中去,普通质量
//image.save(response.outputstream, imageformat.jpeg);
//修改图片保存质量
imagecodecinfo myimagecodecinfo = getencoder(imageformat.jpeg);
myencoder = encoder.quality;
myencoderparameters = new encoderparameters(1);
//图片质量等级
myencoderparameter = new encoderparameter(myencoder, 100l);
myencoderparameters.param[0] = myencoderparameter;
//使用指定参数输出
image.save(response.outputstream, myimagecodecinfo, myencoderparameters);
}
private static imagecodecinfo getencoder(imageformat format)
{
imagecodecinfo[] codecs = imagecodecinfo.getimageencoders();
foreach (imagecodecinfo codec in codecs)
{
if (codec.formatid == format.guid)
{
return codec;
}
}
return null;
}
}
希望本文所述对大家的c#程序设计有所帮助。
上一篇: .net中webconfig 详解
下一篇: Java动态编译执行代码示例