国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#上傳并保存圖片、加水印、生成縮略圖

2019-11-11 06:20:02
字體:
來源:轉載
供稿:網友

伴隨移動設備地普及,處理圖片、視頻等需求也變得越來越基礎,這里介紹的是圖片的存儲。 上傳圖片必須使用form表單提交的方式,我只知道這一種方法,如果大家知道其他方法的話請留言。 保存圖片、加水印和生成縮略圖這三種功能最好各自放在單獨的方法中,盡量降低耦合度,提高代碼復用程度,除此之外我們平常寫代碼是也要盡量做到方法功能的唯一性。

前臺代碼:

<form method="POST" enctype="mult后臺代碼:

PRivate string UploadImage(HttpContext context){ try { System.IO.Stream stream = context.Request.Files["icon"].InputStream; //返回的圖片路徑可以存儲在數據庫中 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 + "http://" + DateTime.Now.Year + "http://" + DateTime.Now.Month + "http://" + DateTime.Now.Day + "http://"); 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 + "http://" + DateTime.Now.Year + "http://" + DateTime.Now.Month + "http://" + DateTime.Now.Day + "http://"); string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(GetFalse); //數據源來自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 + "http://" + DateTime.Now.Year + "http://" + DateTime.Now.Month + "http://" + DateTime.Now.Day + "http://"); string imageUrl = "/" + folderName + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/"; System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(GetFalse); //數據源來自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">水印文字,如果是多行用分號隔開</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 + ";" + "當前時間:" + 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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 偃师市| 额尔古纳市| 英超| 随州市| 城固县| 万荣县| 育儿| 平原县| 讷河市| 天峨县| 徐闻县| 碌曲县| 青海省| 宽城| 合山市| 灵宝市| 波密县| 崇义县| 綦江县| 灵台县| 南郑县| 福安市| 屏东市| 南安市| 潍坊市| 高要市| 阳高县| 密云县| 罗江县| 遵义县| 新疆| 交城县| 南乐县| 峡江县| 绵阳市| 加查县| 长葛市| 夏河县| 招远市| 山阳县| 新巴尔虎右旗|