tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
 
 
namespace TProduct.PumpGraph.Picture
{
    public partial class LxpSeriesChart  
    {
        #region 是否黑白显示
        protected bool _isMonoColor = false;
        public bool IsMonoColor
        {
            get { return _isMonoColor; }
            set
            {
                _isMonoColor = value;
 
            }
        }
        #endregion
 
 
 
 
        #region 自定义背景色(默认白色)
        protected Color _backgroundColor = Color.White;
        public Color BackgroundColor
        {
            get { return _backgroundColor; }
            set { _backgroundColor = value; }
        }
 
        #endregion
 
 
 
        #region 绘制水印
        string _waterMarkText = null;
        public void SetWaterMarkText(string text_content)
        {
            _waterMarkText = text_content;
        }
 
        string _waterMarkLogoPath = null;
        public void SetWaterMarkLogoFile(string file_path)
        {
            _waterMarkLogoPath = file_path;
        }
 
        protected Color _colorWaterMark = Color.FromArgb(50, Color.Blue);
 
        public Action<Graphics,int , int > PrintWaterMarkCb = null;
 
        private bool isPrintWaterMark = true ;//是否有水印
        public bool IsPrintWaterMark
        {
            get { return isPrintWaterMark; }
            set { isPrintWaterMark = value; }
        }
 
 
        public void DrawWaterMark(Graphics g)
        {
            if (!isPrintWaterMark)
                return;
            //三种方式绘制水印
            DrawWaterMarkText(g);
 
            DrawWaterMarkLogo(g);
 
            if (PrintWaterMarkCb != null)
            {
                PrintWaterMarkCb(g,this._imageWidth,this._imageHeight);
            }
        }
        protected void DrawWaterMarkText(Graphics g)
        {
            if (string.IsNullOrEmpty(_waterMarkText))
                return;
 
            GraphicsState gs = g.Save();
 
            using (Font fontText = new Font("Arial", 30, FontStyle.Bold))
            using (SolidBrush drawBrush = new SolidBrush(_colorWaterMark))
            {
                g.ResetTransform();
                g.TranslateTransform(_imageWidth / 4, _imageHeight / 4 - 20); //设置旋转中心为文字中心
                g.RotateTransform(25);
                g.DrawString(_waterMarkText, fontText, drawBrush, 0, 0);
 
                g.ResetTransform();
                g.TranslateTransform(_imageWidth * 3 / 4 - 20, _imageHeight * 3 / 4 - 20); //设置旋转中心为文字中心
                g.RotateTransform(25);
                g.DrawString(_waterMarkText, fontText, drawBrush, 0, 0);
            }
 
            g.Restore(gs);
        }
 
        protected void DrawWaterMarkLogo(Graphics g)
        {
            if (string.IsNullOrEmpty(_waterMarkLogoPath))
                return;
 
            if (!System.IO.File.Exists(_waterMarkLogoPath))
            {
                return;
            }
 
            var imgWaterMark = System.Drawing.Image.FromFile(_waterMarkLogoPath);
            //插入水印宽度和高度
            int water_mark_image_width = imgWaterMark.Width;
            int water_mark_image_height = imgWaterMark.Height;
 
            //水印数量
            int water_image_number_x = (int)(this._chartDiagramSize.Width / (water_mark_image_width * 2.5));
            if (water_image_number_x < 0)
                water_image_number_x = 1;
            if (water_image_number_x > 3)
                water_image_number_x = 3;
            if (water_image_number_x <= 0)
                water_image_number_x = 1;
 
            int water_image_number_y = (int)(this._chartDiagramSize.Height / (water_mark_image_height * 4.0));
            if (water_image_number_y < 0)
                water_image_number_y = 1;
            if (water_image_number_y > 3)
                water_image_number_y = 3;
            if (water_image_number_y <= 0)
                water_image_number_y = 1;
 
            //添加水印
            for (int i = 0; i < water_image_number_x; i++)
            {
                for (int j = 0; j < water_image_number_y; j++)
                {
                    RectangleF rt1 = new RectangleF(
                        this._chartDiagramSize.Width * (i + 1) / (water_image_number_x + 1) - water_mark_image_width / 2,
                        this._chartDiagramSize.Height * (j + 1) / (water_image_number_y + 1) - water_mark_image_height / 2,
                        water_mark_image_width, water_mark_image_height);
 
                    g.DrawImage(imgWaterMark, rt1);
                }
            }
 
 
 
        }
 
        #endregion
 
    }
}