您的位置:首页 > 其它

生成随机图片

2007-10-15 09:36 477 查看
using System;

using System.Collections.Generic;

using System.Text;

using System.Drawing.Imaging;

using System.Drawing;

using System.IO;

using System.Drawing.Text;

namespace CustomerControls

{

#region 随机字符类

/// <summary>

/// 随机字符类

/// </summary>

class charact

{

#region 字符真值

/// <summary>

/// 字符真值

/// </summary>

string _value;

public string Value

{

set { this._value = value; }

get { return this._value; }

}

#endregion

#region 字体名

/// <summary>

/// 字体名

/// </summary>

int _fontNameId;

public int FontNameID

{

set { this._fontNameId = value; }

get { return this._fontNameId; }

}

#endregion

#region 颜色名

/// <summary>

/// 颜色名

/// </summary>

int _ColorNameId;

public int ColorNameID

{

set { this._ColorNameId = value; }

get { return this._ColorNameId; }

}

#endregion

}

#endregion

#region 生成随机字符串类

/// <summary>

/// 生成随机字符串类

/// </summary>

public class RandomBmp

{

#region 变量定义

/// <summary>

/// 定义颜色名常量表

/// </summary>

string[] ColorList = { "Black", "Blue", "Green", "DarkRed", "Red", "Purple" };

/// <summary>

/// 定义字体名常量表

/// </summary>

string[] FontNameList = { "Arial", "Arial Black", "MS Gothic", "MS Mincho", "MS UI Gothic" };

/// <summary>

/// 定义随机字符对象列表对象

/// </summary>

System.Collections.Generic.List<charact> charactlist = new List<charact>();

#endregion

#region 属性

private int _numcount=3;

/// <summary>

/// 随机字符串个数

/// </summary>

public int StrCount

{

set { this._numcount = value; }

get { return this._numcount; }

}

#endregion

#region 构造函数

/// <summary>

/// 生成随机字符串

/// </summary>

public RandomBmp()

{

//charactlist = new List<charact>();

RndNum(_numcount);

}

/// <summary>

/// 根据给定的个数生成随机字符串

/// </summary>

/// <param name="num">随机字符串个数</param>

public RandomBmp(int num)

{

this._numcount = num;

RndNum(_numcount);

}

#endregion

#region 根据字符串生成随机串图片

/// <summary>

/// 根据字符串生成随机串图片

/// </summary>

/// <param name="randomstrs">随机字符对象转化后字符串形式由ConverRandomObjectToStr方法获得</param>

/// <returns>图片流</returns>

/// <example>

/// 参看示例网站

/// </example>

public static MemoryStream GetImageBytes(string randomstrs)

{

//定义画笔初始位置

int strx = 3;

int stry = 3;

//定义图形对象

System.Drawing.Bitmap Img = null;

Graphics g = null;

MemoryStream ms = null;

//生成随机生成器

Random random = new Random();

//产生随机字符对象列 randomstrs

RandomBmp rb = new RandomBmp();

List<charact> charatemp = rb.ConvertStrToRandomObject(randomstrs);

//定义图片宽度

int gheight = charatemp.Count * 18;

//实例化图片对象

Img = new Bitmap(gheight, 30);

//实例化 Graphics

g = Graphics.FromImage(Img);

//设置背景颜色

g.Clear(Color.White);

//画图片的干扰线

for (int i = 0; i < 10; i++)

{

int x1 = random.Next(Img.Width);

int x2 = random.Next(Img.Width);

int y1 = random.Next(Img.Height);

int y2 = random.Next(Img.Height);

g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);

}

//向图片上画文字

foreach (charact chara in charatemp)

{

Font f = new Font(rb.GetFontNameByRandom(chara.FontNameID), 16);

Color c = Color.FromName(rb.GetColorNameByRandom(chara.ColorNameID));

SolidBrush s = new SolidBrush(c);

g.DrawString(chara.Value, f, s, strx, stry);

strx += 16;

}

//画图片的前景干扰点

for (int i = 0; i < 30; i++)

{

int x = random.Next(Img.Width);

int y = random.Next(Img.Height);

Img.SetPixel(x, y, Color.FromArgb(random.Next()));

}

//保存图象流

ms = new MemoryStream();

Img.Save(ms, ImageFormat.Jpeg);

//关闭图形对象

g.Dispose();

Img.Dispose();

//返回图形流

return ms;

}

#endregion

#region 根据随机数获取字体名称

/// <summary>

/// 根据随机数获取字体名称

/// </summary>

/// <param name="ran">随机数</param>

/// <returns>字体名称</returns>

private string GetFontNameByRandom(int ran)

{

return FontNameList[ran].ToString();

}

#endregion

#region 根据随机数获取颜色名称

/// <summary>

/// 根据随机数获取颜色名称

/// </summary>

/// <param name="ran">随机数</param>

/// <returns>颜色名称</returns>

private string GetColorNameByRandom(int ran)

{

return ColorList[ran].ToString();

}

#endregion

#region 获取字体名串个数

/// <summary>

/// 获取字体名串个数

/// </summary>

/// <returns>字体名串个数</returns>

private int GetFontNameCount()

{

return FontNameList.Length;

}

#endregion

#region 获取颜色串个数

/// <summary>

/// 获取颜色串个数

/// </summary>

/// <returns>颜色串个数</returns>

private int GetColorNameCount()

{

return ColorList.Length;

}

#endregion

#region 指定数目创建随字字符串

/// <summary>

/// 指定数目创建随字字符串

/// </summary>

/// <param name="VcodeNum">指定数目</param>

private void RndNum(int VcodeNum)

{

string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +

",q,r,s,t,u,v,w,x,y,z";

string[] VcArray = Vchar.Split(new Char[] { ',' });

int vtemp = -1;

int ctemp = -1;

int ftemp = -1;

charactlist.Clear();

Random rand = new Random();

for (int i = 1; i < VcodeNum + 1; i++)

{

charact chara = new charact();

//随机产生真值

if (vtemp != -1)

{

rand = new Random(i * vtemp * unchecked((int)DateTime.Now.Ticks));

}

int vt = rand.Next(35);

vtemp = vt;

chara.Value = VcArray[vt];

//随机产生颜色

if (ctemp != -1)

{

rand = new Random(i * ctemp * unchecked((int)DateTime.Now.Ticks));

}

int ct = rand.Next(GetColorNameCount());

ctemp = ct;

chara.ColorNameID = ct;

//随机产生字体

if (ftemp != -1)

{

rand = new Random(i * ftemp * unchecked((int)DateTime.Now.Ticks));

}

int ft = rand.Next(GetFontNameCount());

ftemp = ft;

chara.FontNameID = ft;

charactlist.Add(chara);

}

}

#endregion

#region 获取生成的随机字符串

/// <summary>

/// 获取生成的随机字符串

/// </summary>

/// <returns>随机字符串真值</returns>

public string GetRandomValues()

{

string result = "";

foreach (charact c in charactlist)

{

result += c.Value.ToString();

}

return result;

}

#endregion

#region 字符串转换成随机串对象

/// <summary>

/// 字符串转换成随机串对象

/// </summary>

/// <param name="strs"></param>

/// <returns>随机串对象List<charact></returns>

private List<charact> ConvertStrToRandomObject(string strs)

{

List<charact> result = new List<charact>();

foreach (string s in strs.Split('|'))

{

charact cha = new charact();

string[] temp = s.Split(',');

cha.Value = temp[0];

cha.FontNameID = int.Parse(temp[1]);

cha.ColorNameID = int.Parse(temp[2]);

result.Add(cha);

}

return result;

}

#endregion

#region 随机串对象转换成字符串

/// <summary>

/// 随机串对象转换成字符串

/// </summary>

/// <returns>字符串</returns>

public string ConverRandomObjectToStr()

{

string result = "";

foreach (charact s in charactlist)

{

result += s.Value + ",";

result += s.FontNameID + ",";

result += s.ColorNameID + "|";

}

return result.Substring(0, result.Length - 1);

}

#endregion

}

#endregion

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: