Code
1 protected void Button1_ServerClick(object sender, System.EventArgs e)
2 {
3 if(File1.PostedFile!=null)
4 {
5 string fileName = File1.PostedFile.FileName ;
6 //取得上传文件的扩展名
7 string fileExtName =fileName.Substring(fileName.LastIndexOf("."));
8 //创建GUID,作为上传文件的唯一标识
9 System.Guid myGuid = System.Guid.NewGuid();
10 string savePath = Server.MapPath("~").ToString() + "//uploadfile//" + myGuid.ToString() + "." + fileExtName;
11 string newSavePath = Server.MapPath("~").ToString() + "//uploadfile1//" + myGuid.ToString() + "." + fileExtName;
12 string waterMarkPath = Server.MapPath("~").ToString() + "//uploadfile//watermark.bmp";
13 File1.PostedFile.SaveAs(savePath);
14 watermark myWatermark = new watermark();
15 myWatermark.MakeWatermark("北京朝阳",waterMarkPath,savePath,newSavePath);
16 // //文件的相关属性:文件名,文件类型,文件大小
17 // myFile.PostedFile.FileName;
18 // myFile.PostedFile.ContentType ;
19 // myFile.PostedFile.ContentLength.ToString();
20 }
21 }
22
1 protected void Button1_ServerClick(object sender, System.EventArgs e)
2 {
3 if(File1.PostedFile!=null)
4 {
5 string fileName = File1.PostedFile.FileName ;
6 //取得上传文件的扩展名
7 string fileExtName =fileName.Substring(fileName.LastIndexOf("."));
8 //创建GUID,作为上传文件的唯一标识
9 System.Guid myGuid = System.Guid.NewGuid();
10 string savePath = Server.MapPath("~").ToString() + "//uploadfile//" + myGuid.ToString() + "." + fileExtName;
11 string newSavePath = Server.MapPath("~").ToString() + "//uploadfile1//" + myGuid.ToString() + "." + fileExtName;
12 string waterMarkPath = Server.MapPath("~").ToString() + "//uploadfile//watermark.bmp";
13 File1.PostedFile.SaveAs(savePath);
14 watermark myWatermark = new watermark();
15 myWatermark.MakeWatermark("北京朝阳",waterMarkPath,savePath,newSavePath);
16 // //文件的相关属性:文件名,文件类型,文件大小
17 // myFile.PostedFile.FileName;
18 // myFile.PostedFile.ContentType ;
19 // myFile.PostedFile.ContentLength.ToString();
20 }
21 }
22
看看 类的处理
/**//*
* 给图片设置水印效果图的函数
* 可以在图片上添加自己的版权和LOGO图片的水印
*
* 原作者:Joel Neubeck ,from bbs.51aspx.com
*
* 本人在原作者的基础上进行了部分修改,并改为一个单独的函数
* 大家可以根据需要修改此代码
* 但请保留原作者信息.
*
* 星宿.net(zhuhee)
* 修改于2006-6-14
*
*/
Code
1 using System;
2 using System.Drawing;
3 using System.Drawing.Imaging;
4 using System.Drawing.Drawing2D;
5 namespace WebWaterMark.Components
6 {
7 /**//// <summary>
8 /// watermark 的摘要说明。
9 /// </summary>
10 public class watermark
11 {
12 public watermark()
13 {
14 //
15 // TODO: 在此处添加构造函数逻辑
16 //
17 }
18 /**//// <summary>
19 /// 用于处理图片
20 /// 给图片加入水印
21 /// </summary>
22 /// <param name="Copyright">需要写入的版权信息</param>
23 /// <param name="MarkBmpPath">需要假如的logo图片</param>
24 /// <param name="photoPath">需要处理的图片的路径</param>
25 /// <param name="savePhotoPath">保存的图片路径</param>
26 public void MakeWatermark(string Copyright,string MarkBmpPath,string photoPath,string savePhotoPath)
27 {
28 //创建一个image对象,即要被处理的图片
29 Image imgPhoto = Image.FromFile(photoPath);
30 int phWidth = imgPhoto.Width;
31 int phHeight = imgPhoto.Height;
32 //创建原始图片大小的Bitmap
33 Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
34 bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
35 //将位图bmPhoto加载到Graphics对象
36 Graphics grPhoto = Graphics.FromImage(bmPhoto);
37 //创建一个需要填充水银的Image对象
38 Image imgWatermark = new Bitmap(MarkBmpPath);
39 int wmWidth = imgWatermark.Width;
40 int wmHeight = imgWatermark.Height;
41 //------------------------------------------------------------
42 //第一步,插入版权信息
43 //------------------------------------------------------------
44 //设置图象的成相质量
45 grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
46 //将原始图象绘制到grPhoto上
47 grPhoto.DrawImage(
48 imgPhoto, // 要绘制的Image对象
49 new Rectangle(0, 0, phWidth, phHeight), // 绘制图象的位置和大小
50 0, // 要绘制的原图象部分的左上角的X坐标
51 0, // 要绘制的原图象部分的左上角的Y坐标
52 phWidth, // 要绘制的原图象的高度
53 phHeight, // 要绘制的原图象的宽度
54 GraphicsUnit.Pixel); // 源矩形的度量单位
55 //-------------------------------------------------------
56 //字体大小放在一个数组中,最大字体为32.
57 //感觉用处不大,可以自己再修改这里
58 //-------------------------------------------------------
59 int[] sizes = new int[]{32,14,12,10,8,6,4};
60 Font crFont = null;
61 SizeF crSize = new SizeF();
62 //循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小
63 for (int i=0 ;i<6; i++)
64 {
65 //设置字体类型,可以单独提出,作为参数
66 crFont = new Font("迷你繁篆书", sizes[0], FontStyle.Bold);
67 //测量此种字体大小
68 crSize = grPhoto.MeasureString(Copyright, crFont);
69 if((ushort)crSize.Width < (ushort)phWidth)
70 break;
71 }
72 //给底部保留%3的空间
73 int yPixlesFromBottom = (int)(phHeight *.03);
74 //设置字体在图片中的位置
75 float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));
76 //float xCenterOfImg = (phWidth/2);
77 float xCenterOfImg = (phWidth-(crSize.Width)/2);
78 //设置字体居中
79 StringFormat StrFormat = new StringFormat();
80 StrFormat.Alignment = StringAlignment.Center;
81
82 //设置绘制文本的颜色和纹理 (Alpha=153)
83 SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
84 //将版权信息绘制到图象上
85 grPhoto.DrawString(Copyright, //版权信息
86 crFont, //字体
87 semiTransBrush2, //绘制文本的颜色及纹理
88 new PointF(xCenterOfImg+1,yPosFromBottom+1), //绘制文本的位置
89 StrFormat); //格式
90 //设置绘制文本的颜色和纹理 (Alpha=153)
91 SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
92 //重新绘制版权信息,以让其具有阴影效果
93 //将文本向又下移动一个象素
94 grPhoto.DrawString(Copyright, //版权信息
95 crFont, //字体
96 semiTransBrush, //绘制文本的颜色及纹理
97 new PointF(xCenterOfImg,yPosFromBottom), //绘制文本的位置
98 StrFormat); //格式
99
100 //------------------------------------------------------------
101 //第二步,插入图片水印
102 //------------------------------------------------------------
103 //在原来修改过的bmPhoto上创建一个水银位图
104 Bitmap bmWatermark = new Bitmap(bmPhoto);
105 bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
106 //将位图bmWatermark加载到Graphics对象
107 Graphics grWatermark = Graphics.FromImage(bmWatermark);
108 //To achieve a transulcent watermark we will apply (2) color
109 //manipulations by defineing a ImageAttributes object and
110 //seting (2) of its properties.
111 ImageAttributes imageAttributes = new ImageAttributes();
112 //The first step in manipulating the watermark image is to replace
113 //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
114 //to do this we will use a Colormap and use this to define a RemapTable
115 ColorMap colorMap = new ColorMap();
116 //My watermark was defined with a background of 100% Green this will
117 //be the color we search for and replace with transparency
118 colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
119 colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
120 ColorMap[] remapTable = { colorMap };
121 imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
122 //The second color manipulation is used to change the opacity of the
123 //watermark. This is done by applying a 5x5 matrix that contains the
124 //coordinates for the RGBA space. By setting the 3rd row and 3rd column
125 //to 0.3f we achive a level of opacity
126 float[][] colorMatrixElements = {
127 new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
128 new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
129 new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
130 new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
131 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
132 ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
133 imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
134 ColorAdjustType.Bitmap);
135 //For this example we will place the watermark in the upper right
136 //hand corner of the photograph. offset down 10 pixels and to the
137 //left 10 pixles
138 int xPosOfWm = ((phWidth - wmWidth) - 10);
139 int yPosOfWm = 10;
140 grWatermark.DrawImage(imgWatermark,
141 new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //Set the detination Position
142 0, // x-coordinate of the portion of the source image to draw.
143 0, // y-coordinate of the portion of the source image to draw.
144 wmWidth, // Watermark Width
145 wmHeight, // Watermark Height
146 GraphicsUnit.Pixel, // Unit of measurment
147 imageAttributes); //ImageAttributes Object
148 //Replace the original photgraphs bitmap with the new Bitmap
149 imgPhoto = bmWatermark;
150 grPhoto.Dispose();
151 grWatermark.Dispose();
152 //save new image to file system.
153 imgPhoto.Save(savePhotoPath, ImageFormat.Jpeg);
154 imgPhoto.Dispose();
155 imgWatermark.Dispose();
156 }
157 }
158 }
159
1 using System;
2 using System.Drawing;
3 using System.Drawing.Imaging;
4 using System.Drawing.Drawing2D;
5 namespace WebWaterMark.Components
6 {
7 /**//// <summary>
8 /// watermark 的摘要说明。
9 /// </summary>
10 public class watermark
11 {
12 public watermark()
13 {
14 //
15 // TODO: 在此处添加构造函数逻辑
16 //
17 }
18 /**//// <summary>
19 /// 用于处理图片
20 /// 给图片加入水印
21 /// </summary>
22 /// <param name="Copyright">需要写入的版权信息</param>
23 /// <param name="MarkBmpPath">需要假如的logo图片</param>
24 /// <param name="photoPath">需要处理的图片的路径</param>
25 /// <param name="savePhotoPath">保存的图片路径</param>
26 public void MakeWatermark(string Copyright,string MarkBmpPath,string photoPath,string savePhotoPath)
27 {
28 //创建一个image对象,即要被处理的图片
29 Image imgPhoto = Image.FromFile(photoPath);
30 int phWidth = imgPhoto.Width;
31 int phHeight = imgPhoto.Height;
32 //创建原始图片大小的Bitmap
33 Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
34 bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
35 //将位图bmPhoto加载到Graphics对象
36 Graphics grPhoto = Graphics.FromImage(bmPhoto);
37 //创建一个需要填充水银的Image对象
38 Image imgWatermark = new Bitmap(MarkBmpPath);
39 int wmWidth = imgWatermark.Width;
40 int wmHeight = imgWatermark.Height;
41 //------------------------------------------------------------
42 //第一步,插入版权信息
43 //------------------------------------------------------------
44 //设置图象的成相质量
45 grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
46 //将原始图象绘制到grPhoto上
47 grPhoto.DrawImage(
48 imgPhoto, // 要绘制的Image对象
49 new Rectangle(0, 0, phWidth, phHeight), // 绘制图象的位置和大小
50 0, // 要绘制的原图象部分的左上角的X坐标
51 0, // 要绘制的原图象部分的左上角的Y坐标
52 phWidth, // 要绘制的原图象的高度
53 phHeight, // 要绘制的原图象的宽度
54 GraphicsUnit.Pixel); // 源矩形的度量单位
55 //-------------------------------------------------------
56 //字体大小放在一个数组中,最大字体为32.
57 //感觉用处不大,可以自己再修改这里
58 //-------------------------------------------------------
59 int[] sizes = new int[]{32,14,12,10,8,6,4};
60 Font crFont = null;
61 SizeF crSize = new SizeF();
62 //循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小
63 for (int i=0 ;i<6; i++)
64 {
65 //设置字体类型,可以单独提出,作为参数
66 crFont = new Font("迷你繁篆书", sizes[0], FontStyle.Bold);
67 //测量此种字体大小
68 crSize = grPhoto.MeasureString(Copyright, crFont);
69 if((ushort)crSize.Width < (ushort)phWidth)
70 break;
71 }
72 //给底部保留%3的空间
73 int yPixlesFromBottom = (int)(phHeight *.03);
74 //设置字体在图片中的位置
75 float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2));
76 //float xCenterOfImg = (phWidth/2);
77 float xCenterOfImg = (phWidth-(crSize.Width)/2);
78 //设置字体居中
79 StringFormat StrFormat = new StringFormat();
80 StrFormat.Alignment = StringAlignment.Center;
81
82 //设置绘制文本的颜色和纹理 (Alpha=153)
83 SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
84 //将版权信息绘制到图象上
85 grPhoto.DrawString(Copyright, //版权信息
86 crFont, //字体
87 semiTransBrush2, //绘制文本的颜色及纹理
88 new PointF(xCenterOfImg+1,yPosFromBottom+1), //绘制文本的位置
89 StrFormat); //格式
90 //设置绘制文本的颜色和纹理 (Alpha=153)
91 SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
92 //重新绘制版权信息,以让其具有阴影效果
93 //将文本向又下移动一个象素
94 grPhoto.DrawString(Copyright, //版权信息
95 crFont, //字体
96 semiTransBrush, //绘制文本的颜色及纹理
97 new PointF(xCenterOfImg,yPosFromBottom), //绘制文本的位置
98 StrFormat); //格式
99
100 //------------------------------------------------------------
101 //第二步,插入图片水印
102 //------------------------------------------------------------
103 //在原来修改过的bmPhoto上创建一个水银位图
104 Bitmap bmWatermark = new Bitmap(bmPhoto);
105 bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
106 //将位图bmWatermark加载到Graphics对象
107 Graphics grWatermark = Graphics.FromImage(bmWatermark);
108 //To achieve a transulcent watermark we will apply (2) color
109 //manipulations by defineing a ImageAttributes object and
110 //seting (2) of its properties.
111 ImageAttributes imageAttributes = new ImageAttributes();
112 //The first step in manipulating the watermark image is to replace
113 //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
114 //to do this we will use a Colormap and use this to define a RemapTable
115 ColorMap colorMap = new ColorMap();
116 //My watermark was defined with a background of 100% Green this will
117 //be the color we search for and replace with transparency
118 colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
119 colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
120 ColorMap[] remapTable = { colorMap };
121 imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
122 //The second color manipulation is used to change the opacity of the
123 //watermark. This is done by applying a 5x5 matrix that contains the
124 //coordinates for the RGBA space. By setting the 3rd row and 3rd column
125 //to 0.3f we achive a level of opacity
126 float[][] colorMatrixElements = {
127 new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
128 new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
129 new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
130 new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
131 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
132 ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
133 imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
134 ColorAdjustType.Bitmap);
135 //For this example we will place the watermark in the upper right
136 //hand corner of the photograph. offset down 10 pixels and to the
137 //left 10 pixles
138 int xPosOfWm = ((phWidth - wmWidth) - 10);
139 int yPosOfWm = 10;
140 grWatermark.DrawImage(imgWatermark,
141 new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //Set the detination Position
142 0, // x-coordinate of the portion of the source image to draw.
143 0, // y-coordinate of the portion of the source image to draw.
144 wmWidth, // Watermark Width
145 wmHeight, // Watermark Height
146 GraphicsUnit.Pixel, // Unit of measurment
147 imageAttributes); //ImageAttributes Object
148 //Replace the original photgraphs bitmap with the new Bitmap
149 imgPhoto = bmWatermark;
150 grPhoto.Dispose();
151 grWatermark.Dispose();
152 //save new image to file system.
153 imgPhoto.Save(savePhotoPath, ImageFormat.Jpeg);
154 imgPhoto.Dispose();
155 imgWatermark.Dispose();
156 }
157 }
158 }
159