请教类代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web;
using System.Configuration;
using System.IO;
using System.Text;
namespace CqyCommon
{
public abstract class commonclass
{
public static string GetClientIP()
{
string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.UserHostAddress;
}
return result;
}
public static byte[] getCodeImage(string code, int p_h)
{
code = getCodeText(code);
int p_w = code.Length;
p_h = p_h + 20;
Bitmap myBitmap = new Bitmap(p_w, p_h);
Graphics myGrap = Graphics.FromImage(myBitmap);
myGrap.Clear(Color.White);
for (int i = 0; i < p_w; i++)
{
Pen myPen = new Pen(Color.White, 1);
if (code.Substring(i, 1) == "|")
{
myPen.Color = Color.Black;
}
// myGrap.DrawString(_code.Substring(i, 1), new Font("宋体", 12), new SolidBrush(Color.Black), i*13, 20);
myGrap.DrawLine(myPen, i, 0, i, p_h);
}
myGrap.DrawString(code, new Font("Courier New", 10), new SolidBrush(Color.Black), -4, p_h);
MemoryStream ms = new MemoryStream();
myBitmap.Save(ms, ImageFormat.Jpeg);
byte[] myImage = null;
myImage = ms.GetBuffer();
return myImage;
}
public static string getCodeText(string n)
{
string zf = n.ToLower();
zf = zf.Replace("0", "_|_|__||_||_|");
zf = zf.Replace("1", "_||_|__|_|_||");
zf = zf.Replace("2", "_|_||__|_|_||");
zf = zf.Replace("3", "_||_||__|_|_|");
zf = zf.Replace("4", "_|_|__||_|_||");
zf = zf.Replace("5", "_||_|__||_|_|");
zf = zf.Replace("7", "_|_|__|_||_||");
zf = zf.Replace("6", "_|_||__||_|_|");
zf = zf.Replace("8", "_||_|__|_||_|");
zf = zf.Replace("9", "_|_||__|_||_|");
zf = zf.Replace("a", "_||_|_|__|_||");
zf = zf.Replace("b", "_|_||_|__|_||");
zf = zf.Replace("c", "_||_||_|__|_|");
zf = zf.Replace("d", "_|_|_||__|_||");
zf = zf.Replace("e", "_||_|_||__|_|");
zf = zf.Replace("f", "_|_||_||__|_|");
zf = zf.Replace("g", "_|_|_|__||_||");
zf = zf.Replace("h", "_||_|_|__||_|");
zf = zf.Replace("i", "_|_||_|__||_|");
zf = zf.Replace("j", "_|_|_||__||_|");
zf = zf.Replace("k", "_||_|_|_|__||");
zf = zf.Replace("l", "_|_||_|_|__||");
zf = zf.Replace("m", "_||_||_|_|__|");
zf = zf.Replace("n", "_|_|_||_|__||");
zf = zf.Replace("o", "_||_|_||_|__|");
zf = zf.Replace("p", "_|_||_||_|__|");
zf = zf.Replace("r", "_||_|_|_||__|");
zf = zf.Replace("q", "_|_|_|_||__||");
zf = zf.Replace("s", "_|_||_|_||__|");
zf = zf.Replace("t", "_|_|_||_||__|");
zf = zf.Replace("u", "_||__|_|_|_||");
zf = zf.Replace("v", "_|__||_|_|_||");
zf = zf.Replace("w", "_||__||_|_|_|");
zf = zf.Replace("x", "_|__|_||_|_||");
zf = zf.Replace("y", "_||__|_||_|_|");
zf = zf.Replace("z", "_|__||_||_|_|");
zf = zf.Replace("-", "_|__|_|_||_||");
zf = zf.Replace("*", "_|__|_||_||_|");
zf = zf.Replace("/", "_|__|__|_|__|");
zf = zf.Replace("%", "_|_|__|__|__|");
zf = zf.Replace("+", "_|__|_|__|__|");
zf = zf.Replace(".", "_||__|_|_||_|");
return zf;
}
public static string SubStr(string sString, int nLeng)
{
sString = sString.Trim();
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}
/// <summary>
/// 获取当前模块名称
/// </summary>
public static string GetCurrentURL()
{
string strUrl = HttpContext.Current.Request.Url.PathAndQuery;
strUrl = strUrl.Substring(strUrl.LastIndexOf("/") + 1, strUrl.LastIndexOf(".aspx") - strUrl.LastIndexOf("/") - 1) +".aspx";
return strUrl;
}
/// <summary>
/// 获取单个简体中文字的拼音首字母
/// </summary>
/// <param name="cn">简体中文字</param>
/// <returns>拼音首字母</returns>
public static string getSpell(string cn)
{
byte[] arrCN = Encoding.Default.GetBytes(cn);
if (arrCN.Length > 1)
{
int area = (short)arrCN[0];
int pos = (short)arrCN[1];
int code = (area << 8) + pos;
int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
for (int i = 0; i < 26; i++)
{
int max = 55290;
if (i != 25) max = areacode[i + 1];
if (areacode[i] <= code && code < max)
{
return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
}
}
return ",";
}
else return cn;
}
}
}
请教一下,这段类代码是什么意思啊
[解决办法]
执行一下看看不就得了?
[解决办法]
你运行一下吧,感觉像在弄验证码之类的
[解决办法]
GDI+?
是不是要画什么东西的?类似水印效果的?有没有前台的啊,如果是单纯的类的话,添加到后台代码里去试试
[解决办法]
这个方法名说的很清楚啊
GetClientIP() 获取IP的。看看代码,确实是获取IP的
getCodeImage() 把图片转换成流的方式写入数据库的
getCodeText() 替换文本的,比如文本中有0,就替换成_|_|__||_||_| 替换掉后,看起来很高深的代码
SubStr() 截取字符串的
下面的两个方法,已经有注释了。我就不解释了!
[解决办法]
GetClientIP() 得到当前的客户端IP
getCodeImage() 得到图片的byte[]数组
getCodeText() 得到代码内容
GetCurrentURL() 获取当前模块名称的URL
getSpell() 获取单个简体中文字的拼音首字母
[解决办法]
写个测试调用下。