7 changed files with 500 additions and 245 deletions
-
2src/Shentun.Peis.Application/PacsBusiness/PacsBusinessAppService.cs
-
2src/Shentun.Peis.Application/RegisterCheckPictures/RegisterCheckPictureAppService.cs
-
105src/Shentun.Utilities/DocnetConverter.cs
-
89src/Shentun.Utilities/MagickConverter.cs
-
74src/Shentun.Utilities/PdfPigConverter.cs
-
9src/Shentun.Utilities/Shentun.Utilities.csproj
-
464src/Shentun.Utilities/ValidationCodeHelper.cs
@ -0,0 +1,105 @@ |
|||
using Docnet.Core; |
|||
using Docnet.Core.Models; |
|||
using SkiaSharp; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Drawing; |
|||
using System.Drawing.Imaging; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Runtime.InteropServices; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Shentun.Utilities |
|||
{ |
|||
public class DocnetConverter |
|||
{ |
|||
public static List<string> ConvertPdfToImages(string pdfBase64) |
|||
{ |
|||
List<string> base64Images = new List<string>(); |
|||
|
|||
// 处理可能包含 data:application/pdf;base64, 前缀的情况
|
|||
if (pdfBase64.Contains("base64,")) |
|||
{ |
|||
pdfBase64 = pdfBase64.Substring(pdfBase64.IndexOf(",") + 1); |
|||
} |
|||
|
|||
// 将Base64字符串转换为字节数组
|
|||
byte[] pdfBytes = Convert.FromBase64String(pdfBase64); |
|||
|
|||
// 使用 Docnet 处理
|
|||
using (var docLib = DocLib.Instance) |
|||
using (var docReader = docLib.GetDocReader(pdfBytes, new PageDimensions())) |
|||
{ |
|||
int pageCount = docReader.GetPageCount(); |
|||
|
|||
for (int i = 0; i < pageCount; i++) // Docnet 页码从 0 开始
|
|||
{ |
|||
using (var pageReader = docReader.GetPageReader(i)) |
|||
{ |
|||
// 获取页面尺寸
|
|||
int width = pageReader.GetPageWidth(); |
|||
int height = pageReader.GetPageHeight(); |
|||
|
|||
// 获取 BGRA 原始数据
|
|||
byte[] rawBytes = pageReader.GetImage( |
|||
RenderFlags.RenderAnnotations | RenderFlags.OptimizeTextForLcd |
|||
); |
|||
|
|||
// 创建 Bitmap 并写入像素数据
|
|||
using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb)) |
|||
{ |
|||
// 锁定位图的内存区域
|
|||
var rect = new Rectangle(0, 0, width, height); |
|||
var bmpData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, bitmap.PixelFormat); |
|||
|
|||
try |
|||
{ |
|||
// Docnet 返回的是 BGRA 格式,Bitmap 需要 ARGB 格式
|
|||
// 需要转换颜色顺序:BGRA -> ARGB
|
|||
int stride = bmpData.Stride; |
|||
byte[] argbBytes = new byte[stride * height]; |
|||
|
|||
for (int y = 0; y < height; y++) |
|||
{ |
|||
int rawRowOffset = y * width * 4; |
|||
int argbRowOffset = y * stride; |
|||
|
|||
for (int x = 0; x < width; x++) |
|||
{ |
|||
int rawIndex = rawRowOffset + x * 4; |
|||
int argbIndex = argbRowOffset + x * 4; |
|||
|
|||
// BGRA -> ARGB 转换
|
|||
argbBytes[argbIndex] = rawBytes[rawIndex + 2]; // R
|
|||
argbBytes[argbIndex + 1] = rawBytes[rawIndex + 1]; // G
|
|||
argbBytes[argbIndex + 2] = rawBytes[rawIndex]; // B
|
|||
argbBytes[argbIndex + 3] = rawBytes[rawIndex + 3]; // A
|
|||
} |
|||
} |
|||
|
|||
// 复制转换后的数据到位图
|
|||
Marshal.Copy(argbBytes, 0, bmpData.Scan0, argbBytes.Length); |
|||
} |
|||
finally |
|||
{ |
|||
bitmap.UnlockBits(bmpData); |
|||
} |
|||
|
|||
// 保存为 PNG 并转换为 Base64
|
|||
using (var ms = new MemoryStream()) |
|||
{ |
|||
bitmap.Save(ms, ImageFormat.Png); |
|||
string base64 = Convert.ToBase64String(ms.ToArray()); |
|||
base64Images.Add(base64); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
return base64Images; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,89 @@ |
|||
using ImageMagick; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Shentun.Utilities |
|||
{ |
|||
public class MagickConverter |
|||
{ |
|||
//static MagickConverter()
|
|||
//{
|
|||
// // 设置32位Ghostscript的目录
|
|||
// // 根据您的实际安装路径修改
|
|||
// MagickNET.SetGhostscriptDirectory(@"C:\Program Files\gs\gs10.04.0\bin");
|
|||
|
|||
// // 或者直接指定可执行文件路径
|
|||
// // MagickNET.SetGhostscriptExecutablePath(@"C:\Program Files (x86)\gs\gs10.03.0\bin\gswin32c.exe");
|
|||
//}
|
|||
|
|||
public static List<string> ConvertPdfToImages(string pdfBase64, int dpi = 200, MagickFormat imageFormat = MagickFormat.Jpeg, int jpegQuality = 85, int startPage = 1, int? pageCount = null) |
|||
{ |
|||
var base64Images = new List<string>(); |
|||
|
|||
// 1. 处理 Base64 前缀
|
|||
if (pdfBase64.Contains("base64,")) |
|||
{ |
|||
pdfBase64 = pdfBase64.Substring(pdfBase64.IndexOf(",") + 1); |
|||
} |
|||
|
|||
// 2. 将 Base64 转换为字节数组
|
|||
byte[] pdfBytes = Convert.FromBase64String(pdfBase64); |
|||
|
|||
// 3. 使用 Magick.NET 处理 PDF
|
|||
var settings = new MagickReadSettings |
|||
{ |
|||
Density = new Density(dpi, dpi) |
|||
}; |
|||
|
|||
// 如果指定了分页参数,则设置
|
|||
if (startPage > 1 || pageCount.HasValue) |
|||
{ |
|||
settings.FrameIndex = (uint)(startPage - 1); // 强制转换为 uint
|
|||
if (pageCount.HasValue) |
|||
{ |
|||
settings.FrameCount = (uint)pageCount.Value; // 强制转换为 uint
|
|||
} |
|||
} |
|||
|
|||
using (var memoryStream = new MemoryStream(pdfBytes)) |
|||
using (var images = new MagickImageCollection()) |
|||
{ |
|||
// 读取 PDF
|
|||
images.Read(memoryStream, settings); |
|||
|
|||
// 遍历每一页
|
|||
for (int i = 0; i < images.Count; i++) |
|||
{ |
|||
using (var image = images[i]) |
|||
{ |
|||
// 解决某些 PDF 电子签章导致的黑屏问题
|
|||
image.Alpha(AlphaOption.Remove); |
|||
|
|||
// 设置输出格式
|
|||
image.Format = imageFormat; |
|||
|
|||
// 如果是 JPEG,设置质量
|
|||
if (imageFormat == MagickFormat.Jpeg) |
|||
{ |
|||
image.Quality = (uint)jpegQuality; |
|||
} |
|||
|
|||
// 转换为 Base64
|
|||
using (var outputStream = new MemoryStream()) |
|||
{ |
|||
image.Write(outputStream); |
|||
string base64 = Convert.ToBase64String(outputStream.ToArray()); |
|||
base64Images.Add(base64); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
return base64Images; |
|||
} |
|||
} |
|||
} |
|||
@ -1,256 +1,256 @@ |
|||
using System; |
|||
using System.DrawingCore; |
|||
using System.DrawingCore.Drawing2D; |
|||
using System.DrawingCore.Imaging; |
|||
//using System.DrawingCore;
|
|||
//using System.DrawingCore.Drawing2D;
|
|||
//using System.DrawingCore.Imaging;
|
|||
using System.IO; |
|||
namespace Shentun.Utilities |
|||
{ |
|||
public sealed class ValidationCodeHelper |
|||
{ |
|||
#region 单例模式
|
|||
//创建私有化静态obj锁
|
|||
private static readonly object _lockObject = new object(); |
|||
//创建私有静态字段,接收类的实例化对象
|
|||
private static ValidationCodeHelper _validationCodeHelper = null; |
|||
//构造函数私有化
|
|||
private ValidationCodeHelper() { } |
|||
//创建单利对象资源并返回
|
|||
public static ValidationCodeHelper Instance |
|||
{ |
|||
get { |
|||
if (_validationCodeHelper == null) |
|||
{ |
|||
lock (_lockObject) |
|||
{ |
|||
if (_validationCodeHelper == null) |
|||
_validationCodeHelper = new ValidationCodeHelper(); |
|||
} |
|||
} |
|||
return _validationCodeHelper; |
|||
} |
|||
//public sealed class ValidationCodeHelper
|
|||
//{
|
|||
// #region 单例模式
|
|||
// //创建私有化静态obj锁
|
|||
// private static readonly object _lockObject = new object();
|
|||
// //创建私有静态字段,接收类的实例化对象
|
|||
// private static ValidationCodeHelper _validationCodeHelper = null;
|
|||
// //构造函数私有化
|
|||
// private ValidationCodeHelper() { }
|
|||
// //创建单利对象资源并返回
|
|||
// public static ValidationCodeHelper Instance
|
|||
// {
|
|||
// get {
|
|||
// if (_validationCodeHelper == null)
|
|||
// {
|
|||
// lock (_lockObject)
|
|||
// {
|
|||
// if (_validationCodeHelper == null)
|
|||
// _validationCodeHelper = new ValidationCodeHelper();
|
|||
// }
|
|||
// }
|
|||
// return _validationCodeHelper;
|
|||
// }
|
|||
|
|||
} |
|||
#endregion
|
|||
// }
|
|||
// #endregion
|
|||
|
|||
#region 生产验证码
|
|||
// #region 生产验证码
|
|||
|
|||
|
|||
/// <summary>
|
|||
/// 1.数字验证码
|
|||
/// </summary>
|
|||
/// <param name="length"></param>
|
|||
/// <returns></returns>
|
|||
private string CreateNumberValidationCode(int length) |
|||
{ |
|||
int[] randMembers = new int[length]; |
|||
int[] validateNums = new int[length]; |
|||
string validateNumberStr = ""; |
|||
//生成起始序列值
|
|||
int seekSeek = unchecked((int)DateTime.Now.Ticks); |
|||
Random seekRand = new Random(seekSeek); |
|||
int beginSeek = seekRand.Next(0, Int32.MaxValue - length * 10000); |
|||
int[] seeks = new int[length]; |
|||
for (int i = 0; i < length; i++) |
|||
{ |
|||
beginSeek += 10000; |
|||
seeks[i] = beginSeek; |
|||
} |
|||
//生成随机数字
|
|||
for (int i = 0; i < length; i++) |
|||
{ |
|||
Random rand = new Random(seeks[i]); |
|||
int pownum = 1 * (int)Math.Pow(10, length); |
|||
randMembers[i] = rand.Next(pownum, Int32.MaxValue); |
|||
} |
|||
//抽取随机数字
|
|||
for (int i = 0; i < length; i++) |
|||
{ |
|||
string numStr = randMembers[i].ToString(); |
|||
int numLength = numStr.Length; |
|||
Random rand = new Random(); |
|||
int numPosition = rand.Next(0, numLength - 1); |
|||
validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1)); |
|||
} |
|||
//生成验证码
|
|||
for (int i = 0; i < length; i++) |
|||
{ |
|||
validateNumberStr += validateNums[i].ToString(); |
|||
} |
|||
return validateNumberStr; |
|||
} |
|||
// /// <summary>
|
|||
// /// 1.数字验证码
|
|||
// /// </summary>
|
|||
// /// <param name="length"></param>
|
|||
// /// <returns></returns>
|
|||
// private string CreateNumberValidationCode(int length)
|
|||
// {
|
|||
// int[] randMembers = new int[length];
|
|||
// int[] validateNums = new int[length];
|
|||
// string validateNumberStr = "";
|
|||
// //生成起始序列值
|
|||
// int seekSeek = unchecked((int)DateTime.Now.Ticks);
|
|||
// Random seekRand = new Random(seekSeek);
|
|||
// int beginSeek = seekRand.Next(0, Int32.MaxValue - length * 10000);
|
|||
// int[] seeks = new int[length];
|
|||
// for (int i = 0; i < length; i++)
|
|||
// {
|
|||
// beginSeek += 10000;
|
|||
// seeks[i] = beginSeek;
|
|||
// }
|
|||
// //生成随机数字
|
|||
// for (int i = 0; i < length; i++)
|
|||
// {
|
|||
// Random rand = new Random(seeks[i]);
|
|||
// int pownum = 1 * (int)Math.Pow(10, length);
|
|||
// randMembers[i] = rand.Next(pownum, Int32.MaxValue);
|
|||
// }
|
|||
// //抽取随机数字
|
|||
// for (int i = 0; i < length; i++)
|
|||
// {
|
|||
// string numStr = randMembers[i].ToString();
|
|||
// int numLength = numStr.Length;
|
|||
// Random rand = new Random();
|
|||
// int numPosition = rand.Next(0, numLength - 1);
|
|||
// validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
|
|||
// }
|
|||
// //生成验证码
|
|||
// for (int i = 0; i < length; i++)
|
|||
// {
|
|||
// validateNumberStr += validateNums[i].ToString();
|
|||
// }
|
|||
// return validateNumberStr;
|
|||
// }
|
|||
|
|||
/// <summary>
|
|||
/// 2.字母验证码
|
|||
/// </summary>
|
|||
/// <param name="length">字符长度</param>
|
|||
/// <returns>验证码字符</returns>
|
|||
private string CreateAbcValidationCode(int length) |
|||
{ |
|||
char[] validation = new char[length]; |
|||
char[] dictionary = { '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', |
|||
'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' |
|||
}; |
|||
Random random = new Random(); |
|||
for (int i = 0; i < length; i++) |
|||
{ |
|||
validation[i] = dictionary[random.Next(dictionary.Length - 1)]; |
|||
} |
|||
return new string(validation); |
|||
} |
|||
// /// <summary>
|
|||
// /// 2.字母验证码
|
|||
// /// </summary>
|
|||
// /// <param name="length">字符长度</param>
|
|||
// /// <returns>验证码字符</returns>
|
|||
// private string CreateAbcValidationCode(int length)
|
|||
// {
|
|||
// char[] validation = new char[length];
|
|||
// char[] dictionary = { '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',
|
|||
// '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'
|
|||
// };
|
|||
// Random random = new Random();
|
|||
// for (int i = 0; i < length; i++)
|
|||
// {
|
|||
// validation[i] = dictionary[random.Next(dictionary.Length - 1)];
|
|||
// }
|
|||
// return new string(validation);
|
|||
// }
|
|||
|
|||
/// <summary>
|
|||
/// 3.混合验证码
|
|||
/// </summary>
|
|||
/// <param name="length">字符长度</param>
|
|||
/// <returns>验证码字符</returns>
|
|||
private string CreateMixValidationCode(int length) |
|||
{ |
|||
char[] validation = new char[length]; |
|||
char[] dictionary = { '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', |
|||
'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' |
|||
}; |
|||
Random random = new Random(); |
|||
for (int i = 0; i < length; i++) |
|||
{ |
|||
validation[i] = dictionary[random.Next(dictionary.Length - 1)]; |
|||
} |
|||
return new string(validation); |
|||
} |
|||
// /// <summary>
|
|||
// /// 3.混合验证码
|
|||
// /// </summary>
|
|||
// /// <param name="length">字符长度</param>
|
|||
// /// <returns>验证码字符</returns>
|
|||
// private string CreateMixValidationCode(int length)
|
|||
// {
|
|||
// char[] validation = new char[length];
|
|||
// char[] dictionary = { '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',
|
|||
// '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'
|
|||
// };
|
|||
// Random random = new Random();
|
|||
// for (int i = 0; i < length; i++)
|
|||
// {
|
|||
// validation[i] = dictionary[random.Next(dictionary.Length - 1)];
|
|||
// }
|
|||
// return new string(validation);
|
|||
// }
|
|||
|
|||
/// <summary>
|
|||
/// 产生验证码(随机产生4-6位)
|
|||
/// </summary>
|
|||
/// <param name="type">验证码类型:数字,字符,符合</param>
|
|||
/// <returns></returns>
|
|||
public string CreateValidationCode(ValidationCodeType type, int length = 4) |
|||
{ |
|||
string validationCode = string.Empty; |
|||
Random random = new Random(); |
|||
switch (type) |
|||
{ |
|||
case ValidationCodeType.NumberValidationCode: |
|||
validationCode = Instance.CreateNumberValidationCode(length); |
|||
break; |
|||
case ValidationCodeType.AbcValidationCode: |
|||
validationCode = Instance.CreateAbcValidationCode(length); |
|||
break; |
|||
case ValidationCodeType.MixValidationCode: |
|||
validationCode = Instance.CreateMixValidationCode(length); |
|||
break; |
|||
} |
|||
return validationCode; |
|||
} |
|||
#endregion
|
|||
// /// <summary>
|
|||
// /// 产生验证码(随机产生4-6位)
|
|||
// /// </summary>
|
|||
// /// <param name="type">验证码类型:数字,字符,符合</param>
|
|||
// /// <returns></returns>
|
|||
// public string CreateValidationCode(ValidationCodeType type, int length = 4)
|
|||
// {
|
|||
// string validationCode = string.Empty;
|
|||
// Random random = new Random();
|
|||
// switch (type)
|
|||
// {
|
|||
// case ValidationCodeType.NumberValidationCode:
|
|||
// validationCode = Instance.CreateNumberValidationCode(length);
|
|||
// break;
|
|||
// case ValidationCodeType.AbcValidationCode:
|
|||
// validationCode = Instance.CreateAbcValidationCode(length);
|
|||
// break;
|
|||
// case ValidationCodeType.MixValidationCode:
|
|||
// validationCode = Instance.CreateMixValidationCode(length);
|
|||
// break;
|
|||
// }
|
|||
// return validationCode;
|
|||
// }
|
|||
// #endregion
|
|||
|
|||
#region 验证码图片
|
|||
/// <summary>
|
|||
/// 验证码图片 => Bitmap
|
|||
/// </summary>
|
|||
/// <param name="validationCode">验证码</param>
|
|||
/// <param name="width">宽</param>
|
|||
/// <param name="height">高</param>
|
|||
/// <returns>Bitmap</returns>
|
|||
public Bitmap CreateBitmapByImageValidationCode(string validationCode, int width, int height) |
|||
{ |
|||
Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic)); |
|||
Brush brush; |
|||
Bitmap bitmap = new Bitmap(width, height); |
|||
Graphics g = Graphics.FromImage(bitmap); |
|||
SizeF totalSizeF = g.MeasureString(validationCode, font); |
|||
SizeF curCharSizeF; |
|||
PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2); |
|||
Random random = new Random(); //随机数产生器
|
|||
g.Clear(Color.White); //清空图片背景色
|
|||
for (int i = 0; i < validationCode.Length; i++) |
|||
{ |
|||
brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255))); |
|||
g.DrawString(validationCode[i].ToString(), font, brush, startPointF); |
|||
curCharSizeF = g.MeasureString(validationCode[i].ToString(), font); |
|||
startPointF.X += curCharSizeF.Width; |
|||
} |
|||
// #region 验证码图片
|
|||
// /// <summary>
|
|||
// /// 验证码图片 => Bitmap
|
|||
// /// </summary>
|
|||
// /// <param name="validationCode">验证码</param>
|
|||
// /// <param name="width">宽</param>
|
|||
// /// <param name="height">高</param>
|
|||
// /// <returns>Bitmap</returns>
|
|||
// public Bitmap CreateBitmapByImageValidationCode(string validationCode, int width, int height)
|
|||
// {
|
|||
// Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
|
|||
// Brush brush;
|
|||
// Bitmap bitmap = new Bitmap(width, height);
|
|||
// Graphics g = Graphics.FromImage(bitmap);
|
|||
// SizeF totalSizeF = g.MeasureString(validationCode, font);
|
|||
// SizeF curCharSizeF;
|
|||
// PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2);
|
|||
// Random random = new Random(); //随机数产生器
|
|||
// g.Clear(Color.White); //清空图片背景色
|
|||
// for (int i = 0; i < validationCode.Length; i++)
|
|||
// {
|
|||
// brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));
|
|||
// g.DrawString(validationCode[i].ToString(), font, brush, startPointF);
|
|||
// curCharSizeF = g.MeasureString(validationCode[i].ToString(), font);
|
|||
// startPointF.X += curCharSizeF.Width;
|
|||
// }
|
|||
|
|||
//画图片的干扰线
|
|||
for (int i = 0; i < 10; i++) |
|||
{ |
|||
int x1 = random.Next(bitmap.Width); |
|||
int x2 = random.Next(bitmap.Width); |
|||
int y1 = random.Next(bitmap.Height); |
|||
int y2 = random.Next(bitmap.Height); |
|||
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); |
|||
} |
|||
// //画图片的干扰线
|
|||
// for (int i = 0; i < 10; i++)
|
|||
// {
|
|||
// int x1 = random.Next(bitmap.Width);
|
|||
// int x2 = random.Next(bitmap.Width);
|
|||
// int y1 = random.Next(bitmap.Height);
|
|||
// int y2 = random.Next(bitmap.Height);
|
|||
// g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
|
|||
// }
|
|||
|
|||
//画图片的前景干扰点
|
|||
for (int i = 0; i < 100; i++) |
|||
{ |
|||
int x = random.Next(bitmap.Width); |
|||
int y = random.Next(bitmap.Height); |
|||
bitmap.SetPixel(x, y, Color.FromArgb(random.Next())); |
|||
} |
|||
// //画图片的前景干扰点
|
|||
// for (int i = 0; i < 100; i++)
|
|||
// {
|
|||
// int x = random.Next(bitmap.Width);
|
|||
// int y = random.Next(bitmap.Height);
|
|||
// bitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
|
|||
// }
|
|||
|
|||
g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线
|
|||
g.Dispose(); |
|||
return bitmap; |
|||
} |
|||
// g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线
|
|||
// g.Dispose();
|
|||
// return bitmap;
|
|||
// }
|
|||
|
|||
/// <summary>
|
|||
/// 验证码图片 => byte[]
|
|||
/// </summary>
|
|||
/// <param name="validationCode">验证码</param>
|
|||
/// <param name="width">宽</param>
|
|||
/// <param name="height">高</param>
|
|||
/// <returns>byte[]</returns>
|
|||
public byte[] CreateByteByImageValidationCode(string validationCode, int width, int height) |
|||
{ |
|||
Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic)); |
|||
Brush brush; |
|||
Bitmap bitmap = new Bitmap(width, height); |
|||
Graphics g = Graphics.FromImage(bitmap); |
|||
SizeF totalSizeF = g.MeasureString(validationCode, font); |
|||
SizeF curCharSizeF; |
|||
PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2); |
|||
Random random = new Random(); //随机数产生器
|
|||
g.Clear(Color.White); //清空图片背景色
|
|||
for (int i = 0; i < validationCode.Length; i++) |
|||
{ |
|||
brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255))); |
|||
g.DrawString(validationCode[i].ToString(), font, brush, startPointF); |
|||
curCharSizeF = g.MeasureString(validationCode[i].ToString(), font); |
|||
startPointF.X += curCharSizeF.Width; |
|||
} |
|||
// /// <summary>
|
|||
// /// 验证码图片 => byte[]
|
|||
// /// </summary>
|
|||
// /// <param name="validationCode">验证码</param>
|
|||
// /// <param name="width">宽</param>
|
|||
// /// <param name="height">高</param>
|
|||
// /// <returns>byte[]</returns>
|
|||
// public byte[] CreateByteByImageValidationCode(string validationCode, int width, int height)
|
|||
// {
|
|||
// Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
|
|||
// Brush brush;
|
|||
// Bitmap bitmap = new Bitmap(width, height);
|
|||
// Graphics g = Graphics.FromImage(bitmap);
|
|||
// SizeF totalSizeF = g.MeasureString(validationCode, font);
|
|||
// SizeF curCharSizeF;
|
|||
// PointF startPointF = new PointF(0, (height - totalSizeF.Height) / 2);
|
|||
// Random random = new Random(); //随机数产生器
|
|||
// g.Clear(Color.White); //清空图片背景色
|
|||
// for (int i = 0; i < validationCode.Length; i++)
|
|||
// {
|
|||
// brush = new LinearGradientBrush(new Point(0, 0), new Point(1, 1), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)), Color.FromArgb(random.Next(255), random.Next(255), random.Next(255)));
|
|||
// g.DrawString(validationCode[i].ToString(), font, brush, startPointF);
|
|||
// curCharSizeF = g.MeasureString(validationCode[i].ToString(), font);
|
|||
// startPointF.X += curCharSizeF.Width;
|
|||
// }
|
|||
|
|||
//画图片的干扰线
|
|||
for (int i = 0; i < 10; i++) |
|||
{ |
|||
int x1 = random.Next(bitmap.Width); |
|||
int x2 = random.Next(bitmap.Width); |
|||
int y1 = random.Next(bitmap.Height); |
|||
int y2 = random.Next(bitmap.Height); |
|||
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); |
|||
} |
|||
// //画图片的干扰线
|
|||
// for (int i = 0; i < 10; i++)
|
|||
// {
|
|||
// int x1 = random.Next(bitmap.Width);
|
|||
// int x2 = random.Next(bitmap.Width);
|
|||
// int y1 = random.Next(bitmap.Height);
|
|||
// int y2 = random.Next(bitmap.Height);
|
|||
// g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
|
|||
// }
|
|||
|
|||
//画图片的前景干扰点
|
|||
for (int i = 0; i < 100; i++) |
|||
{ |
|||
int x = random.Next(bitmap.Width); |
|||
int y = random.Next(bitmap.Height); |
|||
bitmap.SetPixel(x, y, Color.FromArgb(random.Next())); |
|||
} |
|||
// //画图片的前景干扰点
|
|||
// for (int i = 0; i < 100; i++)
|
|||
// {
|
|||
// int x = random.Next(bitmap.Width);
|
|||
// int y = random.Next(bitmap.Height);
|
|||
// bitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
|
|||
// }
|
|||
|
|||
g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线
|
|||
g.Dispose(); |
|||
// g.DrawRectangle(new Pen(Color.Silver), 0, 0, bitmap.Width - 1, bitmap.Height - 1); //画图片的边框线
|
|||
// g.Dispose();
|
|||
|
|||
//保存图片数据
|
|||
MemoryStream stream = new MemoryStream(); |
|||
bitmap.Save(stream, ImageFormat.Jpeg); |
|||
byte[] imageByte = stream.ToArray(); |
|||
// //保存图片数据
|
|||
// MemoryStream stream = new MemoryStream();
|
|||
// bitmap.Save(stream, ImageFormat.Jpeg);
|
|||
// byte[] imageByte = stream.ToArray();
|
|||
|
|||
//输出图片流
|
|||
return imageByte; |
|||
// //输出图片流
|
|||
// return imageByte;
|
|||
|
|||
} |
|||
#endregion
|
|||
} |
|||
// }
|
|||
// #endregion
|
|||
//}
|
|||
|
|||
public enum ValidationCodeType { NumberValidationCode, AbcValidationCode, MixValidationCode }; |
|||
//public enum ValidationCodeType { NumberValidationCode, AbcValidationCode, MixValidationCode };
|
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue