您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#怎么實(shí)現(xiàn)的上傳圖片、保存圖片、加水印、生成縮略圖功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
伴隨移動(dòng)設(shè)備地普及,處理圖片、視頻等需求也變得越來(lái)越基礎(chǔ),這里介紹的是圖片的存儲(chǔ)。
上傳圖片必須使用form表單提交的方式,我只知道這一種方法,如果大家知道其他方法的話請(qǐng)留言。
保存圖片、加水印和生成縮略圖這三種功能最好各自放在單獨(dú)的方法中,盡量降低耦合度,提高代碼復(fù)用程度,除此之外我們平常寫(xiě)代碼是也要盡量做到方法功能的唯一性。
前臺(tái)代碼:
<form method="POST" enctype="multipart/form-data" action="UploadImg.ashx"> <table> <tr> <td>func:</td> <td><input type="text" name="func"/></td> </tr> <tr> <td>用戶(hù)Id:</td> <td><input type="text" name="userId"/></td> </tr> <tr> <td>頭像:</td> <td><input type="file" name="icon"/></td> </tr> <tr> <td>水?。?lt;/td> <td><input type="text" name="waterMark"/></td> </tr> </table> <input type="submit" value="提交"/> </form>
后臺(tái)代碼:
private string UploadImage(HttpContext context) { try { System.IO.Stream stream = context.Request.Files["icon"].InputStream; //返回的圖片路徑可以存儲(chǔ)在數(shù)據(jù)庫(kù)中 string imageUrl = SaveImage(stream, "Icon", "蟈蟈"); string thumbnailImageUrl = SaveThumbnailImage(stream, "Icon"); string thumbnailImageUrlWithWatermark = SaveThumbnailImage(ConfigurationManager.AppSettings["AttachmentsDirectory"] + imageUrl, "Icon"); return "上傳成功!"; } catch (Exception ex) { return "上傳失??!"; } } private string SaveImage(Stream stream, string folderName, string waterMark) { try { string fileName = Guid.NewGuid() + ".jpg"; string path = ConfigurationManager.AppSettings["AttachmentsDirectory"]; path = Path.Combine(path, folderName + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\"); string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; if (!string.IsNullOrEmpty(waterMark)) { Image imgSource = Image.FromStream(stream); AddWatermarkAndSave(path, fileName, waterMark, imgSource, imgSource.Height - 300, 10, Color.Red, new Font("宋體", 40)); } else { byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } System.IO.FileStream fs = new System.IO.FileStream(path + fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); fs.Write(buffer, 0, buffer.Length); fs.Flush(); fs.Close(); } return imageUrl + fileName; } catch (Exception ex) { return ""; } } private string SaveThumbnailImage(Stream stream, string folderName) { try { string fileName = Guid.NewGuid() + ".jpg"; string path = ConfigurationManager.AppSettings["AttachmentsDirectory"]; path = Path.Combine(path, folderName + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\"); string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(GetFalse); //數(shù)據(jù)源來(lái)自Stream Image image = System.Drawing.Bitmap.FromStream(stream); System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); thumbnailImage.Save(path + fileName); thumbnailImage.Dispose(); return imageUrl + fileName; } catch (Exception ex) { return ""; } } private string SaveThumbnailImage(string originalFileName, string folderName) { try { string fileName = Guid.NewGuid() + ".jpg"; string path = ConfigurationManager.AppSettings["AttachmentsDirectory"]; path = Path.Combine(path, folderName + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\"); string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(GetFalse); //數(shù)據(jù)源來(lái)自File Image image = System.Drawing.Bitmap.FromFile(originalFileName); System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, myCallback, IntPtr.Zero); thumbnailImage.Save(path + fileName); thumbnailImage.Dispose(); return imageUrl + fileName; } catch (Exception ex) { return ""; } } private bool GetFalse() { return false; } /// <summary> /// 圖片加文字水印 /// </summary> /// <param name="fileName"> </param> /// <param name="text">水印文字,如果是多行用分號(hào)隔開(kāi)</param> /// <param name="img">圖片</param> /// <param name="paddingTop">上邊距</param> /// <param name="paddingLeft">左邊距</param> /// <param name="textColor">文字顏色</param> /// <param name="textFont">字體</param> /// <param name="path">保存地址</param> /// <returns></returns> private bool AddWatermarkAndSave(string path, string fileName, string text, Image img, int paddingTop, int paddingLeft, Color textColor, Font textFont) { text = text + ";" + "當(dāng)前時(shí)間:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } textFont = new Font("宋體", 19); Bitmap bm = new Bitmap(img); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); System.Drawing.Brush b = new SolidBrush(textColor); string[] str = text.Split(';'); for (int i = 0; i < str.Length; i++) g.DrawString(str[i], textFont, b, paddingLeft, paddingTop + 33 * i); g.Dispose(); bm.Save(path + fileName, ImageFormat.Jpeg); bm.Dispose(); return true; }
C#是一個(gè)簡(jiǎn)單、通用、面向?qū)ο蟮木幊陶Z(yǔ)言,它由微軟Microsoft開(kāi)發(fā),繼承了C和C++強(qiáng)大功能,并且去掉了一些它們的復(fù)雜特性,C#綜合了VB簡(jiǎn)單的可視化操作和C++的高運(yùn)行效率,以其強(qiáng)大的操作能力、優(yōu)雅的語(yǔ)法風(fēng)格、創(chuàng)新的語(yǔ)言特性和便捷的面向組件編程從而成為.NET開(kāi)發(fā)的首選語(yǔ)言,但它不適用于編寫(xiě)時(shí)間急迫或性能非常高的代碼,因?yàn)镃#缺乏性能極高的應(yīng)用程序所需要的關(guān)鍵功能。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#怎么實(shí)現(xiàn)的上傳圖片、保存圖片、加水印、生成縮略圖功能”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。