From b3d4961720c867b8913617546d48ab1dbfe19d29 Mon Sep 17 00:00:00 2001 From: wxd <123@qq.com> Date: Thu, 23 Apr 2026 12:04:11 +0800 Subject: [PATCH] =?UTF-8?q?pdf=E8=BD=AC=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PacsBusiness/PacsBusinessAppService.cs | 2 +- .../RegisterCheckPictureAppService.cs | 2 +- src/Shentun.Utilities/DocnetConverter.cs | 105 ++++ src/Shentun.Utilities/MagickConverter.cs | 89 ++++ src/Shentun.Utilities/PdfPigConverter.cs | 74 ++- .../Shentun.Utilities.csproj | 9 +- src/Shentun.Utilities/ValidationCodeHelper.cs | 464 +++++++++--------- 7 files changed, 500 insertions(+), 245 deletions(-) create mode 100644 src/Shentun.Utilities/DocnetConverter.cs create mode 100644 src/Shentun.Utilities/MagickConverter.cs diff --git a/src/Shentun.Peis.Application/PacsBusiness/PacsBusinessAppService.cs b/src/Shentun.Peis.Application/PacsBusiness/PacsBusinessAppService.cs index e50e8b09..7bd4aed0 100644 --- a/src/Shentun.Peis.Application/PacsBusiness/PacsBusinessAppService.cs +++ b/src/Shentun.Peis.Application/PacsBusiness/PacsBusinessAppService.cs @@ -587,7 +587,7 @@ namespace Shentun.Peis.PacsBusiness if (prefix == "application/pdf") { //处理pdf文件 - base64Images = PdfPigConverter.ConvertPdfToImages(input.PictureBaseStr); + base64Images = MagickConverter.ConvertPdfToImages(input.PictureBaseStr); } else { diff --git a/src/Shentun.Peis.Application/RegisterCheckPictures/RegisterCheckPictureAppService.cs b/src/Shentun.Peis.Application/RegisterCheckPictures/RegisterCheckPictureAppService.cs index edad5188..1b44d9bf 100644 --- a/src/Shentun.Peis.Application/RegisterCheckPictures/RegisterCheckPictureAppService.cs +++ b/src/Shentun.Peis.Application/RegisterCheckPictures/RegisterCheckPictureAppService.cs @@ -206,7 +206,7 @@ namespace Shentun.Peis.RegisterCheckPictures { //处理pdf文件 // base64Images = PDFHelper.SplitPdfToBase64Images(item.PictureBaseStr); - base64Images = PdfPigConverter.ConvertPdfToImages(item.PictureBaseStr); + base64Images = MagickConverter.ConvertPdfToImages(item.PictureBaseStr); } else { diff --git a/src/Shentun.Utilities/DocnetConverter.cs b/src/Shentun.Utilities/DocnetConverter.cs new file mode 100644 index 00000000..d1207ce8 --- /dev/null +++ b/src/Shentun.Utilities/DocnetConverter.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 ConvertPdfToImages(string pdfBase64) + { + List base64Images = new List(); + + // 处理可能包含 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; + } + } +} diff --git a/src/Shentun.Utilities/MagickConverter.cs b/src/Shentun.Utilities/MagickConverter.cs new file mode 100644 index 00000000..01c27b41 --- /dev/null +++ b/src/Shentun.Utilities/MagickConverter.cs @@ -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 ConvertPdfToImages(string pdfBase64, int dpi = 200, MagickFormat imageFormat = MagickFormat.Jpeg, int jpegQuality = 85, int startPage = 1, int? pageCount = null) + { + var base64Images = new List(); + + // 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; + } + } +} diff --git a/src/Shentun.Utilities/PdfPigConverter.cs b/src/Shentun.Utilities/PdfPigConverter.cs index 34b59683..f1e340be 100644 --- a/src/Shentun.Utilities/PdfPigConverter.cs +++ b/src/Shentun.Utilities/PdfPigConverter.cs @@ -1,13 +1,15 @@  +using SkiaSharp; using System; using System.Collections.Generic; +using System.Drawing.Text; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using UglyToad.PdfPig; using UglyToad.PdfPig.Rendering.Skia; -using SkiaSharp; +using UglyToad.PdfPig.Writer; namespace Shentun.Utilities { @@ -18,27 +20,47 @@ namespace Shentun.Utilities { List base64Images = new List(); - // 处理可能包含 data:application/pdf;base64, 前缀的情况 + // 处理 Base64 前缀 if (pdfBase64.Contains("base64,")) { pdfBase64 = pdfBase64.Substring(pdfBase64.IndexOf(",") + 1); } - // 将Base64字符串转换为字节数组 byte[] pdfBytes = Convert.FromBase64String(pdfBase64); + // 关键:注册 TrueType 字体到 PdfPig 的字体解析器 + string fontPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fonts", "msyh.ttf"); + if (File.Exists(fontPath)) + { + byte[] fontBytes = File.ReadAllBytes(fontPath); + var builder = new PdfDocumentBuilder(); + // 使用 PdfDocumentBuilder 注册字体(影响全局字体解析) + builder.AddTrueTypeFont(fontBytes); + } + else + { + // 如果项目目录没有,尝试从 Windows 系统字体目录加载 + string systemFontPath = @"C:\Windows\Fonts\msyh.ttf"; + if (File.Exists(systemFontPath)) + { + byte[] fontBytes = File.ReadAllBytes(systemFontPath); + var builder = new PdfDocumentBuilder(); + builder.AddTrueTypeFont(fontBytes); + } + } + using (var stream = new MemoryStream(pdfBytes)) using (var document = PdfDocument.Open(stream)) { - // 关键步骤:注册 Skia 页面工厂,这样才能渲染图片 + // 注册 Skia 页面工厂 document.AddSkiaPageFactory(); - // 注意:页码从 1 开始 for (int i = 1; i <= document.NumberOfPages; i++) { - // 通过 document 获取页面图片,而不是通过 page 对象 - // 参数:页码,缩放倍数(2.0 约等于 144 DPI,背景色参数可忽略) - using (var imageStream = document.GetPageAsPng(i, 4.17f)) // 4.17f ≈ 600 DPI (72 * 4.17 ≈ 300) + // 缩放倍数:300 DPI / 72 DPI ≈ 4.1667 + float scale = 300f / 72f; + + using (var imageStream = document.GetPageAsPng(i, scale)) { byte[] imageBytes = imageStream.ToArray(); string base64 = Convert.ToBase64String(imageBytes); @@ -50,6 +72,42 @@ namespace Shentun.Utilities return base64Images; } + //public static List ConvertPdfToImages(string pdfBase64) + //{ + // List base64Images = new List(); + + // // 处理可能包含 data:application/pdf;base64, 前缀的情况 + // if (pdfBase64.Contains("base64,")) + // { + // pdfBase64 = pdfBase64.Substring(pdfBase64.IndexOf(",") + 1); + // } + + // // 将Base64字符串转换为字节数组 + // byte[] pdfBytes = Convert.FromBase64String(pdfBase64); + + // using (var stream = new MemoryStream(pdfBytes)) + // using (var document = PdfDocument.Open(stream)) + // { + // // 关键步骤:注册 Skia 页面工厂,这样才能渲染图片 + // document.AddSkiaPageFactory(); + + // // 注意:页码从 1 开始 + // for (int i = 1; i <= document.NumberOfPages; i++) + // { + // // 通过 document 获取页面图片,而不是通过 page 对象 + // // 参数:页码,缩放倍数(2.0 约等于 144 DPI,背景色参数可忽略) + // using (var imageStream = document.GetPageAsPng(i, 4.17f)) // 4.17f ≈ 600 DPI (72 * 4.17 ≈ 300) + // { + // byte[] imageBytes = imageStream.ToArray(); + // string base64 = Convert.ToBase64String(imageBytes); + // base64Images.Add(base64); + // } + // } + // } + + // return base64Images; + //} + //private static string ImageToBase64(Image image, ImageFormat format) //{ // using (MemoryStream ms = new MemoryStream()) diff --git a/src/Shentun.Utilities/Shentun.Utilities.csproj b/src/Shentun.Utilities/Shentun.Utilities.csproj index 72f9aa43..4ccf380f 100644 --- a/src/Shentun.Utilities/Shentun.Utilities.csproj +++ b/src/Shentun.Utilities/Shentun.Utilities.csproj @@ -8,8 +8,11 @@ + + + @@ -23,15 +26,15 @@ - + - + - + diff --git a/src/Shentun.Utilities/ValidationCodeHelper.cs b/src/Shentun.Utilities/ValidationCodeHelper.cs index c2ff14fd..13327070 100644 --- a/src/Shentun.Utilities/ValidationCodeHelper.cs +++ b/src/Shentun.Utilities/ValidationCodeHelper.cs @@ -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 生产验证码 - /// - /// 1.数字验证码 - /// - /// - /// - 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; - } + // /// + // /// 1.数字验证码 + // /// + // /// + // /// + // 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; + // } - /// - /// 2.字母验证码 - /// - /// 字符长度 - /// 验证码字符 - 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); - } + // /// + // /// 2.字母验证码 + // /// + // /// 字符长度 + // /// 验证码字符 + // 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); + // } - /// - /// 3.混合验证码 - /// - /// 字符长度 - /// 验证码字符 - 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); - } + // /// + // /// 3.混合验证码 + // /// + // /// 字符长度 + // /// 验证码字符 + // 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); + // } - /// - /// 产生验证码(随机产生4-6位) - /// - /// 验证码类型:数字,字符,符合 - /// - 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 + // /// + // /// 产生验证码(随机产生4-6位) + // /// + // /// 验证码类型:数字,字符,符合 + // /// + // 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 验证码图片 - /// - /// 验证码图片 => Bitmap - /// - /// 验证码 - /// 宽 - /// 高 - /// Bitmap - 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 验证码图片 + // /// + // /// 验证码图片 => Bitmap + // /// + // /// 验证码 + // /// 宽 + // /// 高 + // /// Bitmap + // 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; + // } - /// - /// 验证码图片 => byte[] - /// - /// 验证码 - /// 宽 - /// 高 - /// byte[] - 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; - } + // /// + // /// 验证码图片 => byte[] + // /// + // /// 验证码 + // /// 宽 + // /// 高 + // /// byte[] + // 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 }; } \ No newline at end of file