C#批量生成缩略图(原创)
批量生成缩略图
.net实现批量生成多种格式缩略图,自动根据宽高做判断,固定宽或高,高或宽按比例生成
1.实现代码:
1.1.调用方法:
http://localhost/Default.aspx?year=2010&month=8&day=8
1.2.web.config配置
<httpRuntime executionTimeout="9000" maxRequestLength="2000000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="819200"/>
1.3.default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//调用方法:http://localhost/Default.aspx?year=2010&month=8&day=8
string year = HttpContext.Current.Request.QueryString["year"];
string month = HttpContext.Current.Request.QueryString["month"];
string day = HttpContext.Current.Request.QueryString["day"];
OpratePic(year, month, day);
}
}
protected void OpratePic(string year, string month, string day)
{
string treePath = HttpContext.Current.Server.MapPath("") + "\\pic\\";
string otherPath = HttpContext.Current.Server.MapPath("")+ "\\slt\\";
if (!string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month) && !string.IsNullOrEmpty(day))
{
string treePath1 = treePath + year.ToString() + "\\" + month.ToString() + "\\" + day.ToString() + "\\";
string otherPath1 = otherPath + year.ToString() + "\\" + month.ToString() + "\\" + day.ToString() + "\\";
GeneratePic(treePath1, otherPath1);
Response.Write("生成成功<br/>");
}
else if (!string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month))
{
treePath = treePath + year.ToString() + "\\" + month.ToString() + "\\";
otherPath = otherPath + year.ToString() + "\\" + month.ToString() + "\\";
for (int c = 1; c < 32; c++)
{
string treePath1 = treePath + c.ToString() + "\\";
string otherPath1 = otherPath + c.ToString() + "\\";
GeneratePic(treePath1, otherPath1);
}
Response.Write("生成成功<br/>");
}
else if (!string.IsNullOrEmpty(year))
{
treePath = treePath + year.ToString() + "\\";
otherPath = otherPath + year.ToString() + "\\";
for (int b = 1; b < 13; b++)
{
for (int c = 1; c < 32; c++)
{
string treePath1 = treePath + "\\" + b.ToString() + "\\" + c.ToString() + "\\";
string otherPath1 = otherPath + "\\" + b.ToString() + "\\" + c.ToString() + "\\";
GeneratePic(treePath1, otherPath1);
}
}
Response.Write("生成成功<br/>");
}
else
{
Response.Write("输入参数有误!<br/>");
Response.Write("缩略图格式如下:<br/>");
Response.Write("http://photo.jmw.com.cn/default.aspx?year=2008&month=12&day=20<br/>");
}
}
protected void GeneratePic(string treePath, string otherPath)
{
if (Directory.Exists(treePath))
{
string[] stringFiles = Directory.GetFiles(treePath);
foreach (string stringFile in stringFiles)
{
FileInfo file = new FileInfo(stringFile);
if (file.Length > 0)
{
//(||)jpgbmpgif
if (Regex.IsMatch(file.Name, @".*\.(?:jpg|gif|jpeg|bmp|png)$", RegexOptions.IgnoreCase))
{
string webFilePath = treePath + file.Name; // 服务器端文件路径
string webFilePath_s1 = otherPath + "S1_" + file.Name; // 服务器端缩略图路径
string webFilePath_s2 = otherPath + "S2_" + file.Name;
string webFilePath_s3 = otherPath + "S3_" + file.Name;
string webFilePath_s11 = otherPath + "S1_1_" + file.Name;
string webFilePath_s22 = otherPath + "S2_2_" + file.Name;
string webFilePath_s33 = otherPath + "S3_3_" + file.Name;
if (!Directory.Exists(otherPath)) //如果目录不存在则创建
{
Directory.CreateDirectory(otherPath);
}
MakeThumbnail(webFilePath, webFilePath_s22, 250, 188, "W"); // 生成缩略图方法W:指定宽,高按比例
MakeThumbnail(webFilePath_s22, webFilePath_s2, 250, 188, "Cut");
MakeThumbnail(webFilePath, webFilePath_s11, 152, 114, "W"); // 生成缩略图方法W:指定宽,高按比例
MakeThumbnail(webFilePath_s11, webFilePath_s1, 152, 114, "Cut");
MakeThumbnail(webFilePath, webFilePath_s33, 38, 29, "W"); // 生成缩略图方法W:指定宽,高按比例
MakeThumbnail(webFilePath_s33, webFilePath_s3, 38, 29, "Cut");
}
}
}
}
}
#region 生成缩略图
/// 〈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)
{
try
{
using (System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath))
{
if (mode != "Cut")
{
double d1 = double.Parse(width.ToString()) / double.Parse(height.ToString());
//宽高比
double wd = double.Parse(originalImage.Width.ToString()) / double.Parse(originalImage.Height.ToString());
//高宽比
double hd = double.Parse(originalImage.Height.ToString()) / double.Parse(originalImage.Width.ToString());
double itsw = 0d;
double itsh = 0d;
int itsin = 0;
//与宽高比对比
if (d1 > wd)
{
itsw = d1 - wd;
}
else
{
itsw = wd - d1;
}
//与高宽比对比
if (d1 > hd)
{
itsh = d1 - hd;
}
else
{
itsh = hd - d1;
}
//如果高宽比更接近比例
if (itsw > itsh)
{
mode = "W";
}
else
{
mode = "H";
}
}
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图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
HttpContext.Current.Response.Write(e.ToString() + thumbnailPath);
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
catch
{
}
}
#endregion
}
2.相关方法
//根据url获取文件名
public static string GetUrlFileName(string url)
{
if (string.IsNullOrEmpty(url))
{
return "";
}
string[] strs1 = url.Split(new char[] { '/' });
return strs1[strs1.Length - 1].Split(new char[] { '?' })[0];
}
浙公网安备 33010602011771号