?? mycreateimage.aspx.cs
字號:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.IO;
namespace Example_11_6
{
/// <summary>
/// Summary description for MyCreateImage.
/// </summary>
public class MyCreateImage : System.Web.UI.Page
{
private ArrayList dataList = new ArrayList();
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
///繪制相應的圖像
CreateImage(SetDataList(dataList));
}
}
private ArrayList SetDataList(ArrayList dataList)
{
dataList.Clear();
///添加圖片的起始坐標
DataPoint pointzero = new DataPoint();
pointzero.X = 0;
pointzero.Y = 0;
dataList.Add(pointzero);
///添加圖片的最大坐標
DataPoint pointwero = new DataPoint();
pointwero.X = 200;
pointwero.Y = 200;
dataList.Add(pointwero);
///返回坐標數據
return(dataList);
}
private void CreateImage(ArrayList dataList)
{
///判定數據是否正確,否則終止程序
if(dataList.Count < 2)
{
Response.Write("從數據庫讀取數據錯誤!");
return;
}
///創建背景圖片
Bitmap mymap = new Bitmap(((DataPoint)dataList[1]).X * 2,((DataPoint)dataList[1]).Y * 2);
///創建畫筆并設置其顏色
Graphics graphic = Graphics.FromImage(mymap);
Pen pen = new Pen(Color.Red,4);
///繪制標準圓
graphic.DrawEllipse(pen,((DataPoint)dataList[0]).X,((DataPoint)dataList[0]).Y,((DataPoint)dataList[1]).X,((DataPoint)dataList[1]).Y);
///繪制三角形
graphic.DrawLine(new Pen(Color.Green,3),new Point(((DataPoint)dataList[0]).X,((DataPoint)dataList[1]).Y),new Point(((DataPoint)dataList[1]).X,((DataPoint)dataList[1]).Y));
graphic.DrawLine(new Pen(Color.Green,3),new Point(((DataPoint)dataList[1]).X,((DataPoint)dataList[1]).Y),new Point(((DataPoint)dataList[1]).X,((DataPoint)dataList[1]).Y * 2));
graphic.DrawLine(new Pen(Color.Green,3),new Point(((DataPoint)dataList[1]).X,((DataPoint)dataList[1]).Y * 2),new Point(((DataPoint)dataList[0]).X,((DataPoint)dataList[1]).Y));
///繪制正方形
graphic.DrawRectangle(new Pen(Color.Yellow,3),225,75,((DataPoint)dataList[1]).Y / 2 + 50,((DataPoint)dataList[1]).Y);
///保存并輸出繪制的圖片
// mymap.Save(Response.OutputStream,ImageFormat.Jpeg);
OutPicture(mymap);
///釋放占用的資源
graphic.Dispose();
mymap.Dispose();
}
private void OutPicture(Bitmap map)
{
///定義輸出的內存流
MemoryStream ms = new MemoryStream();
///保存圖像
map.Save(ms,ImageFormat.Png);
///清空網頁的輸出
Response.ClearContent();
///設置網頁輸出的格式
Response.ContentType = "Image/png";
///輸出圖像
Response.BinaryWrite(ms.ToArray());
Response.End();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
/// <summary>
/// 定義坐標類
/// </summary>
public class DataPoint
{
public int X = 0; ///保存坐標的X值
public int Y = 0; ///保存坐標的Y值
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -