注册 登录
编程论坛 ASP.NET技术论坛

ASP.NET动态生成进度条图片

jacklee 发布于 2007-10-29 12:01, 7896 次点击

效果图片:

只有本站会员才能查看附件,请 登录


//Jack.Lee 2007-10-28
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;
}
}

只有本站会员才能查看附件,请 登录

[此贴子已经被作者于2007-10-29 12:03:14编辑过]

32 回复
#2
bygg2007-10-29 13:01
不错啊..呵,学习
#3
sam08022007-10-29 18:11
看不懂
#4
垃圾的沉默2007-10-29 18:39

努力学习当中

#5
jacklee2007-10-30 07:53
就一个结构和一个类,现实的来说是一个函数。。。函数调用结构。不难.GDI。
#6
Hunt2007-10-30 18:26
目前还看不懂,呵呵,刚开始学习网页,才上了没有几节课
#7
jacklee2007-10-30 20:15
这也不完全算是WEB。是和WIN共享。。。
#8
lilyguaiguai2007-10-31 15:48
学习学习!
#9
guoxhvip2007-10-31 16:34
学习学习
#10
skylence2007-11-09 22:08
弄下来玩玩  呵呵  谢谢啊
#11
笑看人生活快乐2007-11-10 12:12

请问有做好的例子吗?可以给我传一份吗l168174128@126.com

#12
By17822007-11-10 12:58
楼主能否再解释一下,这个类中的方法所需要的参数是什么
比如在上传的时候,想显示进度条!

UIMaps mp=new UIMaps();
mp.BuildDefault(xxxxx);

这里面的XXXX应该是什么值呢?

请教,谢谢您
#13
jacklee2007-11-11 09:31
public struct UIProgressProperty
是这个值,一个结构。保存着进度条的数据属性。

那个结构里面提供了两种默认的。你可以申明一个结构调用函数,自动生成样式。。。再BUILDEFAULT就有图了。
#14
By17822007-11-11 11:40
斑竹,帖子调用这个类的代码吧,我试了一晚上都没有见到效果
不死心!!!
#15
yms1232007-11-11 20:47
.NET在网页上画图比ASP要方便多了。
#16
jacklee2007-11-12 08:09

是啊,他有GDI系统。。。



斑竹,帖子调用这个类的代码吧,我试了一晚上都没有见到效果
不死心!!!
不是吧?/LH。。。。得到图片可以用WEB的OUTPUT直接输出到网页。。
兄弟我帮你写个例子吧。。
UIProgressProperty uidata;
uidata.SetThemeGreen(100,18);//设置高度宽度
uidata.SetValueAndSave(99,100,true,"c:\\");//set value & maxvalue ..后两个参数设置为TRUE为保存文件到本地,。。路径
UIMaps map;
Image img=(Image)map.BuildDefault(uidata);
//OK图片得到了。后面随便你怎么处置图片了。是直接OUTPUT还是保存出来(已保存在C:\\)调用自己处理。

#17
wuhongyao32007-11-12 14:57
呵呵,有点懵........
#18
By17822007-11-12 16:39

谢谢 jacklee斑竹

我也懵了, 进度条生成也是用在操作时间比较长的地方使用哈
比如上传文件的时候是否可以显示他的进度
此时该如何调用它?

能否详细一些或者给个例子吧,小弟对这个类实在太有兴趣,但是整到现在还是没有弄明白!

不明白斑竹的得到图片后可以直接输出到网页上或者保存到本地,这个用处是什么?
照斑竹的方法试了一下,出错了,
Image是不明确的引用
using System.Drawing.Imaging;
using System.Drawing;
加了名字空间都不行!

跪谢了,等待中.........

#19
zhishiouran2007-11-12 17:51
不错不错 正需要这方面的资料呢 谢谢
#20
jacklee2007-11-14 07:38
以下是引用By1782在2007-11-12 16:39:30的发言:

谢谢 jacklee斑竹

我也懵了, 进度条生成也是用在操作时间比较长的地方使用哈
比如上传文件的时候是否可以显示他的进度
此时该如何调用它?

能否详细一些或者给个例子吧,小弟对这个类实在太有兴趣,但是整到现在还是没有弄明白!

不明白斑竹的得到图片后可以直接输出到网页上或者保存到本地,这个用处是什么?
照斑竹的方法试了一下,出错了,
Image是不明确的引用
using System.Drawing.Imaging;
using System.Drawing;
加了名字空间都不行!

跪谢了,等待中.........

大家如此关注,我今天中午写个DEMO吧,不过这只是静态的,可以拿来很好的显示比例有如此作用。如要改成动态进度,必须要用到ATLAS技术。你可以扩展写成组件就可以像WINDOWS一样了。

#21
cyyu_ryh2007-11-15 13:33
太深奥了
#22
jacklee2007-11-16 09:10
#23
一水先生2007-11-16 12:17
我觉得不错,收藏到我的小站上去了
http://www.boy5d.com/article/html/2007-11/9.html
觉得不妥的话,可以删了,
同时也欢迎大家推荐好文章,作品.静候佳音!
#24
jacklee2007-11-19 08:06

可以!

#25
melack2007-11-19 09:11

不错收了

#26
一水先生2007-11-20 18:20
有好文章可以继续推荐!
谢谢!
#27
wangchen2232007-11-26 16:47
我也学习学习,哈哈
#28
eakcon2007-11-28 12:10
2
發這個有什么意義啊?
#29
taiyangcn2007-12-07 21:07
有点看不懂刚刚开始学习
#30
hoddog2008-02-27 23:05
不错,我顶
不错我顶,收藏,沙发
#31
maomao08082008-10-29 16:07
是真正的进度条吗?
#32
小瘪三的烦恼2008-10-30 12:07
代码太长了...如果有注释就更好了
1