![使用FileUpload控件上傳圖片并自動生成縮略圖_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/3/74709f1f-61cf-4bac-8aff-ff858deb98a6/74709f1f-61cf-4bac-8aff-ff858deb98a61.gif)
![使用FileUpload控件上傳圖片并自動生成縮略圖_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/3/74709f1f-61cf-4bac-8aff-ff858deb98a6/74709f1f-61cf-4bac-8aff-ff858deb98a62.gif)
![使用FileUpload控件上傳圖片并自動生成縮略圖_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/3/74709f1f-61cf-4bac-8aff-ff858deb98a6/74709f1f-61cf-4bac-8aff-ff858deb98a63.gif)
![使用FileUpload控件上傳圖片并自動生成縮略圖_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/3/74709f1f-61cf-4bac-8aff-ff858deb98a6/74709f1f-61cf-4bac-8aff-ff858deb98a64.gif)
![使用FileUpload控件上傳圖片并自動生成縮略圖_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/3/74709f1f-61cf-4bac-8aff-ff858deb98a6/74709f1f-61cf-4bac-8aff-ff858deb98a65.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;public partial class upfile_upfi
2、le : System.Web.UI.Pageprotected void Page_Load(object sender, EventArgs e protected void Button1_Click(object sender, EventArgs eif (FileUpload1.HasFilestring fileContentType = FileUpload1.PostedFile.ContentType;if (fileContentType = image/bmp | fileContentType = image/gif | fileContentType = image
3、/pjpegstring name = FileUpload1.PostedFile.FileName; / 客戶端文件路徑FileInfo file = new FileInfo(name;string fileName = file.Name; / 文件名稱string fileName_s = s_ + file.Name; / 縮略圖文件名稱string fileName_sy = sy_ + file.Name; / 水印圖文件名稱(文字)string fileName_syp = syp_ + file.Name; / 水印圖文件名稱(圖片)string webFilePath =
4、 Server.MapPath(file/ + fileName; / 服務(wù)器端文件路徑string webFilePath_s = Server.MapPath(file/ + fileName_s;/ 服務(wù)器端縮略圖路徑string webFilePath_sy = Server.MapPath(file/ + fileName_sy;/ 服務(wù)器端帶水印圖路徑(文字string webFilePath_syp = Server.MapPath(file/ + fileName_syp;/ 服務(wù)器端帶水印圖路徑(圖片string webFilePath_sypf = Server.MapPa
5、th(file/shuiyin.jpg;/ 服務(wù)器端水印圖路徑(圖片if (!File.Exists(webFilePathtryFileUpload1.SaveAs(webFilePath; / 使用 SaveAs 方法保存文件AddShuiYinWord(webFilePath, webFilePath_sy;AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf;MakeThumbnail(webFilePath, webFilePath_s, 130, 130, Cut; / 生成縮略圖方法Label1.Text = 提
6、示:文件“ + fileName + ”成功上傳,并生成“ + fileName_s + ”縮略圖,文件類型為: + FileUpload1.PostedFile.ContentType + ,文件大小為: + FileUpload1.PostedFile.ContentLength + B;catch (Exception exLabel1.Text = 提示:文件上傳失敗,失敗原因: + ex.Message;elseLabel1.Text = 提示:文件已經(jīng)存在,請重命名后上傳;elseLabel1.Text = 提示:文件類型不符;/ / 生成縮略圖/ / 源圖路徑(物理路徑)/ 縮略
7、圖路徑(物理路徑)/ 縮略圖寬度/ 縮略圖高度/ 生成縮略圖的方式 public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string modeSystem.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath;int towidth = width;int toheight = height;int x = 0;int y = 0;int
8、ow = originalImage.Width;int oh = originalImage.Height;switch (modecase HW:/指定高寬縮放(可能變形) break;case W:/指定寬,高按比例 toheight = originalImage.Height * width / originalImage.Width;break;case H:/指定高,寬按比例towidth = originalImage.Width * height / originalImage.Height;break;case Cut:/指定高寬裁減(不變形) if (doubleorig
9、inalImage.Width / (doubleoriginalImage.Height (doubletowidth / (doubletoheightoh = originalImage.Height;ow = originalImage.Height * towidth / toheight;y = 0;x = (originalImage.Width - ow / 2;elseow = originalImage.Width;oh = originalImage.Width * height / towidth;x = 0;y = (originalImage.Height - oh
10、 / 2;break;default:break;/新建一個bmp圖片System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight;/新建一個畫板System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap;/設(shè)置高質(zhì)量插值法g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;/設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度g.SmoothingMode = Sy
11、stem.Drawing.Drawing2D.SmoothingMode.HighQuality;/清空畫布并以透明背景色填充g.Clear(System.Drawing.Color.Transparent;/在指定位置并且按指定大小繪制原圖片的指定部分g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight,new System.Drawing.Rectangle(x, y, ow, oh,System.Drawing.GraphicsUnit.Pixel;try/以jpg格式保存縮略圖bi
12、tmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg;catch (System.Exception ethrow e;finallyoriginalImage.Dispose(;bitmap.Dispose(;g.Dispose(;/ / 在圖片上增加文字水印/ / 原服務(wù)器圖片路徑/ 生成的帶文字水印的圖片路徑protected void AddShuiYinWord(string Path, string Path_systring addText = 測試水印;System.Drawing.Image imag
13、e = System.Drawing.Image.FromFile(Path;System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image;g.DrawImage(image, 0, 0, image.Width, image.Height;System.Drawing.Font f = new System.Drawing.Font(Verdana, 16;System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue;g
14、.DrawString(addText, f, b, 15, 15;g.Dispose(;image.Save(Path_sy;image.Dispose(;/*/ / 在圖片上生成圖片水印/ / 原服務(wù)器圖片路徑/ 生成的帶圖片水印的圖片路徑/ 水印圖片路徑protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypfSystem.Drawing.Image image = System.Drawing.Image.FromFile(Path;System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf;System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image;g.DrawImage(copyImage, new System.Drawing.R
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度攤鋪機租賃與操作培訓(xùn)合同范本
- 個人合伙的協(xié)議書(15篇)
- 設(shè)計方案評審函
- 2025年健身俱樂部事故免責(zé)合同
- 2025年人工智能合作協(xié)議書
- 2025年臨時用電合作協(xié)議書規(guī)范文本
- 2025年飛機空調(diào)車ACM項目規(guī)劃申請報告模稿
- 2025年共同經(jīng)營商業(yè)地產(chǎn)合作協(xié)議
- 2025年短期勞動合同范例
- 2025年專利申請授權(quán)實施合同樣本
- 2023第二學(xué)期八年級英語備課組工作總結(jié)
- 國企經(jīng)理層任期制和契約化管理任期制與契約化相關(guān)模板
- 壓力管道檢驗員題庫
- 動脈采血操作評分標(biāo)準(zhǔn)
- 病理科科科內(nèi)會診記錄
- 小學(xué)生主題班會 弘揚航天精神 課件 (27張PPT)
- 電力服務(wù)收費標(biāo)準(zhǔn)附表
- 小學(xué)主題班會教學(xué)設(shè)計-《給你點個“贊”》通用版
- 【教學(xué)創(chuàng)新大賽】《系統(tǒng)解剖學(xué)》教學(xué)創(chuàng)新成果報告
- 賽意EAM設(shè)備管理IOT解決方案
- 初中語文-《桃花源記》教學(xué)課件設(shè)計
評論
0/150
提交評論