当前位置: 首页 > news >正文

C#生成缩略图

/// <summary> /// 获取等比例缩放图片的方法 /// </summary> /// <param name="imgPath">待缩放及保存图片的路径</param> /// <param name="format">缩放图片保存的格式</param> /// <param name="scaling">要保持的宽度或高度</param> /// <param name="keepWidthOrHeight">如果为true则保持宽度为scaling,否则保持高度为scaling</param> /// <returns></returns> public bool GetThumbnail(string imgPath, System.Drawing.Imaging.ImageFormat format, int scaling, bool keepWidthOrHeight) { File.Copy(imgPath, imgPath + ".bak"); try { using (System.Drawing.Bitmap myBitmap = new System.Drawing.Bitmap(imgPath)) { int width = 0; int height = 0; int tw = myBitmap.Width;//图像的实际宽度 int th = myBitmap.Height;//图像的实际高度 if (keepWidthOrHeight)//保持宽度 { #region 自动保持宽度 if (scaling >= tw) { width = tw; height = th; } else { double ti = Convert.ToDouble(tw) / Convert.ToDouble(scaling); if (ti == 0d) { width = tw; height = th; } else { width = scaling; height = Convert.ToInt32(Convert.ToDouble(th) / ti); } } #endregion } else { #region 自动保持高度 if (scaling >= th) { width = tw; height = th; } else { double ti = Convert.ToDouble(th) / Convert.ToDouble(scaling); if (ti == 0d) { width = tw; height = th; } else { width = Convert.ToInt32(Convert.ToDouble(tw) / ti); height = scaling; } } #endregion } using (System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(width, height, null, IntPtr.Zero)) { myBitmap.Dispose(); File.Delete(imgPath); myThumbnail.Save(imgPath); } } File.Delete(imgPath + ".bak"); return true; } catch { return false; } }

相关文章:

  • 假期的充电
  • 关于最近
  • 做网页很实用代码集合和CSS制作网页小技巧整理
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • Div之滚动条
  • PAC (proxy auto-config) 自动代理
  • DB2 导出数据(CSV格式)
  • WinXP下搭建virtualbox+ubuntu10.04+LAMP+tomcat6环境小记
  • WCF开发之消息契约(MessageContract)
  • nebula
  • WinXP下VirtualBox虚拟Ubuntu系统文件夹共享
  • About me
  • h264解码时的AVCDecoderConfigurationRecord 与 CodecPrivateData
  • PHP导出数据到淘宝助手CSV的方法分享
  • 公司最近招招.net程序员
  • JavaScript-如何实现克隆(clone)函数
  • 【笔记】你不知道的JS读书笔记——Promise
  • android图片蒙层
  • C# 免费离线人脸识别 2.0 Demo
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • Cumulo 的 ClojureScript 模块已经成型
  • JAVA 学习IO流
  • KMP算法及优化
  • October CMS - 快速入门 9 Images And Galleries
  • oschina
  • sessionStorage和localStorage
  • SwizzleMethod 黑魔法
  • Vue ES6 Jade Scss Webpack Gulp
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • 基于遗传算法的优化问题求解
  • 今年的LC3大会没了?
  • 聊聊redis的数据结构的应用
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 日剧·日综资源集合(建议收藏)
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 微信开放平台全网发布【失败】的几点排查方法
  • 小程序button引导用户授权
  • 选择阿里云数据库HBase版十大理由
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (1)常见O(n^2)排序算法解析
  • (C++17) std算法之执行策略 execution
  • (HAL库版)freeRTOS移植STMF103
  • (java)关于Thread的挂起和恢复
  • (Matlab)使用竞争神经网络实现数据聚类
  • (Note)C++中的继承方式
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (二)学习JVM —— 垃圾回收机制
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • (转)Sql Server 保留几位小数的两种做法
  • .NET 设计模式初探
  • @Import注解详解
  • [20180129]bash显示path环境变量.txt