ASP.NET动态生成进度条图片
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
/// <summary>
/// this class for build Some pictures for web-ui
/// </summary>
public class UIMaps
{
public UIMaps()
{
}
/// <summary>
/// Build a progress width this value in UIProgressProperty structure
/// </summary>
/// <param name="uiprspty">Property</param>
/// <returns>a bitmap</returns>
public Bitmap BuildDefault(UIProgressProperty uiprspty)
{
if (uiprspty.Width <= 5 || uiprspty.Height <= 3)
return null;
#region Init
//Measure Value
int rValueWidth = 0;
if (uiprspty.Value <= 0)
{
rValueWidth = 0;
uiprspty.Value = 0;
}
else if (uiprspty.Value > uiprspty.maxValue)
{
rValueWidth = uiprspty.Width;
uiprspty.Value = uiprspty.maxValue;
}
else
rValueWidth = (int)((float)((float)uiprspty.Value / (float)uiprspty.maxValue) * uiprspty.Width); //real length of data
//Caption
uiprspty.Text += Convert.ToString((int)(((float)uiprspty.Value / (float)uiprspty.maxValue) * 100)) + "%"; //%20
Bitmap map = new Bitmap(uiprspty.Width, uiprspty.Height);
Graphics g = Graphics.FromImage(map);
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
//background
g.FillRectangle(new SolidBrush(uiprspty.backColor),
new Rectangle(0, 0, uiprspty.Width, uiprspty.Height));
#endregion
#region datas
//datas
//g.FillRectangle(new SolidBrush(uiprspty.fColor1),
// new Rectangle(0, 1, rValueWidth, uiprspty.Height - 2));
Rectangle topleftrc = new Rectangle(1, 1, rValueWidth / 2, uiprspty.Height / 2);
if (topleftrc.Width <= 0) topleftrc.Width = 1;
LinearGradientBrush topleft = new LinearGradientBrush(topleftrc,
uiprspty.fColor1, uiprspty.fColor2,
LinearGradientMode.ForwardDiagonal);
Rectangle toprgtrc = new Rectangle((rValueWidth / 2) + 1, 1, rValueWidth / 2, uiprspty.Height / 2);
if (toprgtrc.Width <= 0) toprgtrc.Width = 1;
LinearGradientBrush toprgt = new LinearGradientBrush(toprgtrc,
uiprspty.fColor1, uiprspty.fColor2,
LinearGradientMode.BackwardDiagonal);
Rectangle btmleftrc = new Rectangle(1, (uiprspty.Height / 2) - 1, rValueWidth / 2, uiprspty.Height / 2);
if (btmleftrc.Width <= 0) btmleftrc.Width = 1;
LinearGradientBrush btmleft = new LinearGradientBrush(btmleftrc,
uiprspty.fColor2, uiprspty.fColor1, LinearGradientMode.BackwardDiagonal);
Rectangle btmrgtrc = new Rectangle((rValueWidth / 2) + 1, (uiprspty.Height / 2) - 1, rValueWidth / 2, uiprspty.Height / 2);
if (btmrgtrc.Width <= 0) btmrgtrc.Width = 1;
LinearGradientBrush btmrgt = new LinearGradientBrush(btmrgtrc,
uiprspty.fColor2, uiprspty.fColor1, LinearGradientMode.ForwardDiagonal);
g.FillRectangle(topleft, topleftrc);
g.FillRectangle(toprgt, toprgtrc);
g.FillRectangle(btmleft, btmleftrc);
g.FillRectangle(btmrgt, btmrgtrc);
topleft.Dispose();
toprgt.Dispose();
btmleft.Dispose();
btmrgt.Dispose();
#endregion
#region paint text
if (uiprspty.showText)
{
if (uiprspty.Text != null || uiprspty.Text.Trim() != "")
{
SizeF szf = g.MeasureString(uiprspty.Text, uiprspty.textFont);
int leftp = (int)szf.Width > uiprspty.Width ? 1 : (uiprspty.Width - (int)szf.Width) / 2;
int toppx = (int)szf.Height > uiprspty.Height ? 1 : (uiprspty.Height - (int)szf.Height) / 2;
g.DrawString(uiprspty.Text, uiprspty.textFont,
new SolidBrush(uiprspty.foreColor), (float)leftp, (float)toppx);
}
}
#endregion
//border
g.DrawRectangle(new Pen(uiprspty.borderColor),
new Rectangle(0, 0, uiprspty.Width - 1, uiprspty.Height - 1));
#region SaveMap
//ptr disponse
g.Dispose();
GC.Collect();
//save?
if (uiprspty.bSave)
{
try
{
map.Save(uiprspty.path, System.Drawing.Imaging.ImageFormat.Png);
uiprspty.bSaved = true;
}
catch
{
uiprspty.bSaved = false;
}
}
#endregion
return map;
}
}
public struct UIProgressProperty
{
public int Width;
public int Height;
public bool showText;
public string Text;
public Font textFont;
public Color foreColor;
public Color fColor1;
public Color fColor2;
public Color borderColor;
public Color backColor;
public int Value;
public int maxValue;
public bool bSave;
public bool bSaved;
public string path;
public void SetValueAndSave(int value, int maxvalue, bool save, string savepath)
{
Value = value;
maxValue = maxvalue;
bSave = save;
path = savepath;
}
public void SetThemeOrange(int ctlWidth, int ctlHeight)
{
Width = ctlWidth;
Height = ctlHeight;
showText = true;
textFont = new Font("Arial", 9f);
Text = "";
foreColor = Color.OrangeRed;
borderColor = Color.OrangeRed;
backColor = Color.White;
fColor1 = Color.Orange;
fColor2 = Color.White;
}
public void SetThemeGreen(int ctlWidth, int ctlHeight)
{
Width = ctlWidth;
Height = ctlHeight;
showText = true;
textFont = new Font("Arial", 9f);
Text = "";
foreColor = Color.Green;
borderColor = Color.Green;
backColor = Color.White;
fColor1 = Color.FromArgb(16, 217, 3);
fColor2 = Color.White;
}
本篇文章来源于 精品文章收藏网 原文链接:http://www.boy5d.com/article/html/2007-11/9.html