




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
北京联高软件开发有限公司倾情奉献经过测试的超强C#图片上传,加水印,自动生成缩略图源代码。可以指定水印位置、文字;可以设置缩略图背景颜色,边框及其颜色,阴影及其颜色;可以指定缩略图质量;支持GIF,JPEG,JPG,PNG,BMP,TIFF等多种格式图片文件。 protected void Page_Load(object sender, EventArgs e) if (IsPostBack) foreach (string f in Request.Files.AllKeys) / 查找是否存在指定的目,如果允许创建目录,则创建之。 string f_path = TextBox1.Text; if (!System.IO.Directory.Exists(Server.MapPath(f_path) if (CheckBox1.Checked) System.IO.Directory.CreateDirectory(Server.MapPath(f_path); else return; / 获取提交的文件名,包含的全部的目录信息;需要提取其文件名(名字后缀) HttpPostedFile file = Request.Filesf; string fname = file.FileName.ToLower(); fname = fname.Replace(, /); int lsub = fname.LastIndexOf(/); if (lsub = 0) fname = fname.Substring(lsub + 1, fname.Length - lsub - 1); string fa_ext = fname.Split(.); string f_pre = fa_extfa_ext.Length - 2; string f_ext = fa_extfa_ext.Length - 1; if (f_ext = gif | f_ext = jpg | f_ext = jpeg) file.SaveAs(Server.MapPath(f_path) + / + f_pre + . + f_ext); XImage img = new XImage(); / 添加水印 if (TextBox2.Text.Trim() != ) img.markPosition = DropDownList5.ItemsDropDownList5.SelectedIndex.Text; img.Mark(f_path + f_pre + . + f_ext, TextBox2.Text); / 如果需要生成所略图 if (CheckBox2.Checked) int tWidth = Int32.Parse(TextBox3.Text); int tHeight = Int32.Parse(TextBox4.Text); img.tBackground = img.ToColor(DropDownList1.ItemsDropDownList1.SelectedIndex.Text); img.tBorder = img.ToColor(DropDownList2.ItemsDropDownList2.SelectedIndex.Text); img.tShadow = img.ToColor(DropDownList3.ItemsDropDownList3.SelectedIndex.Text); img.tQuality = img.ToQuality(DropDownList4.ItemsDropDownList4.SelectedIndex.Text); img.ToThumbnail(f_path + f_pre + . + f_ext, f_path + f_pre + _thumb. + f_ext, tWidth, tHeight); C#图片上传及生成缩略图,添加水印的代码 保存的目录: /UploadedFiles/ 自动创建目录 添加水印字: 文字:50018.COM (可以为空,则不加水印) 位置: 左下角 左上角 右下角 右上角 图片中间 生成缩略图: 生成缩略图 宽度: 100 高度: 75 背景: 无色 白色 红色 蓝色 黄色 黑色 绿色 紫色 灰色 边框: 无色 白色 红色 蓝色 黄色 黑色 绿色 紫色 灰色 阴影: 无色 白色 红色 蓝色 黄色 黑色 绿色 紫色 灰色 质量: 100% 90% 80% 60% 50% 40% 请选择文件: 纯文本方式 拷贝到裁剪板 打印1. usingSystem; 2. usingSystem.Data; 3. usingSystem.Data.SqlClient; 4. usingSystem.Configuration; 5. usingSystem.Web; 6. usingSystem.Web.Security; 7. usingSystem.Web.UI; 8. usingSystem.Web.UI.WebControls; 9. usingSystem.Web.UI.WebControls.WebParts; 10. usingSystem.Web.UI.HtmlControls; 11. usingSystem.Text; 12. usingSystem.Text.RegularExpressions; 13. usingSystem.Drawing; 14. usingSystem.Drawing.Imaging; 15. usingSystem.Collections; 16. usingSystem.ComponentModel; 17. namespaceLegalsoft.Images 18. 19. / 20. /News的摘要说明 21. / 22. publicclassXImage 23. 24. publicColortBackground; 25. publicColortBorder; 26. publicColortShadow; 27. publicinttQuality; 28. publicstringmarkPosition; 29. / 30. /图片参数预定义 31. / 32. staticHashtablehtmimes=newHashtable(); 33. internalreadonlystringAllowExt=.jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp|.gif; 34. publicXImage() 35. 36. tBackground=Color.Transparent; 37. tBorder=Color.Transparent; 38. tShadow=Color.Transparent; 39. tQuality=100; 40. markPosition=左下角; 41. #region图片类型预定义 42. htmimes.jpe=image/jpeg; 43. htmimes.jpeg=image/jpeg; 44. htmimes.jpg=image/jpeg; 45. htmimes.png=image/png; 46. htmimes.tif=image/tiff; 47. htmimes.tiff=image/tiff; 48. htmimes.bmp=image/bmp; 49. htmimes.gif=image/gif; 50. #endregion 51. 52. #region下载指定URL图片并保存 53. / 54. /下载指定URL的文件并保存到指定目录 55. / 56. / 57. publicvoidDownloadImage(stringstrUrl,stringfile) 58. 59. System.Net.WebClientwc=newSystem.Net.WebClient(); 60. wc.DownloadFile(strUrl,file); 61. 62. #endregion 63. #regionC#自动生成缩略图 64. / 65. /按给定名字确定颜色 66. / 67. / 68. / 69. publicColorToColor(stringname) 70. 71. if(name=白色)returnColor.White; 72. if(name=红色)returnColor.Red; 73. if(name=蓝色)returnColor.Blue; 74. if(name=绿色)returnColor.Green; 75. if(name=黑色)returnColor.Black; 76. if(name=灰色)returnColor.DarkGray; 77. if(name=黄色)returnColor.Yellow; 78. if(name=紫色)returnColor.Cyan; 79. if(name=无色)returnColor.Transparent; 80. returnColor.Transparent; 81. 82. / 83. /按名字设置各种颜色,可以自行扩充:) 84. / 85. / 86. / 87. publicintToQuality(stringname) 88. 89. returnInt32.Parse(name.Replace(%,); 90. 91. / 92. /获取图像编码解码器的所有相关信息 93. / 94. /包含编码解码器的多用途网际邮件扩充协议(MIME)类型的字符串 95. /返回图像编码解码器的所有相关信息 96. privatestaticImageCodecInfoGetCodecInfo(stringmimeType) 97. 98. ImageCodecInfoCodecInfo=ImageCodecInfo.GetImageEncoders(); 99. foreach(ImageCodecInfoiciinCodecInfo) 100. 101. if(ici.MimeType=mimeType)returnici; 102. 103. returnnull; 104. 105. / 106. /检测扩展名的有效性 107. / 108. /文件名扩展名 109. /如果扩展名有效,返回true,否则返回false. 110. privateboolCheckValidExt(stringsExt) 111. 112. boolflag=false; 113. stringaExt=AllowExt.Split(|); 114. foreach(stringfiletypeinaExt) 115. 116. if(filetype.ToLower()=sExt) 117. 118. flag=true; 119. break; 120. 121. 122. returnflag; 123. 124. / 125. /保存图片 126. / 127. /Image对象 128. /保存路径 129. /指定格式的编解码参数 130. privatevoidSaveImage(System.Drawing.Imageimage,stringsavePath,ImageCodecInfoici) 131. 132. /设置原图片对象的EncoderParameters对象 133. EncoderParametersparameters=newEncoderParameters(1); 134. parameters.Param0=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,(long)tQuality); 135. image.Save(savePath,ici,parameters); 136. parameters.Dispose(); 137. 138. / 139. /生成缩略图 140. / 141. /原图片路径(相对路径) 142. /生成的缩略图路径,如果为空则保存为原图片路径(相对路径) 143. /缩略图的宽度(高度与按源图片比例自动生成) 144. publicvoidToThumbnail(stringsourceImagePath,stringthumbnailImagePath,intthumbnailImageWidth,intthumbnailImageHeight) 145. 146. /1.先检查图片格式等信息 147. stringThumbnailImagePath=thumbnailImagePath; 148. stringSourceImagePath=sourceImagePath; 149. stringsExt=SourceImagePath.Substring(SourceImagePath.LastIndexOf(.).ToLower(); 150. if(SourceImagePath.ToString()=System.String.Empty) 151. 152. thrownewNullReferenceException(SourceImagePathisnull!); 153. 154. if(!CheckValidExt(sExt) 155. 156. thrownewArgumentException(原图片文件格式不正确,支持的格式有+AllowExt+,SourceImagePath); 157. 158. /从原图片创建Image对象 159. System.Drawing.Imageimage=System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(SourceImagePath); 160. /2.计算图片的位置、尺寸等信息 161. inttWidth,tHeight,tLeft,tTop; 162. doublefScale=(double)thumbnailImageHeight/(double)thumbnailImageWidth;/高度宽度比 163. if(double)image.Width*fScale)(double)image.Height)/如果原图比较宽 164. 165. tWidth=thumbnailImageWidth; 166. tHeight=(int)(double)image.Height*(double)tWidth/(double)image.Width); 167. tLeft=0; 168. tTop=(thumbnailImageHeight-tHeight)/2; 169. 170. else171. 172. tHeight=thumbnailImageHeight; 173. tWidth=(int)(double)image.Width*(double)tHeight/(dou
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年办公软件操作中级水平考试模拟题及答案解析
- 2025年中学化学教师招聘考试重点知识点梳理与预测题解析
- 2025年中国邮政客户经理招聘考试模拟试题及备考指南
- 2025年铁基及铁镍基非晶合金项目发展计划
- 2025年柔性树脂版合作协议书
- 2025年频率测量仪表项目合作计划书
- 2025年港口业投资项目合作计划书
- 2025年超多道数字地震仪项目建议书
- 2025年自动包装设备项目建议书
- 跨境电商物流 题库试题及答案 任务五 跨境电商出口货物包装
- 法拍房介绍课件
- 器械gcp培训课件
- 《医院感染监测与控制》课程教学大纲(本科)
- 访问控制安全管理制度
- 【课件】数轴(课件)数学人教版2024七年级上册
- 乌镇景区管理制度
- 跨流域生态服务权衡-洞察及研究
- NEDD4在非小细胞肺癌EGFR-TKIs继发耐药中的作用机制与临床启示
- 公司意识形态管理制度
- 建筑行业技术总工岗位职责
- 《茶叶加工技术》课件
评论
0/150
提交评论