标签:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Security.Cryptography;
using System.Net;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Web.Mail;
using System.Collections;
using System.Data;using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace PubClass
{
public class PubFunction
{
#region MD5加密
public string MD5(string DataStr)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(DataStr, "md5");
}
public string MD5_16(string DataStr)
{
string md5str = this.MD5(DataStr);
return md5str.Substring(8, 16);
}
#endregion
#region 随机字串
/// <summary>
/// 生成指定长度的随机字串
/// </summary>
/// <param name="len">长度</param>
/// <returns>随机字串</returns>
public string GetRandomizedString(int len)
{
return this.GetRandomizedString(len, "all");
}
/// <summary>
/// 生成指定长度的随机字串
/// </summary>
/// <param name="len">长度</param>
/// <param name="codes">字串取值范围</param>
/// <returns>随机字串</returns>
public string GetRandomizedString(int len, string codes)
{
string series;
switch (codes)
{
case "number":
series = "01234567890123456789";
break;
case "lower":
series = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
break;
case "upper":
series = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
break;
case "alpha":
series = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
break;
default:
series = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
break;
}
if (len < 1) return "";
int olen = series.Length;
StringBuilder sb = new StringBuilder();
Random rnd = new Random();
for (int i = 0; i < len; i++)
{
sb.Append(series.Substring(rnd.Next(0, olen - 1), 1));
}
return sb.ToString();
}
#endregion
#region 删除特定字符
/// <summary>
/// 清除字符串的非数字、字母(含下划线)和汉字字符
/// </summary>
/// <param name="txt">传入参数</param>
/// <returns>结果字串</returns>
public string delNonePassportAllowChar(string txt)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[\x00-\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\xFF]");
return reg.Replace(txt, "");
}
public bool hasIllegalPassportChar(string txt)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[\x00-\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\xFF]{1,}");
return reg.IsMatch(txt);
}
public string onlyAlphaAndDigit(string txt)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9]");
return reg.Replace(txt, "");
}
public bool hasNoneAlphaAndDigit(string txt)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9]{1,}");
return reg.IsMatch(txt);
}
#endregion
#region 返回文件扩展名
/// <summary>
/// 返回文件扩展名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>小写扩展名</returns>
public string getFileExt(string filename)
{
int lidx = filename.LastIndexOf(".");
if (lidx == filename.Length - 1) return "";
string ext = "";
if (lidx > -1)
{
ext = filename.Substring(lidx + 1).ToLower();
}
return ext;
}
#endregion
#region 截字处理(按字节算)
/// <summary>
/// 截字处理(按字节算)
/// </summary>
/// <param name="str">原始字串</param>
/// <param name="len">字节长度</param>
/// <returns>结果字串</returns>
public string SubStringByByte(string str, int len)
{
char[] cc = str.ToCharArray();
int i = 0, j = 0, code;
for (i = 0, j = 0; i < cc.Length && j < len; i++)
{
code = (int)cc[i];
if (code >= 0 && code <= 255)
{
j++;
}
else
{
j += 2;
}
}
if (i > 0)
{
return str.Substring(0, i);
}
else
{
return "";
}
}
public string SubStringByByte(string str, int len, bool ismore)
{
char[] cc = str.ToCharArray();
int i = 0, j = 0, code;
for (i = 0, j = 0; i < cc.Length && j < len; i++)
{
code = (int)cc[i];
if (code >= 0 && code <= 255)
{
j++;
}
else
{
j += 2;
}
}
if (i > 0)
{
if (!IsBlank(str))
{
if (CalcByteLength(str) > len)
{
return str.Substring(0, i) + "..";
}
else
{
return str.Substring(0, i);
}
}
else
{
return str.Substring(0, i);
}
}
else
{
return "";
}
}
/// <summary>
/// 返回字节长度
/// </summary>
/// <param name="str">计算字串</param>
/// <returns>长度</returns>
public int CalcByteLength(string str)
{
char[] cc = str.ToCharArray();
int i = 0, j = 0, code;
for (i = 0, j = 0; i < cc.Length; i++)
{
code = (int)cc[i];
if (code >= 0 && code <= 255)
{
j++;
}
else
{
j += 2;
}
}
return j;
}
#endregion
#region 检测字串是否是电子邮件地址格式
/// <summary>
/// 检测字串是否是电子邮件地址格式
/// </summary>
/// <param name="email">需要检测的电子邮件地址字串</param>
/// <returns>是否是电子邮件地址格式</returns>
public bool IsEmail(string email)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
return reg.IsMatch(email);
}
#endregion
#region 正则匹配检测
/// <summary>
/// 正则匹配检测
/// </summary>
/// <param name="txt">需要检测的字串</param>
/// <param name="reg">正则表达式</param>
/// <returns>结果</returns>
public bool RegIsMatch(string txt, string reg)
{
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(reg);
return regex.IsMatch(txt);
}
#endregion
#region 获取字符串字节数
public int GetStringByteLength(string str)
{
char[] cc = str.ToCharArray();
int i = 0, j = 0, code;
for (i = 0, j = 0; i < cc.Length; i++)
{
code = (int)cc[i];
if (code >= 0 && code <= 255)
{
j++;
}
else
{
j += 2;
}
}
return j;
}
#endregion
#region 生成以日期时间为基础的不重复数字字串
private Random rnd = new Random();
/// <summary>
/// 生成以日期时间为基础的不重复数字字串
/// </summary>
/// <returns>字串</returns>
public string getIdenticalStringBaseDateTime()
{
return DateTime.Now.ToString("yyMMddHHmm") + rnd.Next(1000, 9999).ToString();
}
/// <summary>
/// 生成以日期时间为基础的不重复数字字串
/// </summary>
/// <returns>字串</returns>
public string getIdenticalStringBaseDateTime2()
{
return DateTime.Now.ToString("yyMMddHHmmss") + rnd.Next(10000, 99999).ToString();
}
#endregion
#region 生成以日期时间为基的SHA1加密字串
/// <summary>
/// 生成以日期时间为基的SHA1加密字串
/// </summary>
/// <returns>40位的加密字串</returns>
public string GetSHA1StringBasedDateTime()
{
string source = DateTime.Now.ToString("yyyyMMddHHss") + rnd.Next(10000, 99999).ToString();
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(source, "sha1");
}
#endregion
#region 表单处理
/// <summary>
/// 表单处理
/// </summary>
/// <param name="str">处理字串</param>
/// <returns>结果字串</returns>
public string TextareaStringToDatabase(string str)
{
StringBuilder sb = new StringBuilder(System.Web.HttpUtility.HtmlEncode(str));
sb = sb.Replace(" "," ");
sb = sb.Replace("\n", "<br />");
return sb.ToString();
}
public string DatabaseStringToTextarea(string str)
{
StringBuilder sb = new StringBuilder(System.Web.HttpUtility.HtmlDecode(str));
sb = sb.Replace(" ", " ");
sb = sb.Replace("<br />", "\n");
return sb.ToString();
}
#endregion
#region 检测MIME字串是否是图片性质
/// <summary>
/// 检测MIME字串是否是图片性质
/// </summary>
/// <param name="mime"></param>
/// <returns></returns>
public bool testMimeIsImage(string mime)
{
if (mime.Length < 7) return false;
string begin = mime.Substring(0, 6).ToLower();
return begin == "image/";
}
#endregion
#region 检测MIME字串是否是音频性质
/// <summary>
/// 检测MIME字串是否是音频性质
/// </summary>
/// <param name="mime"></param>
/// <returns></returns>
public bool testMimeIsAudio(string mime)
{
if (mime.Length < 7) return false;
string begin = mime.Substring(0, 6).ToLower();
return begin == "audio/";
}
#endregion
#region 检测MIME字串是否是视频性质
/// <summary>
/// 检测MIME字串是否是礼频性质
/// </summary>
/// <param name="mime"></param>
/// <returns></returns>
public bool testMimeIsVideo(string mime)
{
if (mime.Length < 7) return false;
string begin = mime.Substring(0, 6).ToLower();
return begin == "video/";
}
#endregion
#region 检测是否是支持的文件扩展名
/// <summary>
/// 检测是否是支持的文件扩展名
/// </summary>
/// <param name="ext">扩展名,如果是空字串,则默认都是不能通过检测</param>
/// <param name="extcoll">扩展句列表,用|分隔,如果是空字串,则默认都是通过检测</param>
/// <returns>结果</returns>
public bool testIsSupportFileExtent(string ext, string extcoll)
{
if (ext == "") return false;
if (extcoll == "") return true;
ext = ext.Trim().ToLower();
char[] sep = { ‘,‘ };
string[] extarr = extcoll.Split(sep);
for (int i = 0; i < extarr.Length; i++)
{
if (extarr[i].Trim().ToLower() == ext) return true;
}
return false;
}
#endregion
#region 文字水印
/// <summary>
///
/// </summary>
/// <param name="page"></param>
/// <param name="path"></param>
/// <returns></returns>
public bool MakeWaterImageWord(System.Web.UI.Page page, string path, string newpath)
{
//加文字水印,
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
//System.Drawing.Image image=System.Drawing.Image.FromStream();
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("Verdana", 16);
Brush b = new SolidBrush(Color.BurlyWood);
string addText = "熊猫伯伯\nhttp://www.pandabobo.cn";
try
{
g.DrawString(addText, f, b, 10, 10);
g.Dispose();
//保存加水印过后的图片,删除原始图片
string newPath = newpath;
image.Save(newPath);
image.Dispose();
if (File.Exists(path))
{
File.Delete(path);
}
return true;
}
catch
{
return false;
}
}
public bool MakeWaterImageWord(System.Web.UI.Page page, string path, string newpath, System.IO.Stream stream)
{
//加文字水印,
//System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("Verdana", 16);
Brush b = new SolidBrush(Color.BurlyWood);
string addText = "熊猫伯伯\nhttp://www.pandabobo.cn";
try
{
g.DrawString(addText, f, b, 10, 10);
g.Dispose();
//保存加水印过后的图片,删除原始图片
string newPath = newpath;
image.Save(newPath);
image.Dispose();
if (File.Exists(path))
{
File.Delete(path);
}
return true;
}
catch
{
return false;
}
}
#endregion
#region 图片水印
/// <summary>
/// 水印
/// </summary>
/// <param name="page"></param>
/// <param name="path">图片地址</param>
/// <returns></returns>
public bool MakeWaterImageTwo(string copyImgPath, string path)
{
//加图片水印
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(copyImgPath);
//源图
int sWidth = image.Width;
int sHeight = image.Height;
//水印图
int wWidth = (((copyImage.Width) * (sWidth / copyImage.Width)) * 1) / 3;
int wHeight = (((copyImage.Height) * (sWidth / copyImage.Width)) * 1) / 3;
if (sHeight < (sWidth * 0.382))
{
wWidth = (((copyImage.Width) * (sHeight / copyImage.Height)) * 1) / 3;
wHeight = (((copyImage.Height) * (sHeight / copyImage.Height)) * 1) / 3;
}
//make Graphics.
//Graphics g = Graphics.FromImage(image);
//新建一个bmp图片
System.Drawing.Image BMP = new System.Drawing.Bitmap(sWidth, sHeight);
//新建一个画板
Graphics G = System.Drawing.Graphics.FromImage(BMP);
//设置高质量插值法
G.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
G.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
G.Clear(Color.Transparent);
//加载原始图片
G.DrawImage(image, new Rectangle(0, 0, sWidth, sHeight));
//水印属性
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)100/100f, 0},
new float[] {0, 0, 0, 0, 1}
};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes MarkAttr = new ImageAttributes();
MarkAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
try
{
//G.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height,copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel,MarkAttr);
G.DrawImage(copyImage, new Rectangle(image.Width - wWidth, image.Height - wHeight, wWidth, wHeight), 8, 10, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel, MarkAttr);
image.Dispose();
if (File.Exists(path))
{
File.Delete(path);
}
BMP.Save(path);
BMP.Dispose();
return true;
}
catch
{
return false;
}
}
#endregion
#region 图片水印 2
/// <summary>
/// 生成水印,可按左上、左下、右上、右下、居中、透明度生成文件,只对jpeg或jpg格式有效!
/// </summary>
/// <param name="sourceFile">底图</param>
/// <param name="waterMarkFile">水印图</param>
/// <param name="saveFile">要保存的文件</param>
/// <param name="local">位置:左上(1)、左下(2)、右上(3)、右下(4)、居中(5)</param>
/// <param name="alpha">透明度(1-100)</param>
/// <returns>bool,是否成功</returns>
public bool MakeWaterImage(string sourceFile, string waterMarkFile, string saveFile, int local, int alpha)
{
bool result;
if (!File.Exists(sourceFile) || !File.Exists(waterMarkFile)) //如果源图或水印图不存在
return false;
FileInfo fi = new FileInfo(sourceFile);
//判断文件类型是否合法
if (fi.Extension.ToLower() != ".jpg" & fi.Extension.ToLower() != ".jpeg")
return false;
try
{
//原图
Bitmap sImage = new Bitmap(sourceFile);
int sWidth = sImage.Width;
int sHeight = sImage.Height;
//水印图
Bitmap wImage = new Bitmap(waterMarkFile);
int wWidth = wImage.Width;
int wHeight = wImage.Height;
//make Graphics.
Graphics g = Graphics.FromImage(sImage);
int x; //临时变量
int y; //监时变量
int x1 = 0; //原图和水印图的宽度差,即开始绘图的X位置
int y1 = 0; //原图和水印图的高度差,即开始绘图的Y位置
int w = 0; //生成的水印图的宽度,即结束绘图的X位置
int h = 0; //生成的水印图的高度,即结束绘图的Y位置
int al; //alpha
int rl; //Red
int gl; //Green
int bl; //Blue
//校验透明度
if (alpha < 1 || alpha > 100)
al = 80;
else
al = alpha;
if (sWidth > wWidth & sHeight > wHeight) //如果源图比水印图大
{
switch (local)
{
case 1: //左上
x1 = 0;
y1 = 0;
break;
case 2: //左下
x1 = 0