这篇文章主要介绍了C#怎么实现的上传图片、保存图片、加水印、生成缩略图功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联公司是一家专业提供安平企业网站建设,专注与网站制作、做网站、H5页面制作、小程序制作等业务。10年已为安平众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
具体如下:
伴随移动设备地普及,处理图片、视频等需求也变得越来越基础,这里介绍的是图片的存储。
上传图片必须使用form表单提交的方式,我只知道这一种方法,如果大家知道其他方法的话请留言。
保存图片、加水印和生成缩略图这三种功能最好各自放在单独的方法中,尽量降低耦合度,提高代码复用程度,除此之外我们平常写代码是也要尽量做到方法功能的唯一性。
前台代码:
<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>用户Id:</td> <td><input type="text" name="userId"/></td> </tr> <tr> <td>头像:</td> <td><input type="file" name="icon"/></td> </tr> <tr> <td>水印:</td> <td><input type="text" name="waterMark"/></td> </tr> </table> <input type="submit" value="提交"/> </form>
后台代码:
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 + "\\" + 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); //数据源来自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); //数据源来自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; }
C#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的首选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。
感谢你能够认真阅读完这篇文章,希望小编分享的“C#怎么实现的上传图片、保存图片、加水印、生成缩略图功能”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
网站题目:C#怎么实现的上传图片、保存图片、加水印、生成缩略图功能
本文URL:https://www.cdcxhl.com/article18/ghdgdp.html
成都网站建设公司_创新互联,为您提供微信公众号、动态网站、标签优化、全网营销推广、用户体验、
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联