LNotes-ASP.NET-1)画图,水印,2)验证码,3)饼状图

ylbtech-LearningNotes:LNotes-ASP.NET-1)画图,水印,2)验证码,3)饼状图
 
1.A,学习课程-知识点

 主要案例:1)画图,水印,2)验证码,3)饼状图。

1.B,课堂笔记及课下总结
C#18__画图,水印
步骤: 主要案例:1)画图,水印,2)验证码,3)饼状图,  
 
  1)创建画图
System.Drawing.Bitmap image = new Bitmap(w,h);
 
  2)取画笔
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
 
  3)设置背景色
g.Clear(System.Drawing.Color.Yellow);
 
  4)画图 //1,画线
g.DrawLine(new Pen(Color.Bark),x,y,w,h);
 
    //2,画随园
g.DrawEllipse(new Pen(Color.Green),x,y,w,h);
 
    //3,画弧线
g.DrawArc(new Pen(Color.Red),x,y,w,h,sin1,sin2);
 
    实心体  
    实心长方形
g.FillRectangle(new SolidBrush(Color.Blue),x,y,w,h);
 
    画实心半圆角
g.FillPie(new SolidBrush(Color.FromArgb(red,green,blue)), x, y, w, h, beginSin, endSing);
 
  5)字符串(重点) //画字符串
g.DrawString("梦之星蓝",new Font("宋体",33), new SolidBrush(Color.LightSteelBlue), x, y);
 
  6)存图 //存为网页输出
image.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
 
    直接生成图片文件
image.Save(Server.MapPath("a1.jpg"));
 
例题: 完整步骤  
 

注意:

要释放g和image

System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("Images/sansan.jpg"));
Graphics g = Graphics.FromImage(image); image.Dispose();

try
{
g.DrawString("lb.com", new Font("宋体", 44, FontStyle.Bold), new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, 0), new Point(200, 30), Color.Gray, Color.Green), image.Width - 280, image.Height - 60);
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
finally
{
g.Dispose();
image.Dispose();
}

 
相关对象    
颜色 Color Color c= Color.FromArgb(ran.Next(256), ran.Next(256), ran.Next(256))  
刷子 SolidBrush 1),普通刷子
new SolidBrush(Color.LightSteelBlue)
 
    2),渐变刷子
new System.Drawing.Drawing2D.LinearGradientBrush(new Point(0, 0), new Point(200, 30), Color.Gray, Color.Green)
 
  相关知识    
  random Random ran = new Random(); 方法: ran.Next(max)  
       
2,验证码

//数值型的
private string GetNumberCheckCode()
{
string s1 = "";
Random ran = new Random();

for (int i = 0; i < 4; i++)
{
s1 = s1 + ran.Next(10);
}
return s1;
}

//字符型的
private string GetTextCheckCode()
{
Random ran=new Random();
string s = "的人大会回电和好龙二三四九天气王与鱼羊";
string s1 = "";
for (int i = 0; i < 3; i++)
{
char c=s[ran.Next(s.Length)];
if(s1.IndexOf(c)!=-1)
{
i--;
}
else{
s1 = s1 + c;
}
}
return s1;
}

protected void Page_Load(object sender, EventArgs e)
{
int width = 66;
int height = 24;

Bitmap image = new Bitmap(width, height);
Graphics g = Graphics.FromImage(image);

g.Clear(Color.YellowGreen);

//得到随机的 验证码//画到图片上
string code = this.GetNumberCheckCode();
g.DrawString(code, new Font("隶书", 20, FontStyle.Bold), new SolidBrush(Color.Blue), 2, -2);

//string code = this.GetTextCheckCode();
//g.DrawString(code, new Font("隶书", 16, FontStyle.Italic), new SolidBrush(Color.Blue), -2,0);

//存入session
Session["code"] = code;

//10条干扰线
Random ran=new Random();
for (int i = 0; i < 10; i++)
{
g.DrawLine(new Pen(Color.Gray), ran.Next(width), ran.Next(height), ran.Next(width), ran.Next(height));
}

//100个干扰点
for (int i = 0; i < 100; i++)
{
image.SetPixel(ran.Next(width), ran.Next(height), Color.FromArgb(ran.Next(256), ran.Next(256), ran.Next(256)));
}

image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
}

 
     
3)饼状图    
 

private void BindVote()
{
DataSet d = new DataSet();
d.ReadXml(Server.MapPath("vote.xml"));
DataTable dt = d.Tables[0];
float sum = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
sum = sum + Convert.ToInt32(dt.Rows[i][2]);
}

Bitmap image = new Bitmap(500, 520);
Graphics g = System.Drawing.Graphics.FromImage(image);
g.Clear(Color.Gold);

Random ran = new Random();
float begin = ran.Next(356);
float end = 0;


for (int i = 0; i < dt.Rows.Count; i++)
{
end = Convert.ToInt32(dt.Rows[i][2]) / sum * 360;
g.FillPie(new SolidBrush(Color.FromArgb(ran.Next(256),ran.Next(256),ran.Next(256))), 10, 10, 200, 200, begin, end);
begin = begin+end;
g.DrawString(dt.Rows[i][1].ToString() , new Font("宋体", 14), new SolidBrush(Color.Green), 240, 60+i * 30);

g.FillRectangle(new SolidBrush(Color.GreenYellow), 300,20, 20, 60 + i * 30);

}

image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

}

 
2010.4.20 啊 这一阵 太忙了 最近你们好吗 ?    
  你可能只是这世界的一个人,但对某个人来说,你就是全世界。  
     
1.C,升华提升|领悟|感知|天马行空-痴人说梦

 无。

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2013-02-25 13:55  ylbtech  阅读(230)  评论(0编辑  收藏  举报