/// <summary>
/// 生成图片缩率图Action
/// </summary>
/// <param name="p">原图url</param>
/// <param name="id">图片尺寸以及生成缩率图的类型</param>
/// <returns></returns>
[ValidateInput(
false
)]
public
ActionResult Index(
string
p,
string
id)
{
if
(
string
.IsNullOrEmpty(p))
{
return
new
HttpStatusCodeResult(404);
}
string
oPath = Regex.Replace(p,
@"http[s]?://(.*?)/"
,
"/"
, RegexOptions.IgnoreCase);
int
? oWidth = 200, oHeight = 200;
int
cutMode = 3;
string
pPath;
string
oDir;
if
(!
string
.IsNullOrEmpty(id))
{
string
[] ss = id.Split(
new
char
[] {
'_'
}, StringSplitOptions.RemoveEmptyEntries);
if
(ss.Length < 2)
{
return
new
HttpStatusCodeResult(404);
}
if
(ss.Length > 2)
{
cutMode =
int
.Parse(ss[2]);
}
oPath = oPath.Insert(oPath.LastIndexOf(
'/'
) + 1,
string
.Format(
"{0}_{1}_{2}_"
, ss[0], ss[1], cutMode));
oWidth =
int
.Parse(ss[0]);
oHeight =
int
.Parse(ss[1]);
}
pPath = Server.MapPath(oPath);
oDir = Path.GetDirectoryName(pPath);
if
(!System.IO.File.Exists(pPath))
{
byte
[] imagebytes = FileHelper.DownLoadFile(p);
if
(!Directory.Exists(oDir))
{
Directory.CreateDirectory(oDir);
}
FileHelper.MakeThumbnail(FileHelper.BytToImg(imagebytes), oWidth.Value, oHeight.Value, (ThumbnailMode)cutMode, pPath,
true
);
}
return
File(pPath, FileHelper.GetContentTypeByExtension(Path.GetExtension(pPath).ToLower()));
}
public
class
FileHelper
{
/// <summary>
/// 图片后缀和ContentType对应字典
/// </summary>
static
Dictionary<
string
,
string
> extensionContentTypeDic;
static
FileHelper()
{
if
(extensionContentTypeDic ==
null
)
{
//.jpg", ".png", ".gif", ".jpeg
extensionContentTypeDic =
new
Dictionary<
string
,
string
>();
extensionContentTypeDic.Add(
".jpg"
,
"image/jpeg"
);
extensionContentTypeDic.Add(
".png"
,
"image/png"
);
extensionContentTypeDic.Add(
".gif"
,
"image/gif"
);
extensionContentTypeDic.Add(
".jpeg"
,
"image/jpeg"
);
}
}
/// <summary>
/// 根据后缀名获取extension
/// </summary>
/// <param name="extension"></param>
/// <returns></returns>
public
static
string
GetContentTypeByExtension(
string
extension)
{
if
(extensionContentTypeDic.ContainsKey(extension))
{
return
extensionContentTypeDic[extension];
}
return
null
;
}
/// <summary >
/// 将Image对象转化成二进制流
/// </summary >
/// <param name="image" > </param >
/// <returns > </returns >
public
static
byte
[] ImageToByteArray(Image image)
{
MemoryStream imageStream =
new
MemoryStream();
Bitmap bmp =
new
Bitmap(image.Width, image.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(image,
new
System.Drawing.Rectangle(0, 0, image.Width, image.Height));
try
{
bmp.Save(imageStream, image.RawFormat);
}
catch
(Exception e)
{
bmp.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
byte
[] byteImg = imageStream.GetBuffer();
bmp.Dispose();
g.Dispose();
imageStream.Close();
return
byteImg;
}
/// <summary>
/// 字节流转换成图片
/// </summary>
/// <param name="byt">要转换的字节流</param>
/// <returns>转换得到的Image对象</returns>
public
static
Image BytToImg(
byte
[] byt)
{
MemoryStream ms =
new
MemoryStream(byt);
Image img = Image.FromStream(ms);
ms.Close();
return
img;
}
/// <summary>
/// 生成缩率图
/// </summary>
/// <param name="originalImage">原始图片Image</param>
/// <param name="width">缩率图宽</param>
/// <param name="height">缩率图高</param>
/// <param name="mode">生成缩率图的方式</param>
/// <param name="thumbnailPath">缩率图存放的地址</param>
public
static
Image MakeThumbnail(Image originalImage,
int
width,
int
height, ThumbnailMode mode,
string
thumbnailPath,
bool
isSave =
true
)
{
int
towidth = width;
int
toheight = height;
int
x = 0;
int
y = 0;
int
ow = originalImage.Width;
int
oh = originalImage.Height;
switch
(mode)
{
case
ThumbnailMode.HW:
//指定高宽缩放(可能变形)
break
;
case
ThumbnailMode.W:
//指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break
;
case
ThumbnailMode.H:
//指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break
;
case
ThumbnailMode.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图片
System.Drawing.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);
if
(!isSave)
{
return
bitmap;
}
try
{
//以jpg格式保存缩略图
//bitmap.Save(thumbnailPath, bitmap.RawFormat);
bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
return
bitmap;
}
catch
(System.Exception e)
{
throw
e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
return
null
;
}
/// <summary>
/// 下载指定文件
/// </summary>
/// <param name="remoteUrl"></param>
/// <param name="ss"></param>
public
static
byte
[] DownLoadFile(
string
remoteUrl)
{
WebClient wc =
new
WebClient();
try
{
return
wc.DownloadData(remoteUrl);
}
catch
(Exception e)
{
throw
new
Exception(
"下载文件失败"
);
}
}
}
public
enum
ThumbnailMode
{
/// <summary>
/// 指定高宽缩放(可能变形)
/// </summary>
HW,
/// <summary>
/// 指定高,宽按比例
/// </summary>
H,
/// <summary>
/// 指定宽,高按比例
/// </summary>
W,
/// <summary>
/// 指定高宽裁减(不变形)
/// </summary>
Cut,
}
访问方式:
http://www.souji8.com/Home/Index/{width}_{height}_{ThumMode}?p={imageUrl}
{imageUrl}:目标图片地址
{ThumMode}: 1:指定高宽按比例、2:指定宽,高按比例、3:指定高宽裁减(不变形)
{Width}:期望图片宽
{Height}:期望图片高