C#实现的上传图片、保存图片、加水印、生成缩略图功能示例
本文实例讲述了C#实现的上传图片、保存图片、加水印、生成缩略图功能。分享给大家供大家参考,具体如下:
伴随移动设备地普及,处理图片、视频等需求也变得越来越基础,这里介绍的是图片的存储。
上传图片必须使用form表单提交的方式,我只知道这一种方法,如果大家知道其他方法的话请留言。
保存图片、加水印和生成缩略图这三种功能最好各自放在单独的方法中,尽量降低耦合度,提高代码复用程度,除此之外我们平常写代码是也要尽量做到方法功能的唯一性。
前台代码:
func: 用户Id: 头像: 水印:
后台代码:
privatestringUploadImage(HttpContextcontext) { try { System.IO.Streamstream=context.Request.Files["icon"].InputStream; //返回的图片路径可以存储在数据库中 stringimageUrl=SaveImage(stream,"Icon","蝈蝈"); stringthumbnailImageUrl=SaveThumbnailImage(stream,"Icon"); stringthumbnailImageUrlWithWatermark=SaveThumbnailImage(ConfigurationManager.AppSettings["AttachmentsDirectory"]+imageUrl,"Icon"); return"上传成功!"; } catch(Exceptionex) { return"上传失败!"; } } privatestringSaveImage(Streamstream,stringfolderName,stringwaterMark) { try { stringfileName=Guid.NewGuid()+".jpg"; stringpath=ConfigurationManager.AppSettings["AttachmentsDirectory"]; path=Path.Combine(path,folderName+"\\"+DateTime.Now.Year+"\\"+DateTime.Now.Month+"\\"+DateTime.Now.Day+"\\"); stringimageUrl="/"+folderName+"/"+DateTime.Now.Year+"/"+DateTime.Now.Month+"/"+DateTime.Now.Day+"/"; if(!string.IsNullOrEmpty(waterMark)) { ImageimgSource=Image.FromStream(stream); AddWatermarkAndSave(path,fileName,waterMark,imgSource,imgSource.Height-300,10,Color.Red, newFont("宋体",40)); } else { byte[]buffer=newbyte[stream.Length]; stream.Read(buffer,0,buffer.Length); if(!Directory.Exists(path)) { Directory.CreateDirectory(path); } System.IO.FileStreamfs=newSystem.IO.FileStream(path+fileName,System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); fs.Write(buffer,0,buffer.Length); fs.Flush(); fs.Close(); } returnimageUrl+fileName; } catch(Exceptionex) { return""; } } privatestringSaveThumbnailImage(Streamstream,stringfolderName) { try { stringfileName=Guid.NewGuid()+".jpg"; stringpath=ConfigurationManager.AppSettings["AttachmentsDirectory"]; path=Path.Combine(path,folderName+"\\"+DateTime.Now.Year+"\\"+DateTime.Now.Month+"\\"+DateTime.Now.Day+"\\"); stringimageUrl="/"+folderName+"/"+DateTime.Now.Year+"/"+DateTime.Now.Month+"/"+DateTime.Now.Day+"/"; System.Drawing.Image.GetThumbnailImageAbortmyCallback=newSystem.Drawing.Image.GetThumbnailImageAbort(GetFalse); //数据源来自Stream Imageimage=System.Drawing.Bitmap.FromStream(stream); System.Drawing.ImagethumbnailImage=image.GetThumbnailImage(64,64,myCallback,IntPtr.Zero); thumbnailImage.Save(path+fileName); thumbnailImage.Dispose(); returnimageUrl+fileName; } catch(Exceptionex) { return""; } } privatestringSaveThumbnailImage(stringoriginalFileName,stringfolderName) { try { stringfileName=Guid.NewGuid()+".jpg"; stringpath=ConfigurationManager.AppSettings["AttachmentsDirectory"]; path=Path.Combine(path,folderName+"\\"+DateTime.Now.Year+"\\"+DateTime.Now.Month+"\\"+DateTime.Now.Day+"\\"); stringimageUrl="/"+folderName+"/"+DateTime.Now.Year+"/"+DateTime.Now.Month+"/"+DateTime.Now.Day+"/"; System.Drawing.Image.GetThumbnailImageAbortmyCallback=newSystem.Drawing.Image.GetThumbnailImageAbort(GetFalse); //数据源来自File Imageimage=System.Drawing.Bitmap.FromFile(originalFileName); System.Drawing.ImagethumbnailImage=image.GetThumbnailImage(64,64,myCallback,IntPtr.Zero); thumbnailImage.Save(path+fileName); thumbnailImage.Dispose(); returnimageUrl+fileName; } catch(Exceptionex) { return""; } } privateboolGetFalse() { returnfalse; } //////图片加文字水印 /// ////// 水印文字,如果是多行用分号隔开 /// 图片 /// 上边距 /// 左边距 /// 文字颜色 /// 字体 /// 保存地址 /// privateboolAddWatermarkAndSave(stringpath,stringfileName,stringtext,Imageimg, intpaddingTop,intpaddingLeft,ColortextColor,FonttextFont) { text=text+";"+"当前时间:"+DateTime.Now.ToString("yyyy-MM-ddHH:mm"); if(!Directory.Exists(path)) { Directory.CreateDirectory(path); } textFont=newFont("宋体",19); Bitmapbm=newBitmap(img); System.Drawing.Graphicsg=System.Drawing.Graphics.FromImage(bm); System.Drawing.Brushb=newSolidBrush(textColor); string[]str=text.Split(';'); for(inti=0;i 更多关于C#相关内容感兴趣的读者可查看本站专题:《C#图片操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。