題目是這樣的,就是讓任務管理器裡面的 CPU 曲線以一條平衡的百分之五十的直線顯現出來,
然後再寫另外一個程序讓 CPU 曲線以正弦曲線的形式顯現出來,
呵呵,很有趣吧,昨晚看著看著都入迷了,不過由於太晚,所以沒有太注意,
不過有一個我還是有點好奇的,因為我以前沒有寫過正弦曲線出來,
所以早上一起來便寫正弦曲線,當然這條正弦曲線並不能使 CPU 曲線以正弦曲線顯示,
而是純粹的以一張圖片來返回正弦曲線
如果哪位牛人有興趣的話,那絕對可以嘗試一下上面的那個題目,這種問題都想得出,太牛逼了,呵呵
大致代碼如下
try
{
using (Bitmap sinImage = new Bitmap(360, 120))
{
using (Graphics myGraphics = Graphics.FromImage(sinImage))
{
myGraphics.Clear(Color.White);
myGraphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle r1 = new Rectangle(0, 0, 360, 20);
Rectangle r2 = new Rectangle(0, 20, 360, 40);
Rectangle r3 = new Rectangle(0, 60, 360, 40);
Rectangle r4 = new Rectangle(0, 100, 360, 20);
Brush brush1 = new SolidBrush(Color.OrangeRed);
Brush brush2 = new SolidBrush(Color.SkyBlue);
Brush brush3 = new SolidBrush(Color.Pink);
Brush brush4 = new SolidBrush(Color.YellowGreen);
myGraphics.FillRectangle(brush1, r1);
myGraphics.FillRectangle(brush2, r2);
myGraphics.FillRectangle(brush2, r3);
myGraphics.FillRectangle(brush1, r4);
myGraphics.DrawString("0", new Font("宋體", 8), brush1, new PointF(3, 65));
myGraphics.DrawString("90", new Font("宋體", 8), brush1, new PointF(85, 65));
myGraphics.DrawString("180", new Font("宋體", 8), brush1, new PointF(170, 65));
myGraphics.DrawString("360", new Font("宋體", 8), brush1, new PointF(336, 65));
Point myPoint = new Point(0, 60);
float sinValue = 0.0F;
for (int i = 0; i < 360; i++)
{
sinValue = Convert.ToSingle(Math.Sin(Convert.ToSingle((i * Math.PI) / 180))) * 40;
//事實上,這裡根本無需注意 sinValue 的正負
//當其為負時, 60-sinValue 則會變大
Point thisPoint = new Point(i, Convert.ToInt32(60 - sinValue));
myGraphics.DrawLine(new Pen(brush3), thisPoint, myPoint);
myPoint = thisPoint;
}
}
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
context.Response.ContentType = "Image/PNG";
context.Response.Clear();
context.Response.BufferOutput = true;
sinImage.Save(ms, ImageFormat.Png);
ms.Flush();
context.Response.BinaryWrite(ms.GetBuffer());
}
}
}
catch
{
return;
}