C# 加文字水印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; public class WaterMark { /// <summary> /// 生成4位字母和数字的验证码图片 /// </summary> /// <returns>验证码的字符串</returns> //public static string GenerateImage(string username) //{ // return CreateImage(username); //} /// <summary> /// 创建验证码图片,并将其写入内存流中 /// </summary> /// <param name="waterText"></param> public static string CreateImage( string waterText) { // 已经存在直接返回 if (File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + "\\watermarks\\" + waterText + ".png" )) { return "/watermarks/" + waterText + ".png" ; } //验证码未设置时,不生成图片 if (waterText == null || waterText.Trim() == String.Empty) { return null ; } Bitmap img = new Bitmap(640, 640); Graphics g = Graphics.FromImage(img); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.Transparent); g.InterpolationMode = InterpolationMode.High; //設定高品質插值法 g.SmoothingMode = SmoothingMode.HighQuality; //設定高品質,低速度呈現平滑程度 g.CompositingQuality = CompositingQuality.HighQuality; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //画图片的背景噪音线 //for (int iCount = 0; iCount < 25; iCount++) //{ // int x1 = random.Next(img.Width); // int x2 = random.Next(img.Width); // int y1 = random.Next(img.Height); // int y2 = random.Next(img.Height); // g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); //} Font font = new Font( "Arial" , 24, (System.Drawing.FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.DarkRed, 1.2f, true ); g.RotateTransform(-30); float xpos = 0; float ypos = 0; // 获取图片的宽和高 int width = img.Width; int height = img.Height; // 循环打印文字水印 for ( int i = -width / 2; i < width * 1.5; i += 180) { xpos = i; for ( int j = -height / 2; j < height * 1.5; j += 80) { ypos = j; g.DrawString(waterText, font, new SolidBrush(Color.FromArgb(50, 210, 210, 210)), xpos, ypos); //g.DrawString(CheckCode, font, brush, 2, 2); } } //画图片的前景噪音点 //for (int i = 0; i < 100; i++) //{ // int x = random.Next(img.Width); // int y = random.Next(img.Height); // img.SetPixel(x, y, Color.FromArgb(random.Next())); //} //画图片的边框线 //g.DrawRectangle(new Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); img.Save(System.AppDomain.CurrentDomain.BaseDirectory + "\\watermarks\\" + waterText + ".png" , System.Drawing.Imaging.ImageFormat.Png); return "/watermarks/" + waterText + ".png" ; //Response.ClearContent(); //Response.ContentType = "image/Gif"; //Response.BinaryWrite(ms.ToArray()); //return img; } catch (Exception exp) { throw exp; } finally { g.Dispose(); img.Dispose(); } } } |
走向地狱的途中,不小心走了程序员这条路,路上一个个黑心的老板,和暗无天日的加班,我才发现,通往地狱的路径中,我们这行是最短的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义