fileUpLoad上傳檔案,縮圖,加浮水印

來源:互聯網
上載者:User

using System;<br />using System.Data;<br />using System.Configuration;<br />using System.Collections;<br />using System.Web;<br />using System.Web.Security;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.WebControls.WebParts;<br />using System.Web.UI.HtmlControls;<br />using System.IO;</p><p>namespace WebApplication1<br />{<br /> public partial class _Default : System.Web.UI.Page<br /> {<br /> protected void Page_Load(object sender, EventArgs e)<br /> {</p><p> }</p><p> protected void btn_sure_Click(object sender, EventArgs e)<br /> {<br /> string filecontentType = FileUpload1.PostedFile.ContentType;<br /> if (filecontentType == "image/bmp" || filecontentType == "image/pjpeg" || filecontentType == "image/gif")<br /> {<br /> string name = FileUpload1.PostedFile.FileName;<br /> FileInfo file = new FileInfo(name);<br /> string fileName = file.Name;<br /> string fileName_s = "s_" + file.Name;<br /> string fileName_sy = "st_" + file.Name;<br /> string fileName_syp = "syp_" + file.Name;<br /> string webFilePath = Server.MapPath("file/" + fileName);<br /> string webFilePath_s = Server.MapPath("file/" + fileName_s);<br /> string webFilePath_sy = Server.MapPath("file/" + fileName_sy);<br /> string webfilePath_syp = Server.MapPath("file/" + fileName_syp);<br /> string webfilePath_sypf = Server.MapPath("file/suiyin.jpg");<br /> if (!File.Exists(webFilePath))<br /> {<br /> try<br /> {<br /> FileUpload1.SaveAs(webFilePath);<br /> AddShuiYinWord(webFilePath, webFilePath_sy);<br /> AddShuiYinPic(webFilePath, webfilePath_syp, webFilePath_sy);<br /> MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut");<br /> Label1.Text = "提示:檔案 " + fileName + " 成功上傳,並產生 " + fileName_s + " 縮圖,檔案類型為:" + FileUpload1.PostedFile.ContentType + ",檔案大小為:" + FileUpload1.PostedFile.ContentLength + "B";<br /> }<br /> catch (Exception ex)<br /> {<br /> Label1.Text = "提示;檔案上傳失敗,失敗原因:" + ex.Message;<br /> }<br /> }<br /> else<br /> {<br /> Label1.Text = "提示:檔案已經存在,請重新上傳~!";<br /> }<br /> }<br /> else<br /> {<br /> Label1.Text = "提示:檔案類型不符";<br /> }<br /> }</p><p> /// <summary><br /> /// 產生縮圖<br /> /// </summary><br /> /// <param name="originalImagePath">源圖路徑(實體路徑)</param><br /> /// <param name="thumbnailPath">縮圖路徑(實體路徑)</param><br /> /// <param name="width">縮圖寬度</param><br /> /// <param name="height">縮圖高度</param><br /> /// <param name="mode">產生縮圖的方式</param><br /> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)<br /> {<br /> System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);<br /> int towidth = width;<br /> int toheight = height;<br /> int x = 0;<br /> int y = 0;<br /> int ow = originalImage.Width;<br /> int oh = originalImage.Height;<br /> switch (mode)<br /> {<br /> case "HW":<br /> break;<br /> case "W":<br /> toheight = originalImage.Height * width / originalImage.Width;<br /> break;<br /> case "H":<br /> towidth = originalImage.Width * height / originalImage.Height;<br /> break;<br /> case "Cut":<br /> if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)<br /> {<br /> oh = originalImage.Height;<br /> ow = originalImage.Height * towidth / toheight;<br /> y = 0;<br /> x = (originalImage.Width - ow) / 2;<br /> }<br /> else<br /> {<br /> ow = originalImage.Width;<br /> oh = originalImage.Width * height / towidth;<br /> x = 0;<br /> y = (originalImage.Width - ow) / 2;<br /> }<br /> break;<br /> default:<br /> break;<br /> }<br /> System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);<br /> System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);<br /> g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;<br /> g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;<br /> g.Clear(System.Drawing.Color.Transparent);<br /> g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);<br /> try<br /> {<br /> bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);<br /> }<br /> catch (Exception e)<br /> {<br /> throw e;<br /> }<br /> finally<br /> {<br /> originalImage.Dispose();<br /> bitmap.Dispose();<br /> g.Dispose();<br /> }<br /> }<br /> /// <summary><br /> /// 在圖片上增加文字浮水印<br /> /// </summary><br /> /// <param name="Path">原伺服器圖片路徑</param><br /> /// <param name="Path_sy">產生的帶文字浮水印的圖片路徑</param><br /> protected void AddShuiYinWord(string Path, string Path_sy)<br /> {<br /> string addText = "我愛李婷婷";<br /> System.Drawing.Image image = System.Drawing.Image.FromFile(Path);<br /> System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);<br /> g.DrawImage(image, 0, 0, image.Width, image.Height);<br /> System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);<br /> System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);<br /> g.DrawString(addText, f, b, 15, 15);<br /> g.Dispose();<br /> image.Save(Path_sy);<br /> image.Dispose();<br /> }<br /> /// <summary><br /> /// 在圖片上產生圖片浮水印<br /> /// </summary><br /> /// <param name="Path">原伺服器片路徑</param><br /> /// <param name="Path_syp">產生的帶圖片浮水印的圖片路徑</param><br /> /// <param name="Path_sypf">浮水印圖片路徑</param><br /> protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)<br /> {<br /> System.Drawing.Image image = System.Drawing.Image.FromFile(Path);<br /> System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);<br /> System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);<br /> 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);<br /> g.Dispose();<br /> image.Save(Path_syp);<br /> image.Dispose();<br /> } </p><p> }<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.