zoukankan
html css js c++ java
一個圖片縮放類(按長寬比例縮放,自定義長寬縮放)
public
class
Thumbnail
{
/**/
///
<summary>
///
制作缩略图(如果原圖長大於寬以長為基准縮放,反之以寬為基准縮放)
///
</summary>
///
<param name="fileName">
原图路径
</param>
///
<param name="newFileName">
新图路径
</param>
///
<param name="maxWidth">
最大宽度
</param>
///
<param name="maxHeight">
最大高度
</param>
public
static
void
MakeThumbnailImage(
string
fileName,
string
newFileName,
int
maxWidth,
int
maxHeight)
{
Image original
=
Image.FromFile(fileName);
Size _newSize
=
ResizeImage(original.Width, original.Height, maxWidth, maxHeight);
//
_image.Height = _newSize.Height;
//
_image.Width = _newSize.Width;
Image displayImage
=
new
Bitmap(original, _newSize);
try
{
Image tempImage
=
displayImage;
original.Dispose();
tempImage.Save(newFileName, GetFormat(fileName));
}
finally
{
original.Dispose();
}
}
/**/
///
<summary>
///
计算新尺寸
///
</summary>
///
<param name="width">
原始宽度
</param>
///
<param name="height">
原始高度
</param>
///
<param name="maxWidth">
最大新宽度
</param>
///
<param name="maxHeight">
最大新高度
</param>
///
<returns></returns>
private
static
Size ResizeImage(
int
width,
int
height,
int
maxWidth,
int
maxHeight)
{
decimal
MAX_WIDTH
=
(
decimal
)maxWidth;
decimal
MAX_HEIGHT
=
(
decimal
)maxHeight;
decimal
ASPECT_RATIO
=
MAX_WIDTH
/
MAX_HEIGHT;
int
newWidth, newHeight;
decimal
originalWidth
=
(
decimal
)width;
decimal
originalHeight
=
(
decimal
)height;
if
(originalWidth
>
MAX_WIDTH
||
originalHeight
>
MAX_HEIGHT)
{
decimal
factor;
//
determine the largest factor
if
(originalWidth
/
originalHeight
>
ASPECT_RATIO)
{
factor
=
originalWidth
/
MAX_WIDTH;
newWidth
=
Convert.ToInt32(originalWidth
/
factor);
newHeight
=
Convert.ToInt32(originalHeight
/
factor);
}
else
{
factor
=
originalHeight
/
MAX_HEIGHT;
newWidth
=
Convert.ToInt32(originalWidth
/
factor);
newHeight
=
Convert.ToInt32(originalHeight
/
factor);
}
}
else
{
newWidth
=
width;
newHeight
=
height;
}
return
new
Size(newWidth, newHeight);
}
/**/
///
<summary>
///
保存图片
///
</summary>
///
<param name="image">
Image 对象
</param>
///
<param name="savePath">
保存路径
</param>
///
<param name="ici">
指定格式的编解码参数
</param>
private
static
void
SaveImage(Image image,
string
savePath, ImageCodecInfo ici)
{
//
设置 原图片 对象的 EncoderParameters 对象
EncoderParameters parameters
=
new
EncoderParameters(
1
);
parameters.Param[
0
]
=
new
EncoderParameter(Encoder.Quality, ((
long
)
100
));
image.Save(savePath, ici, parameters);
parameters.Dispose();
}
/**/
///
<summary>
///
获取图像编码解码器的所有相关信息
///
</summary>
///
<param name="mimeType">
包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串
</param>
///
<returns>
返回图像编码解码器的所有相关信息
</returns>
private
static
ImageCodecInfo GetCodecInfo(
string
mimeType)
{
ImageCodecInfo[] CodecInfo
=
ImageCodecInfo.GetImageEncoders();
foreach
(ImageCodecInfo ici
in
CodecInfo)
{
if
(ici.MimeType
==
mimeType)
return
ici;
}
return
null
;
}
/**/
///
<summary>
///
得到图片格式
///
</summary>
///
<param name="name">
文件名称
</param>
///
<returns></returns>
public
static
ImageFormat GetFormat(
string
name)
{
string
ext
=
name.Substring(name.LastIndexOf(
"
.
"
)
+
1
);
switch
(ext.ToLower())
{
case
"
jpg
"
:
case
"
jpeg
"
:
return
ImageFormat.Jpeg;
case
"
bmp
"
:
return
ImageFormat.Bmp;
case
"
png
"
:
return
ImageFormat.Png;
case
"
gif
"
:
return
ImageFormat.Gif;
default
:
return
ImageFormat.Jpeg;
}
}
/**/
///
<summary>
///
自定義縮放圖(縮略圖時以圖頂端為起點)
///
</summary>
///
<param name="fileName"></param>
///
<param name="newFileName"></param>
///
<param name="newWidth"></param>
///
<param name="newHeight"></param>
public
static
void
MakeCustomImage(
string
fileName,
string
newFileName,
int
newWidth,
int
newHeight)
{
Image image
=
Image.FromFile(fileName);
int
i
=
0
;
int
width
=
image.Width;
int
height
=
image.Height;
if
(width
>
height)
{
i
=
height;
}
else
{
i
=
width;
}
Bitmap b
=
new
Bitmap(newWidth, newHeight);
try
{
Graphics g
=
Graphics.FromImage(b);
g.InterpolationMode
=
InterpolationMode.High;
g.SmoothingMode
=
SmoothingMode.HighQuality;
//
清除整个绘图面并以透明背景色填充
g.Clear(Color.Transparent);
if
(width
<
height)
{
g.DrawImage(image,
new
Rectangle(
0
,
0
, newWidth, newHeight),
new
Rectangle((width
-
newWidth)
/
2
,
0
, newWidth,newHeight), GraphicsUnit.Pixel);
}
else
{
g.DrawImage(image,
new
Rectangle(
0
,
0
, newWidth, newHeight),
new
Rectangle((width
-
newWidth)
/
2
,
0
, newWidth, newHeight), GraphicsUnit.Pixel);
}
image.Dispose();
SaveImage(b, newFileName, GetCodecInfo(
"
image/
"
+
GetFormat(fileName).ToString().ToLower()));
}
finally
{
image.Dispose();
b.Dispose();
}
}
}
查看全文
相关阅读:
JSON格式
多行写入
文件对象write() and read()
一个虚拟摄像头Filter(Virtual Cam Capture Filter)
五十种最好的开源爬虫
web scraper 里的 Element click 模拟点击「加载更多」
介绍一款好用又易学的爬虫工具:web scraper
安装宝塔面板后 ,centos系统 挂载硬盘 或者 数据盘和系统盘合并
帝国CMS恢复搜索功能 增加搜索数据源设置教程
安装帝国CMS步骤 和恢复数据
原文地址:https://www.cnblogs.com/wang123/p/1200060.html
最新文章
java内部类的继承与覆写
java中代码块,方法与构造器的执行顺序。
SqlServer2005 查询 第一讲 计算列
cmd 统计文件个数
torch.eye
与服务器交互 【转载】 夕小瑶https://www.jiqizhixin.com/articles/2018-07-02-15
pythrch 启动 visdom可视化
nltk RegexpTokenizer类:python自然语言处理
TypeError: a bytes-like object is required, not 'str'
pytorch中squeeze()和unsqueeze()函数介绍
热门文章
linux查看cuda版本和cudnn版本的命令
python读取yaml配置文件
python中的easydict模块使用
简单组件使用案例
三酷猫自建文件数据库
用DOM实现对XML文件的解析
生成XML文件的
xml技术
SAX解析XML文件
读写JSON文件
Copyright © 2011-2022 走看看