本文实例讲述了asp.net实现C#绘制太极图的方法。分享给大家供大家参考。具体如下:
成品图如下所示:
html页面:
注意设置:
复制代码 代码如下:ContentType="Image/Jpeg"
复制代码 代码如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TaiJiTu.aspx.cs" Inherits="TaiJiTu" ContentType="Image/Jpeg" %>
<!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;
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#程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]