前言
网站后台都需要有上传图片的功能,下面的例子就是实现有关图片上传。缺点:图片上传到本服务器上,不适合大量图片上传。
第一、图片上传,代码如下:
1 xxx.aspx 2 <td class="style1"> 3 <asp:FileUpload ID="FileUpload1" runat="server" /> 4 <asp:Button ID="Button1" runat="server" Text="上传一般图片" onclick="Button1_Click" /> 5 </td> 6 <td class="style3"> 7 <asp:Image ID="Image1" runat="server" Height="200px" Width="200px" /> 8 </td> 9 10 xxx.aspx.cs 11 protected void Button1_Click(object sender, EventArgs e) 12 { 13 for (int i = 0; i < Request.Files.Count; i++) 14 { 15 HttpPostedFile file = Request.Files[i]; 16 if (file.ContentLength > 0) 17 { 18 if (file.ContentType.Contains("image/")) 19 { 20 using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream)) 21 { 22 string FileName = System.IO.Path.GetFileName(file.FileName); 23 string[] SplitFileName = FileName.Split('.'); 24 string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss")+"." + SplitFileName[1]; 25 img.Save(Server.MapPath("/upload/" + AtterFileName)); 26 27 this.Image1.ImageUrl = "upload/" + AtterFileName; 28 } 29 } 30 else 31 { 32 Response.Write("<script>alert('该文件不是图片格式!');</script>"); 33 } 34 } 35 else 36 { 37 Response.Write("<script>alert('请选择要上传的图片');</script>"); 38 } 39 40 } 41 }
第二、添加文字水印的图片上传,代码如下:
1 xxx.aspx 2 <td class="style1"> 3 <asp:FileUpload ID="FileUpload2" runat="server" /> 4 <asp:Button ID="Button2" runat="server" Text="上传文字图片" onclick="Button2_Click" /> 5 </td> 6 <td> 7 <asp:Image ID="Image2" runat="server" Height="200px" Width="200px" /> 8 </td> 9 10 xxx.aspx.cs 11 protected void Button2_Click(object sender, EventArgs e) 12 { 13 for (int i = 0; i < Request.Files.Count; i++) 14 { 15 HttpPostedFile file = Request.Files[i]; 16 if (file.ContentLength > 0) 17 { 18 if (file.ContentType.Contains("image/")) 19 { 20 using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream)) 21 { 22 using (Graphics g = Graphics.FromImage(img)) 23 { 24 g.DrawString("我的图片", new Font("宋体", 14), Brushes.Red, 0, 0); 25 } 26 string FileName = System.IO.Path.GetFileName(file.FileName); 27 string[] SplitFileName = FileName.Split('.'); 28 string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1]; 29 img.Save(Server.MapPath("/upload/" + AtterFileName)); 30 this.Image2.ImageUrl = "upload/" + AtterFileName; 31 } 32 } 33 else 34 { 35 Response.Write("<script>alert('该文件不是图片格式!');</script>"); 36 } 37 } 38 else 39 { 40 Response.Write("<script>alert('请选择要上传的图片');</script>"); 41 } 42 43 } 44 }
第三、添加图片水印的图片上传,代码如下:
1 xxx.aspx 2 <td class="style1"> 3 <asp:FileUpload ID="FileUpload3" runat="server" /> 4 <asp:Button ID="Button3" runat="server" Text="上传水印图片" onclick="Button3_Click" /> 5 </td> 6 <td> 7 <asp:Image ID="Image3" runat="server" Height="200px" Width="200px" /> 8 </td> 9 10 xxx.aspx.cs 11 protected void Button3_Click(object sender, EventArgs e) 12 { 13 for (int i = 0; i < Request.Files.Count; i++) 14 { 15 HttpPostedFile file = Request.Files[i]; 16 if (file.ContentLength > 0) 17 { 18 if (file.ContentType.Contains("image/")) 19 { 20 string fileName = file.FileName; 21 using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream)) 22 { 23 using (System.Drawing.Image imgWater = System.Drawing.Image.FromFile(Server.MapPath("/img/czlogo.jpg"))) 24 { 25 using (Graphics g = Graphics.FromImage(img)) 26 { 27 g.DrawImage(imgWater, 0, 0); 28 } 29 string[] SplitFileName = fileName.Split('.'); 30 string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1]; 31 img.Save(Server.MapPath("/upload/" + AtterFileName)); 32 this.Image3.ImageUrl = "upload/" + AtterFileName; 33 } 34 } 35 } 36 else 37 { 38 Response.Write("<script>alert('该文件不是图片格式!');</script>"); 39 } 40 } 41 else 42 { 43 Response.Write("<script>alert('请选择要上传的图片');</script>"); 44 } 45 } 46 }
第四、上传图片浓缩图,代码如下:
1 xxx.aspx 2 <td class="style1"> 3 <asp:FileUpload ID="FileUpload4" runat="server" /> 4 <asp:Button ID="Button4" runat="server" Text="上传浓缩图片" onclick="Button4_Click" /> 5 </td> 6 <td> 7 <asp:Image ID="Image4" runat="server" Height="200px" Width="200px" /> 8 </td> 9 10 xxx.aspx.cs 11 protected void Button4_Click(object sender, EventArgs e) 12 { 13 for (int i = 0; i < Request.Files.Count; i++) 14 { 15 HttpPostedFile file = Request.Files[i]; 16 if (file.ContentLength > 0) 17 { 18 if (file.ContentType.Contains("image/")) 19 { 20 using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream)) 21 { 22 using (System.Drawing.Image imgThumb = new Bitmap(200, 100)) 23 { 24 using (Graphics g = Graphics.FromImage(imgThumb)) 25 { 26 g.DrawImage(img, new Rectangle(0, 0, imgThumb.Width, imgThumb.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); 27 } 28 string fileName = file.FileName; 29 string[] SplitFileName = fileName.Split('.'); 30 string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1]; 31 img.Save(Server.MapPath("/upload/" + AtterFileName)); 32 this.Image4.ImageUrl = "upload/" + AtterFileName; 33 } 34 } 35 } 36 else 37 { 38 Response.Write("<script>alert('该文件不是图片格式!');</script>"); 39 } 40 } 41 else 42 { 43 Response.Write("<script>alert('请选择要上传的图片');</script>"); 44 } 45 } 46 47 }