asp自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖_第1頁
asp自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖_第2頁
asp自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖_第3頁
asp自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖_第4頁
asp自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖_第5頁
已閱讀5頁,還剩6頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、 自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖如圖:點(diǎn)擊瀏覽,選擇圖片之后,右面顯示圖片 第一步:創(chuàng)建CtFileUpLoad.ascx復(fù)制代碼 代碼如下: iframe src=/WebParts/FileUpLoad.aspx? width=240px height=22px frameborder=0 scrolling=no function CallLoaded() ; 復(fù)制代碼 代碼如下:using System; using System.Collections; using System.Configu

2、ration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Drawing; public partial class WebParts_CtFileUpLoad : System.Web.UI.UserControl publi

3、c bool AutoFileName get object ob = ViewStateClientID + AutoFileName; if (ob = null) ob = true; return (bool)ob; set ViewStateClientID + AutoFileName = value; public string UpLoadPath get object ob = ViewStateClientID + UpLoadPath; if (ob = null) ob = UPLOADFILES; return ob.ToString(); set ViewState

4、ClientID + UpLoadPath = value; public string OnLoaded get object ob = ViewStateClientID + OnLoaded; if (ob = null) ob = ; return ob.ToString(); set ViewStateClientID + OnLoaded = value; public string SupportExtension get object ob = ViewStateClientID + SupportExtension; if (ob = null) ob = gif|png|j

5、peg|jpg; return ob.ToString(); set ViewStateClientID + SupportExtension = value.Replace(., ); public bool ShowImg get object ob = ViewStateClientID + ShowImg; if (ob = null) ob = true; return (bool)ob; set if (bool)value) dvImg.Styledisplay = block; else dvImg.Styledisplay = none; public string File

6、Name get return tbFileName.Text; protected string ParsValue = ; protected void Page_Load(object sender, EventArgs e) ParsValue = AutoFileName= + AutoFileName.ToString() + &SupportExtension= + SupportExtension + &UpLoadPath= + UpLoadPath + &ID= + ClientID; 第二步: 創(chuàng)建FileUpLoad.aspx 復(fù)制代碼 代碼如下: !DOCTYPE h

7、tml PUBLIC -/W3C/DTD XHTML 1.0 Transitional/EN /TR/xhtml1/ function upload(ob) var expStr=/.*()$/i; if(!expStr.test(ob.value) alert(上傳文件類型有誤。n(支持文件類型:); else var btUp=document.getElementById(); btUp.click(); 復(fù)制代碼 代碼如下:using System; using System.Collections; using System.Configuration; using Sy

8、stem.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Drawing; using System.IO; public partial class WebParts_FileUpLoad : System.Web.UI.Page protected str

9、ing SupportExtension = ; protected string UpLoadPath = ; protected bool AutoFileName; protected string ParentID = ; protected void Page_Load(object sender, EventArgs e) SupportExtension = Request.QueryStringSupportExtension; UpLoadPath = Request.QueryStringUpLoadPath; AutoFileName = bool.Parse(Reque

10、st.QueryStringAutoFileName.ToString(); ParentID = Request.QueryStringID.ToString(); protected void btUp_Click(object sender, EventArgs e) Boolean fileOK = false; if (FileUpload1.HasFile) String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); String allowedExtensions = Su

11、pportExtension.Split(new string | , StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i allowedExtensions.Length; i+) if (fileExtension = . + allowedExtensionsi) fileOK = true; if (fileOK) string path = ; string name = ; string sPath = ; if (AutoFileName) name = DateTime.Now.Ticks.ToString() +

12、 System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); else name = FileUpload1.FileName; sPath = Request.PhysicalApplicationPath + + UpLoadPath + ; path = sPath + name; /圖片地址 string fileName = name; /文件名 string fileName_s = s_ + name; /縮略圖文件名稱 try /FileInfo file = new FileInfo(name); / string

13、 fileContentType = FileUpload1.PostedFile.ContentType; /string name = FileUpload1.PostedFile.FileName;/客戶端文件路徑 /string fileName_sy = sy_ + file.Name; /水印圖文件名稱(文字) /string fileName_syp = syp_ + file.Name;/水印圖文件名稱(圖片) /string webFilePath = Server.MapPath(UpLoadPath + + fileName); /string webFilePath_s

14、 = Server.MapPath(UpLoadPath + + fileName_s); string webFilePath = sPath + fileName;/服務(wù)器端文件路徑 string webFilePath_s = sPath + fileName_s;/服務(wù)器端縮略圖路徑 if (!File.Exists(webFilePath) if (FileUpload1.PostedFile.ContentLength 800 | image.Height 600) MakeThumbnail(webFilePath, webFilePath_s, 800, 600, W);/生成

15、縮略圖的方法 else MakeThumbnail(webFilePath, webFilePath_s, 300, 230, W);/生成縮略圖的方法 image.Dispose(); / AddShuiYinWord(webFilePath, webFilePath_sy); /保存水印文字圖片 / AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);/保存添加水印圖片之后的圖片 /MakeThumbnail(webFilePath, webFilePath_s, 400, 300, W);/生成縮略圖的方法 Page

16、.RegisterClientScriptBlock(succcess, alert(上傳成功); parent.document.getElementById( + ParentID + _ + tbFileName + ).value= + fileName_s + ; ); catch (Exception ex) Page.RegisterClientScriptBlock(err, alert(提示:文件上傳失敗,失敗原因: + ex.Message + );); else Page.RegisterClientScriptBlock(err, alert(提示:圖片不能大于2M);

17、); else this.Page.RegisterClientScriptBlock(err, alert(圖片重復(fù));); catch (System.Exception err) this.Page.RegisterClientScriptBlock(err, alert( + err.Message + );); try Bitmap bmp = new Bitmap(path); int width = 0, height = 0; int dvalue = 200; if (bmp.Width dvalue & bmp.Height bmp.Height) width = dval

18、ue; height = dvalue * (bmp.Width / bmp.Height); else height = dvalue; width = dvalue * (bmp.Height / bmp.Width); this.Page.RegisterClientScriptBlock(show, parent.VarValue= + UpLoadPath + / + fileName_s + ; + parent. + ParentID + CallLoaded(); + .getElementById( + ParentID + _ + dvImg + ).innerHTML=

19、+ + ; ); bmp.Dispose(); catch finally /*判斷該文件是否存在,如果存在,則刪除圖片 if (File.Exists(path) /*刪除用戶客戶端上傳的圖片,服務(wù)器上只保存縮略之后的圖片 File.Delete(path); else this.Page.RegisterClientScriptBlock(err, alert(error);); / / 生成縮略圖 / / 源圖路徑(物理路徑) / 縮略圖路徑(物理路徑) / 縮略圖寬度 / 縮略圖高度 / 生成縮略圖的方式 public void MakeThumbnail(string origina

20、lImagePath, string thumbnailPath, int width, int height, string mode) System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) ca

21、se HW:/指定高寬縮放(可能變形) break; case W:/指定寬,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case H:/指定高,寬按比例 towidth = originalImage.Width * height / originalImage.Height; break; case Cut:/指定高寬裁減(不變形) if (double)originalImage.Width / (double)originalImage.Height (double)towidth

22、 / (double)toheight) oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; else ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; break; default: break; /新建一個(gè)bmp圖片 System.

23、Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); /新建一個(gè)畫板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); /設(shè)置高質(zhì)量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; /設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMo

24、de.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格式保存縮略圖 bitmap.Save(thumbnailPa

25、th, System.Drawing.Imaging.ImageFormat.Jpeg); catch (System.Exception e) throw e; finally originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); / / 在圖片上增加文字水印 / / 原服務(wù)器圖片路徑 / 生成的帶文字水印的圖片路徑 protected void AddShuiYinWord(string Path, string Path_sy) string addText = ; System.Drawing.Image image = 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.Soli

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論