Let the storm money come!

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();
            }
        }
    }

  

posted @   精密~顽石  阅读(408)  评论(0编辑  收藏  举报
编辑推荐:
· .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 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
在通往地狱的路上,加班能使你更快到达。
点击右上角即可分享
微信分享提示