常有函数
常有函数..........
1
using System;
2
using System.Drawing;
3
using System.Web;
4
using System.Web.UI;
5
using System.Web.UI.HtmlControls;
6
using System.IO;
7
using System.Threading;
8
9
namespace CommonClass
10

{
11
/**//// <summary>
12
/// Common 的摘要说明。
13
/// </summary>
14
public class Common
15
{
16
public Common()
17
{
18
//
19
// TODO: 在此处添加构造函数逻辑
20
//
21
}
22
23
24
(1) 转换大小 ChangeImage(string 原文件路径,string ,int,int,int)#region (1) 转换大小 ChangeImage(string 原文件路径,string ,int,int,int)
25
public static bool ChangeImage( string strSourceFile, string strSaveToFile, int nSaveToWidth, int nSaveToHeight, int nMode )
26
{
27
int nValue;
28
bool bFlag;
29
30
try
31
{
32
System.Drawing.Image SourceImage = System.Drawing.Image.FromFile( strSourceFile );
33
34
35
if ( nMode == 1)
{}
36
if ( nMode == 2 )
37
{
38
if ( SourceImage.Width < nSaveToWidth )
39
{
40
nSaveToWidth = SourceImage.Width;
41
nSaveToHeight = SourceImage.Height;
42
}
43
else
44
{
45
nSaveToHeight = ( int )SourceImage.Height * nSaveToWidth / SourceImage.Width;
46
}
47
}
48
if ( nMode == 3 )
49
{
50
if ( SourceImage.Height < nSaveToHeight )
51
{
52
nSaveToWidth = SourceImage.Width;
53
nSaveToHeight = SourceImage.Height;
54
}
55
else
56
{
57
nSaveToWidth = ( int )SourceImage.Width * nSaveToHeight / SourceImage.Height;
58
}
59
}
60
if ( nMode == 4 )
61
{
62
nValue = ( int )SourceImage.Height * nSaveToWidth / SourceImage.Width;
63
if ( nValue > nSaveToHeight )
64
{
65
nSaveToWidth = ( int )SourceImage.Width * nSaveToHeight / SourceImage.Height;
66
}
67
else
68
{
69
nSaveToHeight = nValue;
70
}
71
}
72
System.Drawing.Image SaveToImage = SourceImage.GetThumbnailImage( nSaveToWidth, nSaveToHeight, null, new System.IntPtr() );
73
SaveToImage.Save( strSaveToFile, GetImageType( strSaveToFile ) );
74
75
bFlag = true;
76
SourceImage.Dispose();
77
}
78
catch( System.Exception e )
79
{
80
bFlag = false;
81
e.ToString();
82
}
83
84
return bFlag;
85
}
86
#endregion
87
88
(1) 转换大小string[] ZipImage(string 原文件路径,string ,int,int,int)#region (1) 转换大小string[] ZipImage(string 原文件路径,string ,int,int,int)
89
public static string[] ZipImage(HtmlInputFile myFile,string strSourcePath, string strSaveToPath,int nSaveToWidth, int nSaveToHeight, bool tag)
90
{
91
string ImageName,FileName;
92
string[] imgname=new string[2];
93
int ImageHeight,ImageWidth;
94
95
FileName=strSourcePath.Substring(strSourcePath.LastIndexOf("\\")+1);
96
//形成新的图片名
97
ImageName=GetDate(DateTime.Now,5);
98
imgname[0]=ChangeImgName(FileName,ImageName);
99
imgname[1]=ChangeImgName(FileName,ImageName+"_1");
100
//获取图片高度和宽读
101
ImageHeight=GetImageHeight(myFile.PostedFile.InputStream);
102
ImageWidth=GetImageWidth(myFile.PostedFile.InputStream);
103
//上传图片到服务器
104
UploadImage(myFile,strSaveToPath,imgname[0]);
105
106
if( tag )
107
{
108
double cut;
109
if( ImageHeight < nSaveToHeight && ImageWidth < nSaveToWidth )
110
UploadImage(myFile,strSaveToPath,imgname[1]);
111
else
112
{
113
if( ImageHeight > ImageWidth )
114
{
115
cut=Convert.ToDouble(ImageHeight)/nSaveToWidth;
116
ChangeImage(strSaveToPath+imgname[0],strSaveToPath+imgname[1],Convert.ToInt32(Convert.ToDouble(ImageWidth)/cut),nSaveToHeight,1);
117
}
118
else
119
{
120
cut=Convert.ToDouble(ImageWidth)/nSaveToHeight;
121
ChangeImage(strSaveToPath+imgname[0],strSaveToPath+imgname[1],nSaveToWidth,Convert.ToInt32(Convert.ToDouble(ImageHeight)/cut),1);
122
}
123
}
124
}
125
126
return imgname;
127
}
128
#endregion
129
130
(2) 根据文件名字符串的扩展名,得到一种图片文件的格式#region (2) 根据文件名字符串的扩展名,得到一种图片文件的格式
131
public static System.Drawing.Imaging.ImageFormat GetImageType( string strFileName )
132
{
133
int i = strFileName.LastIndexOf( "." );
134
string strExt = strFileName.Substring( i + 1 );
135
System.Drawing.Imaging.ImageFormat reImageType = null;
136
switch( strExt )
137
{
138
case "jpg":
139
case "jpeg":
140
reImageType = System.Drawing.Imaging.ImageFormat.Jpeg;
141
break;
142
case "gif":
143
reImageType = System.Drawing.Imaging.ImageFormat.Gif;
144
break;
145
case "bmp":
146
reImageType = System.Drawing.Imaging.ImageFormat.Bmp;
147
break;
148
case "tiff":
149
reImageType = System.Drawing.Imaging.ImageFormat.Tiff;
150
break;
151
}
152
return reImageType;
153
}
154
#endregion
155
156
(3) 完成查找关键字字体高亮显示 string serkeys(string , string)#region (3) 完成查找关键字字体高亮显示 string serkeys(string , string)
157
public static string serkeys(string str,string sqlkey)
158
{
159
160
//sqlstr 待查找的关键字
161
//str 已经查找到带有关键字的字符串
162
163
int len=sqlkey.Length;
164
int len1=str.Length;
165
166
string onesqlstr,pproname,rproname="";
167
for(int i=0;i<=len-1;i++)
168
{
169
onesqlstr=sqlkey.Substring(i,1);
170
for(int j=0;j<=len1-1;j++)
171
{
172
173
pproname=str.Substring(j,1);
174
175
if(onesqlstr==pproname)
176
{
177
pproname="<font color=#ff033><b>"+pproname+"</b></font>";
178
rproname+=pproname;
179
if(i<len-1)
180
onesqlstr=sqlkey.Substring(++i,1);
181
else
182
continue;
183
}
184
else
185
{
186
rproname+=pproname;
187
}
188
}
189
}
190
return rproname;
191
192
}
193
#endregion
194
195
(4) 改变原来图片的名称 ChangeImgName(string img,string NewName)#region (4) 改变原来图片的名称 ChangeImgName(string img,string NewName)
196
public static string ChangeImgName(string imgPath,string NewName)
197
{
198
//imgPath是图片路径
199
//string newName; 新文件名
200
string ExtendName; //文件扩展名
201
string FileName; //文件名
202
int i;
203
int j;
204
i= imgPath.LastIndexOf("\\") ;
205
FileName =imgPath.Substring(i+1);
206
j=FileName.LastIndexOf(".");
207
ExtendName=FileName.Substring(j);
208
NewName+=ExtendName.ToLower();
209
210
return NewName;
211
}
212
#endregion
213
214
(5) 返回日期样式GetDate() 获取日期样式 static string GetDate(object objDate, int Type)#region (5) 返回日期样式GetDate() 获取日期样式 static string GetDate(object objDate, int Type)
215
public static string GetDate( object t )
216
{
217
string GetDate;
218
DateTime dtDate;
219
dtDate=Convert.ToDateTime(t);
220
GetDate =dtDate.Year.ToString().Substring( 2 )+"-";
221
if ( dtDate.Month < 10 ) GetDate += "0";
222
GetDate += dtDate.Month+"-";
223
if ( dtDate.Day < 10 ) GetDate += "0";
224
GetDate += dtDate.Day;
225
return GetDate;
226
}
227
228
public static string GetDate( object objDate, int Type )
229
{
230
try
231
{
232
DateTime dtDate;
233
string GetDate = "";
234
dtDate = Convert.ToDateTime( objDate );
235
236
switch( Type )
237
{
238
case 1: // 2003-1-1
239
GetDate = dtDate.Year + "-" + dtDate.Month + "-" +dtDate.Day;
240
break;
241
case 2: // 2003-1-1 9:9:9
242
GetDate = dtDate.Year + "-" + dtDate.Month + "-" + dtDate.Day + " " + dtDate.Hour + ":" + dtDate.Minute + ":" + dtDate.Second;
243
break;
244
case 3: // 2003年1月1日
245
GetDate = dtDate.Year + "年" + dtDate.Month + "月" +dtDate.Day + "日";
246
break;
247
case 4: // 2003年1月1日9点9分9秒
248
GetDate = dtDate.Year + "年" + dtDate.Month + "月" + dtDate.Day + "日 " + dtDate.Hour + "点" + dtDate.Minute + "分" + dtDate.Second + "秒";
249
break;
250
case 5: // 20030101090909
251
GetDate = "" + dtDate.Year;
252
if ( dtDate.Month < 10 ) GetDate += "0";
253
GetDate += dtDate.Month;
254
if ( dtDate.Day < 10 ) GetDate += "0";
255
GetDate += dtDate.Day;
256
if ( dtDate.Hour < 10 ) GetDate += "0";
257
GetDate += dtDate.Hour;
258
if ( dtDate.Minute < 10 ) GetDate += "0";
259
GetDate += dtDate.Minute;
260
if ( dtDate.Second < 10 ) GetDate += "0";
261
GetDate += dtDate.Second;
262
break;
263
case 6: // 2003.1.1
264
GetDate = dtDate.ToShortDateString();
265
break;
266
case 7: // 03-1-1
267
GetDate = "" + dtDate.Year.ToString().Substring( 2 ) + "-" + dtDate.Month + "-" + dtDate.Day;
268
break;
269
case 8: //20030102
270
dtDate = Convert.ToDateTime( dtDate.ToShortDateString() );
271
GetDate = "" + dtDate.Year;
272
if ( dtDate.Month < 10 ) GetDate += "0";
273
GetDate += dtDate.Month;
274
if ( dtDate.Day < 10 ) GetDate += "0";
275
GetDate += dtDate.Day;
276
break;
277
default: // 2003.1.1 9:9:9
278
GetDate = dtDate.ToString();
279
break;
280
}
281
282
return GetDate;
283
}
284
catch
285
{
286
return "";
287
}
288
}
289
#endregion
290
291
(6) 判断输入是否是数字bool IsNum(object str)#region (6) 判断输入是否是数字bool IsNum(object str)
292
public static bool IsNum(object str)
293
{
294
295
foreach ( char sbit in str.ToString() )
296
{
297
if ( sbit < '0' || sbit > '9') return false;
298
}
299
return true;
300
}
301
#endregion
302
303
(8) 改变字符串长度string ChangeLen(string str,int len)#region (8) 改变字符串长度string ChangeLen(string str,int len)
304
public static string ChangeLen(string str,int len)
305
{
306
if(str.Length>len)
307
return str.Substring(0,len)+"..";
308
else
309
return str;
310
}
311
#endregion
312
313
(9) 图片上串 string UploadImage(HtmlInputFile myFile,string SavePath)#region (9) 图片上串 string UploadImage(HtmlInputFile myFile,string SavePath)
314
public static void UploadImage(HtmlInputFile myFile,string SavePath,string NewName)
315
{
316
myFile.PostedFile.SaveAs(SavePath+NewName);
317
}
318
#endregion
319
320
(10) 图片连接 string GetImg(string img,int height,int width,string memo)#region (10) 图片连接 string GetImg(string img,int height,int width,string memo)
321
public static string GetImg(string img,int height,int width,string memo)
322
{
323
return "<a href='" + img + "' target='_blank'><IMG alt='"+memo+"' src='" + img + "' width='"+width+"' height='"+height+"' border='0'></a>";
324
}
325
#endregion
326
327
(11) 判断图片格式和大小IsImg(HtmlInputFile myFile)#region (11) 判断图片格式和大小IsImg(HtmlInputFile myFile)
328
public static string IsImg( HtmlInputFile myFile , int size)
329
{
330
string temp="",FileName=myFile.PostedFile.FileName.ToLower();
331
if ( ( FileName.IndexOf( ".jpg" ) < 0 ) && ( FileName.IndexOf( ".gif" ) < 0 ) )
332
temp+= "上传文件的格式不正确!\\n";
333
if(myFile.PostedFile.ContentLength > size)
334
temp+= "上传文件过大!";
335
return temp;
336
}
337
#endregion
338
339
(12)压缩图片 RarImage(HtmlInputFile upImage, string imgName, string savePath,int nWidth, int nHeight, string pre) pre小图片前缀#region (12)压缩图片 RarImage(HtmlInputFile upImage, string imgName, string savePath,int nWidth, int nHeight, string pre) pre小图片前缀
340
public static string RarImage(HtmlInputFile upImage, string imgName, string savePath,int nWidth, int nHeight, string pre)
341
{
342
string strReturn = ""; //返回值
343
System.Drawing.Image image=null, newimage=null; //定义image类的对象
344
string imgType,imgPath;
345
double rarImgWidth,rarImgHeight,dScale;
346
347
imgPath= upImage.PostedFile.FileName;
348
imgType = imgPath.Substring(imgPath.LastIndexOf("."));
349
350
351
if(pre =="")
352
pre = "sml_";
353
354
upImage.PostedFile.SaveAs(savePath+"\\"+imgName+imgType);
355
image=System.Drawing.Image.FromFile(savePath+"/"+imgName+imgType);
356
357
//确定压缩 宽 高
358
if(image.Width > image.Height)
359
{
360
if(image.Width > nWidth)
361
{
362
dScale = image.Width/nWidth;
363
rarImgWidth = nWidth;
364
}
365
else
366
{
367
dScale = 1;
368
rarImgWidth = image.Width;
369
}
370
371
rarImgHeight = image.Height/dScale;
372
}
373
else
374
{
375
if(image.Height > nHeight)
376
{
377
dScale = image.Height/nHeight;
378
rarImgHeight = nHeight;
379
}
380
else
381
{
382
dScale = 1;
383
rarImgHeight = image.Height;
384
}
385
386
rarImgWidth = image.Width/dScale;
387
}
388
389
newimage=image.GetThumbnailImage(Convert.ToInt32(rarImgWidth),Convert.ToInt32(rarImgHeight),null,new System.IntPtr()); //生成缩略图
390
FileStream myOutput = new FileStream(savePath+"/"+pre+imgName+imgType,FileMode.Create, FileAccess.Write, FileShare.Write);
391
newimage.Save(myOutput,GetImageType(imgType)); //把缩略图保存到指定的虚拟路径
392
393
myOutput.Close();
394
image.Dispose(); //释放image对象占用的资源
395
newimage.Dispose(); //释放newimage对象的资源
396
397
strReturn = imgName+imgType;
398
399
return strReturn;
400
}
401
402
public static string RarImage(HtmlInputFile upImage, string fileName, string savePath,int nWidth, int nHeight)
403
{
404
return RarImage(upImage, fileName, savePath, nWidth, nHeight, "");
405
}
406
#endregion
407
408
(13) HTML转换 static string UnHtml( object objResource)#region (13) HTML转换 static string UnHtml( object objResource)
409
public static string UnHtml( object objResource )
410
{
411
if ( objResource == null )
412
{
413
return "";
414
}
415
string strResource = objResource.ToString();
416
// strResource = strResource.Replace( "&", "&" );
417
// strResource = strResource.Replace( "<", "<" );
418
// strResource = strResource.Replace( ">", ">" );
419
strResource = strResource.Replace( "\\n", "<br>" );
420
strResource = strResource.Replace( Convert.ToString( Convert.ToChar( 34 ) ), """ );
421
strResource = strResource.Replace( Convert.ToString( Convert.ToChar( 13 ) ), "<br>" );
422
strResource = strResource.Replace( Convert.ToString( Convert.ToChar( 32 ) ), " " );
423
424
return strResource;
425
}
426
#endregion
427
428
(14) 遍历目录返回目录大小GetDirSize(string StrDir)#region (14) 遍历目录返回目录大小GetDirSize(string StrDir)
429
private long GetDirSize(string StrDir)
430
{
431
long LngDirSize=0;
432
DirectoryInfo TmpDir=new DirectoryInfo(StrDir);
433
434
foreach(FileInfo file in TmpDir.GetFiles())
435
{
436
LngDirSize+=file.Length;
437
}
438
439
foreach(DirectoryInfo SubDir in TmpDir.GetDirectories())
440
{
441
LngDirSize+=GetDirSize(SubDir.FullName);
442
}
443
444
return LngDirSize;
445
}
446
#endregion
447
448
(15) 返回图片高度GetImageHeight (System.IO.Stream strSourceFile)#region (15) 返回图片高度GetImageHeight (System.IO.Stream strSourceFile)
449
public static int GetImageHeight (System.IO.Stream strSourceFile)
450
{
451
System.Drawing.Image SourceImage = System.Drawing.Image.FromStream( strSourceFile );
452
int height=SourceImage.Height;
453
SourceImage.Dispose();
454
return height;
455
}
456
#endregion
457
458
(16) 返回图片宽度GetImageHeight (System.IO.Stream strSourceFile)#region (16) 返回图片宽度GetImageHeight (System.IO.Stream strSourceFile)
459
public static int GetImageWidth (System.IO.Stream strSourceFile)
460
{
461
System.Drawing.Image SourceImage = System.Drawing.Image.FromStream( strSourceFile );
462
int width=SourceImage.Width;
463
SourceImage.Dispose();
464
return width;
465
}
466
#endregion
467
468
//(17) 对文件目录压缩 ZipDirectory(string DirectoryPath,string ZipFile)#region //(17) 对文件目录压缩 ZipDirectory(string DirectoryPath,string ZipFile)
469
// public static void ZipDirectory(string DirectoryPath,string ZipFile)
470
// {
471
// ZipClass zip=new ZipClass();
472
// string []FileProperties=new string[2];
473
// FileProperties[0]=DirectoryPath;//待压缩文件目录
474
// FileProperties[1]=ZipFile; //压缩后的目标文件
475
// zip.ZipFileMain( FileProperties );
476
// }
477
// #endregion
478
//
479
// #region (18) 对文件目录解压 UnZipDirectory(string DirectoryPath,string ZipFile)
480
// public static void UnZipDirectory(string DirectoryPath,string ZipFile)
481
// {
482
// UnZipClass unzip=new UnZipClass();
483
// string []FileProperties=new string[2];
484
// FileProperties[0]=ZipFile;//待压解文件
485
// FileProperties[1]=DirectoryPath; //解压后放置的目标目录
486
// unzip.UnZip( FileProperties );
487
// }
488
#endregion
489
490
(19) 加密string GetMd5Psw32( string str )#region (19) 加密string GetMd5Psw32( string str )
491
public static string Md5Psw32( string str )
492
{
493
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"md5");
494
}
495
#endregion
496
497
(20) 检测SQL注入 void StrCheck(object s)#region (20) 检测SQL注入 void StrCheck(object s)
498
public static void StrCheck(object s)
499
{
500
string str= "'|;|and|(|)|--|exec|insert|select|delete|update|count|*|%|chr|mid|truncate|char|declare|drop|xp_cmdshell|exec master.dbo.xp_cmdshell|net user";
501
string ss="";
502
string[] str1=str.Split('|');
503
504
if( s!=null )
505
{
506
ss=s.ToString().ToLower();
507
StrCheck1(ss);
508
for( int i=0;i<str1.Length;i++ )
509
{
510
if(ss.IndexOf(str1[i])>0)
511
{
512
//insert(ss);
513
System.Web.HttpContext.Current.Response.Write("参数错误!");
514
System.Web.HttpContext.Current.Response.End();
515
}
516
}
517
}
518
else
519
{
520
System.Web.HttpContext.Current.Response.Write("参数错误!");
521
System.Web.HttpContext.Current.Response.End();
522
}
523
}
524
525
public static void StrCheck2(object s)
526
{
527
string str= "'|;|and|(|)|--|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|drop|xp_cmdshell|exec master.dbo.xp_cmdshell|net user";
528
string ss="";
529
string[] str1=str.Split('|');
530
531
if( s!=null )
532
{
533
ss=s.ToString().ToLower();
534
535
for( int i=0;i<str1.Length;i++ )
536
{
537
if(ss.IndexOf(str1[i])>0)
538
{
539
System.Web.HttpContext.Current.Response.Write("参数错误!");
540
System.Web.HttpContext.Current.Response.End();
541
}
542
}
543
}
544
else
545
{
546
System.Web.HttpContext.Current.Response.Write("参数错误!");
547
System.Web.HttpContext.Current.Response.End();
548
}
549
}
550
551
public static void StrCheck1(string s)
552
{
553
if( !IsNum(s) )
554
{
555
System.Web.HttpContext.Current.Response.Write("参数错误!");
556
System.Web.HttpContext.Current.Response.End();
557
}
558
}
559
#endregion
560
561
(21) 输出硬盘文件,提供下载ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)#region (21) 输出硬盘文件,提供下载ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
562
/**//// <summary>
563
/// 输出硬盘文件,提供下载
564
/// </summary>
565
/// <param name="_Request">Page.Request对象</param>
566
/// <param name="_Response">Page.Response对象</param>
567
/// <param name="_fileName">下载文件名</param>
568
/// <param name="_fullPath">带文件名下载路径</param>
569
/// <param name="_speed">_speed</param>
570
/// <returns>返回是否成功</returns>
571
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
572
{
573
try
574
{
575
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
576
BinaryReader br = new BinaryReader(myFile);
577
try
578
{
579
_Response.AddHeader("Accept-Ranges", "bytes");
580
_Response.Buffer = false;
581
long fileLength = myFile.Length;
582
long startBytes = 0;
583
584
int pack = 1024; //10K bytes
585
//int sleep = 200; //每秒5次 即5*10K bytes每秒
586
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
587
if (_Request.Headers["Range"] != null)
588
{
589
_Response.StatusCode = 206;
590
string[] range = _Request.Headers["Range"].Split(new char[]
{'=', '-'});
591
startBytes = Convert.ToInt64(range[1]);
592
}
593
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
594
if (startBytes != 0)
595
{
596
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
597
}
598
_Response.AddHeader("Connection", "Keep-Alive");
599
_Response.ContentType = "application/octet-stream";
600
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );
601
602
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
603
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
604
605
for (int i = 0; i < maxCount; i++)
606
{
607
if (_Response.IsClientConnected)
608
{
609
_Response.BinaryWrite(br.ReadBytes(pack));
610
Thread.Sleep(sleep);
611
}
612
else
613
{
614
i=maxCount;
615
}
616
}
617
}
618
catch
619
{
620
return false;
621
}
622
finally
623
{
624
br.Close();
625
myFile.Close();
626
}
627
}
628
catch
629
{
630
return false;
631
}
632
return true;
633
}
634
#endregion
635
636
}
637
}
638
using System;2
using System.Drawing;3
using System.Web;4
using System.Web.UI;5
using System.Web.UI.HtmlControls;6
using System.IO;7
using System.Threading;8

9
namespace CommonClass10


{11

/**//// <summary>12
/// Common 的摘要说明。13
/// </summary>14
public class Common15

{16
public Common()17

{18
//19
// TODO: 在此处添加构造函数逻辑20
//21
}22

23

24

(1) 转换大小 ChangeImage(string 原文件路径,string ,int,int,int)#region (1) 转换大小 ChangeImage(string 原文件路径,string ,int,int,int)25
public static bool ChangeImage( string strSourceFile, string strSaveToFile, int nSaveToWidth, int nSaveToHeight, int nMode )26

{27
int nValue;28
bool bFlag;29
30
try31

{32
System.Drawing.Image SourceImage = System.Drawing.Image.FromFile( strSourceFile );33
34
35

if ( nMode == 1)
{}36
if ( nMode == 2 )37

{38
if ( SourceImage.Width < nSaveToWidth )39

{40
nSaveToWidth = SourceImage.Width;41
nSaveToHeight = SourceImage.Height;42
}43
else44

{45
nSaveToHeight = ( int )SourceImage.Height * nSaveToWidth / SourceImage.Width;46
}47
}48
if ( nMode == 3 )49

{50
if ( SourceImage.Height < nSaveToHeight )51

{52
nSaveToWidth = SourceImage.Width;53
nSaveToHeight = SourceImage.Height;54
}55
else56

{57
nSaveToWidth = ( int )SourceImage.Width * nSaveToHeight / SourceImage.Height;58
}59
}60
if ( nMode == 4 )61

{62
nValue = ( int )SourceImage.Height * nSaveToWidth / SourceImage.Width;63
if ( nValue > nSaveToHeight )64

{65
nSaveToWidth = ( int )SourceImage.Width * nSaveToHeight / SourceImage.Height;66
}67
else68

{69
nSaveToHeight = nValue;70
}71
}72
System.Drawing.Image SaveToImage = SourceImage.GetThumbnailImage( nSaveToWidth, nSaveToHeight, null, new System.IntPtr() );73
SaveToImage.Save( strSaveToFile, GetImageType( strSaveToFile ) );74

75
bFlag = true;76
SourceImage.Dispose();77
}78
catch( System.Exception e )79

{80
bFlag = false;81
e.ToString();82
}83

84
return bFlag;85
}86
#endregion87

88

(1) 转换大小string[] ZipImage(string 原文件路径,string ,int,int,int)#region (1) 转换大小string[] ZipImage(string 原文件路径,string ,int,int,int)89
public static string[] ZipImage(HtmlInputFile myFile,string strSourcePath, string strSaveToPath,int nSaveToWidth, int nSaveToHeight, bool tag)90

{91
string ImageName,FileName;92
string[] imgname=new string[2];93
int ImageHeight,ImageWidth;94
95
FileName=strSourcePath.Substring(strSourcePath.LastIndexOf("\\")+1);96
//形成新的图片名97
ImageName=GetDate(DateTime.Now,5);98
imgname[0]=ChangeImgName(FileName,ImageName);99
imgname[1]=ChangeImgName(FileName,ImageName+"_1");100
//获取图片高度和宽读101
ImageHeight=GetImageHeight(myFile.PostedFile.InputStream);102
ImageWidth=GetImageWidth(myFile.PostedFile.InputStream);103
//上传图片到服务器104
UploadImage(myFile,strSaveToPath,imgname[0]);105
106
if( tag )107

{108
double cut;109
if( ImageHeight < nSaveToHeight && ImageWidth < nSaveToWidth )110
UploadImage(myFile,strSaveToPath,imgname[1]);111
else112

{113
if( ImageHeight > ImageWidth )114

{115
cut=Convert.ToDouble(ImageHeight)/nSaveToWidth;116
ChangeImage(strSaveToPath+imgname[0],strSaveToPath+imgname[1],Convert.ToInt32(Convert.ToDouble(ImageWidth)/cut),nSaveToHeight,1); 117
}118
else119

{120
cut=Convert.ToDouble(ImageWidth)/nSaveToHeight;121
ChangeImage(strSaveToPath+imgname[0],strSaveToPath+imgname[1],nSaveToWidth,Convert.ToInt32(Convert.ToDouble(ImageHeight)/cut),1);122
}123
}124
}125
126
return imgname;127
}128
#endregion129
130

(2) 根据文件名字符串的扩展名,得到一种图片文件的格式#region (2) 根据文件名字符串的扩展名,得到一种图片文件的格式131
public static System.Drawing.Imaging.ImageFormat GetImageType( string strFileName )132

{133
int i = strFileName.LastIndexOf( "." );134
string strExt = strFileName.Substring( i + 1 );135
System.Drawing.Imaging.ImageFormat reImageType = null;136
switch( strExt )137

{138
case "jpg":139
case "jpeg":140
reImageType = System.Drawing.Imaging.ImageFormat.Jpeg;141
break;142
case "gif":143
reImageType = System.Drawing.Imaging.ImageFormat.Gif;144
break;145
case "bmp":146
reImageType = System.Drawing.Imaging.ImageFormat.Bmp;147
break;148
case "tiff":149
reImageType = System.Drawing.Imaging.ImageFormat.Tiff;150
break;151
}152
return reImageType;153
}154
#endregion155
156

(3) 完成查找关键字字体高亮显示 string serkeys(string , string)#region (3) 完成查找关键字字体高亮显示 string serkeys(string , string)157
public static string serkeys(string str,string sqlkey)158

{159
160
//sqlstr 待查找的关键字161
//str 已经查找到带有关键字的字符串162

163
int len=sqlkey.Length;164
int len1=str.Length;165
166
string onesqlstr,pproname,rproname="";167
for(int i=0;i<=len-1;i++)168

{169
onesqlstr=sqlkey.Substring(i,1);170
for(int j=0;j<=len1-1;j++)171

{172
173
pproname=str.Substring(j,1);174

175
if(onesqlstr==pproname)176

{177
pproname="<font color=#ff033><b>"+pproname+"</b></font>";178
rproname+=pproname;179
if(i<len-1)180
onesqlstr=sqlkey.Substring(++i,1);181
else 182
continue;183
}184
else185

{186
rproname+=pproname;187
}188
}189
}190
return rproname;191
192
}193
#endregion194

195

(4) 改变原来图片的名称 ChangeImgName(string img,string NewName)#region (4) 改变原来图片的名称 ChangeImgName(string img,string NewName)196
public static string ChangeImgName(string imgPath,string NewName)197

{198
//imgPath是图片路径199
//string newName; 新文件名 200
string ExtendName; //文件扩展名201
string FileName; //文件名202
int i;203
int j;204
i= imgPath.LastIndexOf("\\") ;205
FileName =imgPath.Substring(i+1);206
j=FileName.LastIndexOf(".");207
ExtendName=FileName.Substring(j);208
NewName+=ExtendName.ToLower();209

210
return NewName;211
}212
#endregion213

214

(5) 返回日期样式GetDate() 获取日期样式 static string GetDate(object objDate, int Type)#region (5) 返回日期样式GetDate() 获取日期样式 static string GetDate(object objDate, int Type)215
public static string GetDate( object t )216

{217
string GetDate;218
DateTime dtDate;219
dtDate=Convert.ToDateTime(t);220
GetDate =dtDate.Year.ToString().Substring( 2 )+"-";221
if ( dtDate.Month < 10 ) GetDate += "0";222
GetDate += dtDate.Month+"-";223
if ( dtDate.Day < 10 ) GetDate += "0";224
GetDate += dtDate.Day;225
return GetDate;226
}227
228
public static string GetDate( object objDate, int Type )229

{230
try231

{232
DateTime dtDate;233
string GetDate = "";234
dtDate = Convert.ToDateTime( objDate );235

236
switch( Type )237

{238
case 1: // 2003-1-1239
GetDate = dtDate.Year + "-" + dtDate.Month + "-" +dtDate.Day;240
break;241
case 2: // 2003-1-1 9:9:9242
GetDate = dtDate.Year + "-" + dtDate.Month + "-" + dtDate.Day + " " + dtDate.Hour + ":" + dtDate.Minute + ":" + dtDate.Second;243
break;244
case 3: // 2003年1月1日245
GetDate = dtDate.Year + "年" + dtDate.Month + "月" +dtDate.Day + "日";246
break;247
case 4: // 2003年1月1日9点9分9秒248
GetDate = dtDate.Year + "年" + dtDate.Month + "月" + dtDate.Day + "日 " + dtDate.Hour + "点" + dtDate.Minute + "分" + dtDate.Second + "秒";249
break;250
case 5: // 20030101090909251
GetDate = "" + dtDate.Year;252
if ( dtDate.Month < 10 ) GetDate += "0";253
GetDate += dtDate.Month;254
if ( dtDate.Day < 10 ) GetDate += "0";255
GetDate += dtDate.Day;256
if ( dtDate.Hour < 10 ) GetDate += "0";257
GetDate += dtDate.Hour;258
if ( dtDate.Minute < 10 ) GetDate += "0";259
GetDate += dtDate.Minute;260
if ( dtDate.Second < 10 ) GetDate += "0";261
GetDate += dtDate.Second;262
break;263
case 6: // 2003.1.1264
GetDate = dtDate.ToShortDateString();265
break;266
case 7: // 03-1-1267
GetDate = "" + dtDate.Year.ToString().Substring( 2 ) + "-" + dtDate.Month + "-" + dtDate.Day;268
break;269
case 8: //20030102270
dtDate = Convert.ToDateTime( dtDate.ToShortDateString() );271
GetDate = "" + dtDate.Year;272
if ( dtDate.Month < 10 ) GetDate += "0";273
GetDate += dtDate.Month;274
if ( dtDate.Day < 10 ) GetDate += "0";275
GetDate += dtDate.Day; 276
break;277
default: // 2003.1.1 9:9:9278
GetDate = dtDate.ToString();279
break;280
}281

282
return GetDate;283
}284
catch285

{286
return "";287
}288
}289
#endregion290

291

(6) 判断输入是否是数字bool IsNum(object str)#region (6) 判断输入是否是数字bool IsNum(object str)292
public static bool IsNum(object str)293

{294
295
foreach ( char sbit in str.ToString() )296

{297
if ( sbit < '0' || sbit > '9') return false;298
}299
return true;300
}301
#endregion302

303

(8) 改变字符串长度string ChangeLen(string str,int len)#region (8) 改变字符串长度string ChangeLen(string str,int len)304
public static string ChangeLen(string str,int len)305

{306
if(str.Length>len)307
return str.Substring(0,len)+"..";308
else309
return str;310
}311
#endregion312

313

(9) 图片上串 string UploadImage(HtmlInputFile myFile,string SavePath)#region (9) 图片上串 string UploadImage(HtmlInputFile myFile,string SavePath)314
public static void UploadImage(HtmlInputFile myFile,string SavePath,string NewName)315

{316
myFile.PostedFile.SaveAs(SavePath+NewName);317
}318
#endregion319

320

(10) 图片连接 string GetImg(string img,int height,int width,string memo)#region (10) 图片连接 string GetImg(string img,int height,int width,string memo)321
public static string GetImg(string img,int height,int width,string memo)322

{323
return "<a href='" + img + "' target='_blank'><IMG alt='"+memo+"' src='" + img + "' width='"+width+"' height='"+height+"' border='0'></a>";324
}325
#endregion326

327

(11) 判断图片格式和大小IsImg(HtmlInputFile myFile)#region (11) 判断图片格式和大小IsImg(HtmlInputFile myFile)328
public static string IsImg( HtmlInputFile myFile , int size)329

{330
string temp="",FileName=myFile.PostedFile.FileName.ToLower();331
if ( ( FileName.IndexOf( ".jpg" ) < 0 ) && ( FileName.IndexOf( ".gif" ) < 0 ) )332
temp+= "上传文件的格式不正确!\\n";333
if(myFile.PostedFile.ContentLength > size)334
temp+= "上传文件过大!";335
return temp;336
}337
#endregion 338

339

(12)压缩图片 RarImage(HtmlInputFile upImage, string imgName, string savePath,int nWidth, int nHeight, string pre) pre小图片前缀#region (12)压缩图片 RarImage(HtmlInputFile upImage, string imgName, string savePath,int nWidth, int nHeight, string pre) pre小图片前缀340
public static string RarImage(HtmlInputFile upImage, string imgName, string savePath,int nWidth, int nHeight, string pre)341

{342
string strReturn = ""; //返回值343
System.Drawing.Image image=null, newimage=null; //定义image类的对象344
string imgType,imgPath;345
double rarImgWidth,rarImgHeight,dScale;346

347
imgPath= upImage.PostedFile.FileName;348
imgType = imgPath.Substring(imgPath.LastIndexOf("."));349

350
351
if(pre =="")352
pre = "sml_";353
354
upImage.PostedFile.SaveAs(savePath+"\\"+imgName+imgType);355
image=System.Drawing.Image.FromFile(savePath+"/"+imgName+imgType);356
357
//确定压缩 宽 高358
if(image.Width > image.Height)359

{360
if(image.Width > nWidth)361

{362
dScale = image.Width/nWidth;363
rarImgWidth = nWidth;364
}365
else366

{367
dScale = 1;368
rarImgWidth = image.Width;369
}370

371
rarImgHeight = image.Height/dScale;372
}373
else374

{375
if(image.Height > nHeight)376

{377
dScale = image.Height/nHeight;378
rarImgHeight = nHeight;379
}380
else381

{382
dScale = 1;383
rarImgHeight = image.Height;384
}385
386
rarImgWidth = image.Width/dScale; 387
}388
389
newimage=image.GetThumbnailImage(Convert.ToInt32(rarImgWidth),Convert.ToInt32(rarImgHeight),null,new System.IntPtr()); //生成缩略图390
FileStream myOutput = new FileStream(savePath+"/"+pre+imgName+imgType,FileMode.Create, FileAccess.Write, FileShare.Write);391
newimage.Save(myOutput,GetImageType(imgType)); //把缩略图保存到指定的虚拟路径392
393
myOutput.Close();394
image.Dispose(); //释放image对象占用的资源395
newimage.Dispose(); //释放newimage对象的资源396

397
strReturn = imgName+imgType;398

399
return strReturn;400
}401

402
public static string RarImage(HtmlInputFile upImage, string fileName, string savePath,int nWidth, int nHeight)403

{404
return RarImage(upImage, fileName, savePath, nWidth, nHeight, "");405
}406
#endregion407

408

(13) HTML转换 static string UnHtml( object objResource)#region (13) HTML转换 static string UnHtml( object objResource)409
public static string UnHtml( object objResource )410

{411
if ( objResource == null )412

{413
return "";414
}415
string strResource = objResource.ToString();416
// strResource = strResource.Replace( "&", "&" );417
// strResource = strResource.Replace( "<", "<" );418
// strResource = strResource.Replace( ">", ">" );419
strResource = strResource.Replace( "\\n", "<br>" );420
strResource = strResource.Replace( Convert.ToString( Convert.ToChar( 34 ) ), """ );421
strResource = strResource.Replace( Convert.ToString( Convert.ToChar( 13 ) ), "<br>" );422
strResource = strResource.Replace( Convert.ToString( Convert.ToChar( 32 ) ), " " );423

424
return strResource;425
}426
#endregion427

428

(14) 遍历目录返回目录大小GetDirSize(string StrDir)#region (14) 遍历目录返回目录大小GetDirSize(string StrDir)429
private long GetDirSize(string StrDir)430

{431
long LngDirSize=0;432
DirectoryInfo TmpDir=new DirectoryInfo(StrDir);433

434
foreach(FileInfo file in TmpDir.GetFiles())435

{436
LngDirSize+=file.Length;437
}438

439
foreach(DirectoryInfo SubDir in TmpDir.GetDirectories())440

{441
LngDirSize+=GetDirSize(SubDir.FullName);442
}443

444
return LngDirSize;445
}446
#endregion447

448

(15) 返回图片高度GetImageHeight (System.IO.Stream strSourceFile)#region (15) 返回图片高度GetImageHeight (System.IO.Stream strSourceFile)449
public static int GetImageHeight (System.IO.Stream strSourceFile)450

{451
System.Drawing.Image SourceImage = System.Drawing.Image.FromStream( strSourceFile );452
int height=SourceImage.Height;453
SourceImage.Dispose();454
return height;455
}456
#endregion457

458

(16) 返回图片宽度GetImageHeight (System.IO.Stream strSourceFile)#region (16) 返回图片宽度GetImageHeight (System.IO.Stream strSourceFile)459
public static int GetImageWidth (System.IO.Stream strSourceFile)460

{461
System.Drawing.Image SourceImage = System.Drawing.Image.FromStream( strSourceFile );462
int width=SourceImage.Width;463
SourceImage.Dispose();464
return width;465
}466
#endregion467

468

//(17) 对文件目录压缩 ZipDirectory(string DirectoryPath,string ZipFile)#region //(17) 对文件目录压缩 ZipDirectory(string DirectoryPath,string ZipFile)469
// public static void ZipDirectory(string DirectoryPath,string ZipFile)470
// {471
// ZipClass zip=new ZipClass();472
// string []FileProperties=new string[2];473
// FileProperties[0]=DirectoryPath;//待压缩文件目录474
// FileProperties[1]=ZipFile; //压缩后的目标文件475
// zip.ZipFileMain( FileProperties );476
// }477
// #endregion478
//479
// #region (18) 对文件目录解压 UnZipDirectory(string DirectoryPath,string ZipFile)480
// public static void UnZipDirectory(string DirectoryPath,string ZipFile)481
// {482
// UnZipClass unzip=new UnZipClass();483
// string []FileProperties=new string[2];484
// FileProperties[0]=ZipFile;//待压解文件485
// FileProperties[1]=DirectoryPath; //解压后放置的目标目录486
// unzip.UnZip( FileProperties );487
// }488
#endregion489

490

(19) 加密string GetMd5Psw32( string str )#region (19) 加密string GetMd5Psw32( string str )491
public static string Md5Psw32( string str )492

{493
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"md5");494
}495
#endregion496

497

(20) 检测SQL注入 void StrCheck(object s)#region (20) 检测SQL注入 void StrCheck(object s)498
public static void StrCheck(object s)499

{500
string str= "'|;|and|(|)|--|exec|insert|select|delete|update|count|*|%|chr|mid|truncate|char|declare|drop|xp_cmdshell|exec master.dbo.xp_cmdshell|net user";501
string ss="";502
string[] str1=str.Split('|');503
504
if( s!=null )505

{506
ss=s.ToString().ToLower();507
StrCheck1(ss);508
for( int i=0;i<str1.Length;i++ )509

{510
if(ss.IndexOf(str1[i])>0)511

{512
//insert(ss);513
System.Web.HttpContext.Current.Response.Write("参数错误!");514
System.Web.HttpContext.Current.Response.End();515
}516
}517
}518
else519

{520
System.Web.HttpContext.Current.Response.Write("参数错误!");521
System.Web.HttpContext.Current.Response.End();522
}523
}524

525
public static void StrCheck2(object s)526

{527
string str= "'|;|and|(|)|--|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|drop|xp_cmdshell|exec master.dbo.xp_cmdshell|net user";528
string ss="";529
string[] str1=str.Split('|');530
531
if( s!=null )532

{533
ss=s.ToString().ToLower();534
535
for( int i=0;i<str1.Length;i++ )536

{537
if(ss.IndexOf(str1[i])>0)538

{539
System.Web.HttpContext.Current.Response.Write("参数错误!");540
System.Web.HttpContext.Current.Response.End();541
}542
}543
}544
else545

{546
System.Web.HttpContext.Current.Response.Write("参数错误!");547
System.Web.HttpContext.Current.Response.End();548
}549
}550

551
public static void StrCheck1(string s)552

{553
if( !IsNum(s) )554

{555
System.Web.HttpContext.Current.Response.Write("参数错误!");556
System.Web.HttpContext.Current.Response.End();557
}558
}559
#endregion560

561

(21) 输出硬盘文件,提供下载ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)#region (21) 输出硬盘文件,提供下载ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)562

/**//// <summary>563
/// 输出硬盘文件,提供下载564
/// </summary>565
/// <param name="_Request">Page.Request对象</param>566
/// <param name="_Response">Page.Response对象</param>567
/// <param name="_fileName">下载文件名</param>568
/// <param name="_fullPath">带文件名下载路径</param>569
/// <param name="_speed">_speed</param>570
/// <returns>返回是否成功</returns>571
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)572

{573
try574

{575
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);576
BinaryReader br = new BinaryReader(myFile);577
try578

{579
_Response.AddHeader("Accept-Ranges", "bytes");580
_Response.Buffer = false;581
long fileLength = myFile.Length;582
long startBytes = 0;583

584
int pack = 1024; //10K bytes585
//int sleep = 200; //每秒5次 即5*10K bytes每秒586
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;587
if (_Request.Headers["Range"] != null)588

{589
_Response.StatusCode = 206;590

string[] range = _Request.Headers["Range"].Split(new char[]
{'=', '-'});591
startBytes = Convert.ToInt64(range[1]);592
}593
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());594
if (startBytes != 0)595

{596
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));597
}598
_Response.AddHeader("Connection", "Keep-Alive");599
_Response.ContentType = "application/octet-stream";600
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );601

602
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);603
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;604

605
for (int i = 0; i < maxCount; i++)606

{607
if (_Response.IsClientConnected)608

{609
_Response.BinaryWrite(br.ReadBytes(pack));610
Thread.Sleep(sleep);611
}612
else613

{614
i=maxCount; 615
}616
}617
}618
catch619

{620
return false;621
}622
finally623

{624
br.Close();625
myFile.Close();626
}627
}628
catch629

{630
return false;631
}632
return true;633
}634
#endregion635

636
}637
}638


浙公网安备 33010602011771号