zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Drawing.Imaging;usingSystem.Drawing;usingSystem.IO;us

大家好,欢迎来到IT知识分享网。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using zhxt.Framework.Entity;

namespace zhxt.Framework
{
/// <summary>
/// 验证码生成相关方法
/// </summary>
public class IdentityCode
{
int _numberCount = 0;
public IdentityCode(int count)
{
_numberCount = count;
}
public OpResult<Tuple<string, Image>> CreateIdentityImage()
{
OpResult<Tuple<string, Image>> result = new OpResult<Tuple<string, Image>>();
try
{
string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字体列表,用于验证码
string[] font = { “Times New Roman”, “MS Mincho”, “Book Antiqua”, “Gungsuh”, “PMingLiU”, “Impact” };
//验证码的字符集,去掉了一些容易混淆的字符
char[] character = { ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘8’, ‘9’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘P’, ‘R’, ‘S’, ‘T’, ‘W’, ‘X’, ‘Y’ };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < _numberCount; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
Bitmap bmp = new Bitmap(_numberCount * 20 + 16, 40);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 10; i++)
{
int x1 = rnd.Next(bmp.Width);
int y1 = rnd.Next(bmp.Height);
int x2 = rnd.Next(bmp.Width);
int y2 = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 18);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8);
}
//画噪点
for (int i = 0; i < 100; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
result.Result = Tuple.Create<string, Image>(chkCode, bmp);
}
catch (Exception ex)
{
result.SetErrorInfo(“生成验证码图片失败:” + ex.Message);
}
return result;
}

/// <summary>
/// base64字符串转图像
/// </summary>
/// <param name=”base64Str”>base64字符串</param>
/// <returns></returns>
public OpResult<Image> Base64StringToImage(string base64Str)
{
OpResult<Image> result = new OpResult<Image>();
try
{
byte[] arr = Convert.FromBase64String(base64Str);
MemoryStream ms = new MemoryStream(arr);
result.Result = new Bitmap(ms);
}
catch (Exception ex)
{
result.SetErrorInfo(“Base64str转image异常:” + ex.Message);
}
return result;
}

/// <summary>
/// 图像转base64
/// </summary>
/// <param name=”img”>图像</param>
/// <returns></returns>
public OpResult<string> ImgToBase64String(Image img)
{
OpResult<string> result = new OpResult<string>();
try
{
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
result.Result = Convert.ToBase64String(arr);
}
catch (Exception ex)
{
result.SetErrorInfo(“image转Base64str异常:” + ex.Message);
}
return result;

}

/// <summary>
/// 图像转base64
/// </summary>
/// <param name=”fileFullPath”>图像路径</param>
/// <returns></returns>
public OpResult ImgToBase64String(string fileFullPath)
{
OpResult<string> result = new OpResult<string>();
try
{
if (!File.Exists(fileFullPath))
{
result.SetErrorInfo(“指定文件不存在!”);
return result;
}
using (FileStream fs = new FileStream(fileFullPath, FileMode.Open))
{
if (fs.Length > int.MaxValue)
{
result.SetErrorInfo(“图片容量太大,无法读取!”);
return result;
}
byte[] arr = new byte[fs.Length];
fs.Read(arr, 0, Convert.ToInt32(fs.Length));
result.Result = Convert.ToBase64String(arr);
}
}
catch (Exception ex)
{
result.SetErrorInfo(“image转Base64str异常:” + ex.Message);
}
return result;
}
}
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace zhxt.Framework.Entity
{
class PagingInfo
{
public int CurrentPage { get; set; }
public int PageSize { get; set; }
public long Records { get; set; }
public int TotalPage { get; set; }
}
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace zhxt.Framework.Entity
{
/// <summary>
/// 操作结果
/// </summary>
[Serializable]
public class OpResult
{
/// <summary>
/// Initializes a new instance of the <see cref=”OpResult” /> class
/// </summary>
public OpResult()
{
IsSuccess = true;
}

/// <summary>
/// 是否成功
/// </summary>
public bool IsSuccess { get; set; }

/// <summary>
/// 出错信息
/// </summary>
public string ErrorInfo { get; set; }

/// <summary>
/// 设置错误信息
/// </summary>
/// <param name=”errorInfo”>错误信息</param>
public void SetErrorInfo(string errorInfo)
{
IsSuccess = false;
ErrorInfo = errorInfo;
}
}

/// <summary>
/// 操作结果统一结构
/// </summary>
/// <typeparam name=”T”>结果值</typeparam>
[Serializable]
public class OpResult<T> : OpResult
{
/// <summary>
/// Initializes a new instance of the <see cref=”OpResult{T}” /> class
/// </summary>
public OpResult()
: base()
{
this.Result = default(T);
}

/// <summary>
/// Initializes a new instance of the <see cref=”OpResult{T}” /> class
/// </summary>
/// <param name=”result”>结果信息</param>
public OpResult(OpResult result)
{
this.IsSuccess = result.IsSuccess;
this.ErrorInfo = result.ErrorInfo;
this.Result = default(T);
}

/// <summary>
/// 结果值
/// </summary>
public T Result { get; set; }
}
}

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/30063.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信