asp.net C#繪製太極圖__.net

來源:互聯網
上載者:User

成品圖:


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;    }}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.