代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
生成缩略图
作者:
Dezai.CN
/ 发布于
2011/7/8
/
782
<div><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 生成缩略图 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="originalImagePath"></span><span style="color: rgb(0,128,0)">源图路径(物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="thumbnailPath"></span><span style="color: rgb(0,128,0)">缩略图路径(物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="width"></span><span style="color: rgb(0,128,0)">缩略图宽度</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="height"></span><span style="color: rgb(0,128,0)">缩略图高度</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="mode"></span><span style="color: rgb(0,128,0)">生成缩略图的方式</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">void</span><span style="color: rgb(0,0,0)"> MakeThumbnail(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> originalImagePath, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> thumbnailPath, </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> width, </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> height, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> mode) { Image originalImage </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> Image.FromFile(originalImagePath); </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> towidth </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> width; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> toheight </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> height; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> x </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> y </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> ow </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> originalImage.Width; </span><span style="color: rgb(0,0,255)">int</span><span style="color: rgb(0,0,0)"> oh </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> originalImage.Height; </span><span style="color: rgb(0,0,255)">switch</span><span style="color: rgb(0,0,0)"> (mode) { </span><span style="color: rgb(0,0,255)">case</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">HW</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">指定高宽缩放(可能变形) </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">break</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">case</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">W</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">指定宽,高按比例 </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> toheight </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> originalImage.Height </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> width </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> originalImage.Width; </span><span style="color: rgb(0,0,255)">break</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">case</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">H</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">指定高,宽按比例</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> towidth </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> originalImage.Width </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> height </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> originalImage.Height; </span><span style="color: rgb(0,0,255)">break</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">case</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(128,0,0)">Cut</span><span style="color: rgb(128,0,0)">"</span><span style="color: rgb(0,0,0)">:</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">指定高宽裁减(不变形) </span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> ((</span><span style="color: rgb(0,0,255)">double</span><span style="color: rgb(0,0,0)">)originalImage.Width </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">double</span><span style="color: rgb(0,0,0)">)originalImage.Height </span><span style="color: rgb(0,0,0)">></span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">double</span><span style="color: rgb(0,0,0)">)towidth </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,255)">double</span><span style="color: rgb(0,0,0)">)toheight) { oh </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> originalImage.Height; ow </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> originalImage.Height </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> towidth </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> toheight; y </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; x </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (originalImage.Width </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> ow) </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">else</span><span style="color: rgb(0,0,0)"> { ow </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> originalImage.Width; oh </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> originalImage.Width </span><span style="color: rgb(0,0,0)">*</span><span style="color: rgb(0,0,0)"> height </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> towidth; x </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">; y </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> (originalImage.Height </span><span style="color: rgb(0,0,0)">-</span><span style="color: rgb(0,0,0)"> oh) </span><span style="color: rgb(0,0,0)">/</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">2</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">break</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">default</span><span style="color: rgb(0,0,0)">: </span><span style="color: rgb(0,0,255)">break</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">新建一个bmp图片</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> Image bitmap </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> System.Drawing.Bitmap(towidth, toheight); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">新建一个画板</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> Graphics g </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> System.Drawing.Graphics.FromImage(bitmap); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">设置高质量插值法</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> g.InterpolationMode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> System.Drawing.Drawing2D.InterpolationMode.High; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">设置高质量,低速度呈现平滑程度</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> g.SmoothingMode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> System.Drawing.Drawing2D.SmoothingMode.HighQuality; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">清空画布并以透明背景色填充</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> g.Clear(Color.Transparent); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">在指定位置并且按指定大小绘制原图片的指定部分</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> g.DrawImage(originalImage, </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Rectangle(</span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">, towidth, toheight), </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">以jpg格式保存缩略图</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (System.Exception e) { </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> e; } </span><span style="color: rgb(0,0,255)">finally</span><span style="color: rgb(0,0,0)"> { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }</span></div>
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3