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
看看 類的處理
/**//*
* 給圖檔設定水印效果圖的函數
* 可以在圖檔上添加自己的版權和LOGO圖檔的水印
*
* 原作者:Joel Neubeck ,from bbs.51aspx.com
* 本人在原作者的基礎上進行了部分修改,并改為一個單獨的函數
* 大家可以根據需要修改此代碼
* 但請保留原作者資訊.
* 星宿.net(zhuhee)
* 修改于2006-6-14
*/
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