Sampson-Li
Sampson.Li学习经验总结博客 学历代表过去,能力代表现在.学习力代表未来!
 1  ///<summary>
2 /// 文件上传类
3 ///</summary>
4 public class UploadFile
5 {
6 ///<summary>
7 /// 上传文件公共方法
8 ///</summary>
9 ///<param name="PostedFile">PostedFile</param>
10 ///<param name="sPath">示例(格式)为:D:/Upload/</param>
11 ///<param name="sExtension">示例(格式)为:.jpg|.gif|.bmp|.png</param>
12 ///<param name="iSize">上传的文件不能超过的大小(单位为KB)</iSize>
13 ///<param name="flage">是否合格</param>
14 ///<returns></returns>
15 public static string UploadFileImg(HttpPostedFile PostedFile, string sPath, string sExtension, int iSize, out bool flage)
16 {
17 string fileName = null;
18 flage = true;
19
20 if (PostedFile != null)
21 {
22 fileName = System.IO.Path.GetFileName(PostedFile.FileName);
23 if (fileName != "")
24 {
25 flage = false;
26 //获得图片的后缀名
27 string imgExtension = System.IO.Path.GetExtension(fileName);
28 //判断是否是图片
29 if (sExtension.ToLower().IndexOf(imgExtension.ToLower()) == -1)
30 {
31 Csharp5K.WebLib.MessageBox.Show("只能上传格式为[" + sExtension + "]的图片文件");
32 }
33 else if (PostedFile.ContentLength / 1024 > iSize) // 上传文件不能大于 [iSize] KB
34 {
35 Csharp5K.WebLib.MessageBox.Show("上传图片不能超过" + iSize + "KB");
36 }
37 else
38 {
39 flage = true;
40 fileName = Guid.NewGuid().ToString("N") + imgExtension;
41 //如果 sPath 中存在“:”则为物理路径
42 if (sPath.IndexOf(":") != -1)
43 PostedFile.SaveAs(sPath + fileName);
44 else
45 PostedFile.SaveAs(HttpContext.Current.Server.MapPath(sPath + fileName));
46 }
47 }
48 }
49
50 return fileName;
51 }
52 }

图片缩略图and简单水印方法

View Code
  1 ///<summary>
2 /// 图形操作类
3 ///</summary>
4 public class Drawing
5 {
6 #region 图片处理
7 ///<summary>
8 /// 生成缩略图
9 ///</summary>
10 ///<param name="originalImagePath">源图路径(物理路径)</param>
11 ///<param name="thumbnailPath">缩略图路径(物理路径)</param>
12 ///<param name="width">缩略图宽度</param>
13 ///<param name="height">缩略图高度</param>
14 ///<param name="mode">生成缩略图的方式【HW:指定高宽缩放(可能变形);W:指定宽,高按比例;H:指定高,宽按比例;Cut:指定高宽裁减(不变形)】</param>
15 public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
16 {
17 //判断源图片是否存在,不存在就不执行
18 if (! File.Exists(originalImagePath)) return;
19
20 //缩略图
21 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
22
23 int towidth = width;
24 int toheight = height;
25
26 int x = 0;
27 int y = 0;
28 int ow = originalImage.Width;
29 int oh = originalImage.Height;
30
31 switch (mode.ToUpper())
32 {
33 case "HW"://指定高宽缩放(可能变形)
34 break;
35 case "W"://指定宽,高按比例
36 toheight = originalImage.Height * width / originalImage.Width;
37 break;
38 case "H"://指定高,宽按比例
39 towidth = originalImage.Width * height / originalImage.Height;
40 break;
41 case "Cut"://指定高宽裁减(不变形)
42 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
43 {
44 oh = originalImage.Height;
45 ow = originalImage.Height * towidth / toheight;
46 y = 0;
47 x = (originalImage.Width - ow) / 2;
48 }
49 else
50 {
51 ow = originalImage.Width;
52 oh = originalImage.Width * height / towidth;
53 x = 0;
54 y = (originalImage.Height - oh) / 2;
55 }
56 break;
57 default:
58 break;
59 }
60
61 //新建一个bmp图片
62 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
63
64 //新建一个画板
65 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
66
67 //设置高质量插值法
68 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
69
70 //设置高质量,低速度呈现平滑程度
71 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
72
73 //清空画布并以透明背景色填充
74 g.Clear(System.Drawing.Color.Transparent);
75
76 //在指定位置并且按指定大小绘制原图片的指定部分
77 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
78 new System.Drawing.Rectangle(x, y, ow, oh),
79 System.Drawing.GraphicsUnit.Pixel);
80
81 try
82 {
83 //以jpg格式保存缩略图
84 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
85 }
86 catch (System.Exception e)
87 {
88 throw e;
89 }
90 finally
91 {
92 originalImage.Dispose();
93 bitmap.Dispose();
94 g.Dispose();
95 }
96 }
97
98 ///<summary>
99 /// 在图片上增加文字水印,好像只能是JPG格式才可能加水印呢
100 ///</summary>
101 ///<param name="strText">文字</param>
102 ///<param name="sPath">原服务器图片路径</param>
103 ///<param name="sPath_sy">生成的带文字水印的图片路径</param>
104 ///<param name="iSize">大小</param>
105 public static void AddWater(string strText,string sPath, string sPath_sy,int iSize)
106 {
107 if (Path.GetExtension(sPath).ToUpper() != ".JPG") return;
108
109 System.Drawing.Image image = System.Drawing.Image.FromFile(sPath);
110 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
111 g.DrawImage(image, 0, 0, image.Width, image.Height);
112 System.Drawing.Font f = new System.Drawing.Font("Arial", iSize);
113 System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
114
115 g.DrawString(strText, f, b, 8, image.Height - 8 - iSize);
116 g.Dispose();
117
118 image.Save(sPath_sy);
119 image.Dispose();
120 }
121
122 ///<summary>
123 /// 在图片上生成图片水印
124 ///</summary>
125 ///<param name="Path">原服务器图片路径</param>
126 ///<param name="Path_syp">生成的带图片水印的图片路径</param>
127 ///<param name="Path_sypf">水印图片路径</param>
128 public static void AddWaterPic(string Path, string Path_syp, string Path_sypf)
129 {
130 System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
131 System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
132 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
133 g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
134 g.Dispose();
135
136 image.Save(Path_syp);
137 image.Dispose();
138 }
139 #endregion
140 }



posted on 2011-11-18 10:47  Sampson  阅读(292)  评论(0编辑  收藏  举报