查msdn,
截图如下:
代码如下:
View Code
1 if (this.FileUpload1.HasFile)
2 {
3 string filename = this.FileUpload1.FileName;//获得文件名
4 string fileExtension = Path.GetExtension(filename).ToUpper();//获得扩展名
5 string filenameforSave = DateTime.Now.ToFileTimeUtc().ToString() ;
6 double size = this.FileUpload1.PostedFile.ContentLength/1024;
7 string sFileMap = Server.MapPath(@"img\") + filenameforSave;
8 if (size < 200)
9 {
10 if (fileExtension == ".JPG" || fileExtension == ".BMP" || fileExtension == ".GIF")
11 {
12 this.FileUpload1.SaveAs(sFileMap+fileExtension);
13
14 System.Drawing.Image image = System.Drawing.Image.FromFile(sFileMap+fileExtension);
15 #region 文字水印
16 //Graphics graphics = Graphics.FromImage(image);
17 //graphics.DrawImage(image, 0, 0, image.Width, image.Height);
18 //Font f = new Font("Verdana", 32);
19 //Brush b = new SolidBrush(Color.Red);
20 //graphics.DrawString(this.TextBox1.Text.Trim(), f, b, 10, 10);
21 //graphics.Flush();
22 //graphics.Dispose();
23 //image.Save(Server.MapPath(@"img\") + filenameforSave+"_new" + fileExtension);
24 //image.Dispose();
25 #endregion
26 #region 图片水印
27 //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Server.MapPath(@"img\") + "logo.gif");
28 //Graphics graphics = Graphics.FromImage(image);
29 //graphics.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
30 //graphics.Flush();
31 //image.Save(sFileMap + "11_new" + fileExtension);
32 //image.Dispose();
33 #endregion
34 }
35 else
36 {
37 Page.RegisterStartupScript("", "<script language=javascript>alert('上传的文件格式错误!');location='javascript:history.go(-1)'</script>");
38 return;
39 }
40 }
41 else
42 {
43 Page.RegisterStartupScript("", "<script language=javascript>alert('上传的文件不能大于200KB!');location='javascript:history.go(-1)'</script>");
44 return;
45 }
46 }
47 else
48 {
49 Page.RegisterStartupScript("", "<script language=javascript>alert('请选择上传的文件!');location='javascript:history.go(-1)'</script>");
50 return;
51 }
52 }