forked from ciker/xcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
254 lines (251 loc) · 10.2 KB
/
Util.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//------------------------------------------------------------------------------
// 文件名称:System\strUtil.cs
// 运 行 库:2.0.50727.1882
// 最后修改:2012年9月8日 22:15:20
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web.Configuration;
using System.Web.Security;
using System.Text;
using System.Management;
namespace System {
/// <summary>
/// 其它工具栏
/// </summary>
public partial class Util
{
/// <summary>
/// 获取机器码
/// </summary>
/// <returns>当前主机的机器码</returns>
public static String GetComputerId()
{
String DiskDriveid = String.Empty;
String CPUid = String.Empty;
ManagementClass mcdisk = new ManagementClass(strUtil.GetStringByUnicode(@"\u0057\u0069\u006e\u0033\u0032\u005f\u0044\u0069\u0073\u006b\u0044\u0072\u0069\u0076\u0065"));
foreach (ManagementObject mo in mcdisk.GetInstances())
{
DiskDriveid = (string)mo.Properties["Model"].Value;
break;
}
ManagementClass mccpu = new ManagementClass(strUtil.GetStringByUnicode(@"\u0057\u0069\u006e\u0033\u0032\u005f\u0050\u0072\u006f\u0063\u0065\u0073\u0073\u006f\u0072"));
foreach (ManagementObject mo in mccpu.GetInstances())
{
CPUid = (string)mo.Properties["ProcessorId"].Value;
break;
}
Int64 diskid = DiskDriveid.GetHashCode();
Int64 cpuid = CPUid.GetHashCode();
Int64 computerid = ((diskid > 0 ? diskid : -diskid) % 10000000) * ((cpuid > 0 ? cpuid : -cpuid) % 10000000);
if (computerid > 100000000000)
computerid = computerid % 1000000000000;
else if (computerid < 100000000000)
computerid += 100000000000;
return computerid.ToString();
}
private static RegisterInfo _RegisterInfo = new RegisterInfo();
public static RegisterInfo GetRegister()
{
if (!_RegisterInfo.Success)
{
string filename = PathHelper.Map(cfgHelper.WebRoot + "Wlniao.dll");
if (!file.Exists(filename))
{
string content = "Sorry,您的程序需要注册!\t\t";
content += "\n\n机器码:" + GetComputerId() + "\t\t";
content += "\n\n请联系开发商进行注册\t\t";
//content += "\n电话:13075463371\t\t";
//content += "\n网址:http://www.wlniao.com\t\t";
System.IO.StreamWriter sw = new System.IO.StreamWriter(PathHelper.Map(cfgHelper.WebRoot + "Wlniao.dll"));
sw.Write(content);
sw.Flush();
sw.Close();
}
else
{
string content = "";
try
{
System.IO.StreamReader sr = new System.IO.StreamReader(filename);
//content = sr.ReadToEnd();
//content = "";
for (int i = 0; i < 9; i++)
{
if (i >= 3 && i < 7)
{
content += sr.ReadLine();
}
else
{
sr.ReadLine();
}
}
int length = Convert.ToInt32(content.Substring(0, 4));
content = content.Substring(4, length);
string softcode = Data.KvTableUtil.GetString("SoftCode");
if (string.IsNullOrEmpty(softcode))
{
softcode = "";
}
content = Encryptor.DesDecrypt(content, GetComputerId() + softcode);
_RegisterInfo = Json.ToObject<RegisterInfo>(content);
}
catch { }
}
}
return _RegisterInfo;
}
public static void Register(string computerid,bool rightone,bool righttwo,string config,DateTime endtime,string filename,string softcode){
RegisterInfo ri=new RegisterInfo();
ri.RightOne=rightone;
ri.RightTwo=righttwo;
ri.Config=config;
ri.EndTiem=endtime;
string content=Json.ToStringEx (ri);
if (string.IsNullOrEmpty(softcode))
{
softcode = Data.KvTableUtil.GetString("SoftCode");
}
if (string.IsNullOrEmpty(softcode))
{
softcode = "";
}
content = Encryptor.DesEncrypt(content, computerid + softcode);
content=content.Length.ToString ().PadLeft (4,'0')+content+Rand.Str_char(1020,true).ToUpper();
content=content.Substring (0,1024);
if(file.Exists(filename)){
file.Delete (filename);
}
System.IO.StreamWriter sw=new System.IO.StreamWriter(filename);
sw.WriteLine (Rand.Str_char(256,true).ToUpper());
sw.WriteLine (Rand.Str_char(256,true).ToUpper());
sw.WriteLine (Rand.Str_char(256,true).ToUpper());
sw.WriteLine (content.Substring(0,256));
sw.WriteLine (content.Substring(256,256));
sw.WriteLine (content.Substring(512,256));
sw.WriteLine (content.Substring(768,256));
sw.WriteLine (Rand.Str_char(256,true).ToUpper());
sw.WriteLine (Rand.Str_char(256,true).ToUpper());
sw.Flush();
sw.Close();
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式 HW:H:W:Cut</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(thumbnailPath)))
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(thumbnailPath));
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
public class RegisterInfo
{
public bool Success
{
get{return DateTools.GetNow()<_EndTiem;}
}
private DateTime _EndTiem;
public DateTime EndTiem
{
get { return _EndTiem; }
internal set { _EndTiem = value; }
}
private bool _RightOne;
public bool RightOne
{
get{return _RightOne;}
internal set { _RightOne = value; }
}
private bool _RightTwo;
public bool RightTwo
{
get{return _RightTwo;}
internal set { _RightTwo = value; }
}
private string _Config;
public string Config
{
get{return _Config;}
internal set { _Config = value; }
}
public RegisterInfo(){
_EndTiem=DateTime.MinValue;
_RightOne=false;
_RightTwo=false;
_Config="";
}
}
}