zoukankan
html css js c++ java
asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式
///
<summary>
///
生成缩略图
///
</summary>
///
<param name="originalImagePath">
源图路径(物理路径)
</param>
///
<param name="thumbnailPath">
缩略图路径(物理路径)
</param>
///
<param name="width">
缩略图宽度
</param>
///
<param name="height">
缩略图高度
</param>
///
<param name="mode">
生成缩略图的方式
</param>
public
static
void
MakeThumbnail(
string
originalImagePath,
string
thumbnailPath,
int
width,
int
height,
string
mode)
{
Image originalImage
=
Image.FromFile(originalImagePath);
int
towidth
=
width;
int
toheight
=
height;
int
x
=
0
;
int
y
=
0
;
int
ow
=
originalImage.Width;
int
oh
=
originalImage.Height;
switch
(mode)
{
case
"
HW
"
:
//
指定高宽缩放(可能变形)
break
;
case
"
W
"
:
//
指定宽,高按比例
toheight
=
originalImage.Height
*
width
/
originalImage.Width;
break
;
case
"
H
"
:
//
指定高,宽按比例
towidth
=
originalImage.Width
*
height
/
originalImage.Height;
break
;
case
"
Cut
"
:
//
指定高宽裁减(不变形)
if
((
double
)originalImage.Width
/
(
double
)originalImage.Height
>
(
double
)towidth
/
(
double
)toheight)
{
oh
=
originalImage.Height;
ow
=
originalImage.Height
*
towidth
/
toheight;
y
=
0
;
x
=
(originalImage.Width
-
ow)
/
2
;
}
else
{
ow
=
originalImage.Width;
oh
=
originalImage.Width
*
height
/
towidth;
x
=
0
;
y
=
(originalImage.Height
-
oh)
/
2
;
}
break
;
default
:
break
;
}
//
新建一个bmp图片
Image bitmap
=
new
System.Drawing.Bitmap(towidth,toheight);
//
新建一个画板
Graphics g
=
System.Drawing.Graphics.FromImage(bitmap);
//
设置高质量插值法
g.InterpolationMode
=
System.Drawing.Drawing2D.InterpolationMode.High;
//
设置高质量,低速度呈现平滑程度
g.SmoothingMode
=
System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//
清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//
在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage,
new
Rectangle(
0
,
0
, towidth, toheight),
new
Rectangle(x, y, ow,oh),
GraphicsUnit.Pixel);
try
{
//
以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch
(System.Exception e)
{
throw
e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
查看全文
相关阅读:
SVN——Jenkins自动发布
IIS之虚拟目录学习
SVN迁移
通过配置host,自定义域名让本地访问
比较两个时间的大小 举例:CompareDate("12:00","11:15")
[转]SQL Server 批量完整备份
js前台编码,asp.net后台解码 防止前台传值到后台为乱码
前端将图片二进制流显示在html端
【转】解析<button>和<input type="button"> 的区别
利用bat批处理——实现数据库的自动备份和删除
原文地址:https://www.cnblogs.com/yangbin1005/p/1260237.html
最新文章
undefined reference to...
qt多线程
qtcreator配置
buildroot使用
显示内核支持的文件系统
gdbserver 使用方法
iTOP-4412开发板p2p视频
iTOP-4412 nfs文件系统启动
make 2>&1 | tee build.log
iTOP-4412开发板使用
热门文章
python——定时闹钟讲解
Django——静态文件(如bootstrap)的配置
python——从datetime模块探索python的数据架构
python——实例方法、静态方法、类方法、类变量和实例变量浅析
python——父类与子类的一些说明
python——双下划线与python命名机制
国内外DNS服务器地址列表大全
Google Chrome 35 Released – Install on RHEL/CentOS 6 and Fedora 20-15
CentOS——yum命令运行错误解决办法
Linux(Centos)——下升级python3.3
Copyright © 2011-2022 走看看