
using System;
using System.Data;
using System.Configuration;
using System.Drawing;
using using System.Drawing.Drawing2D;
/// <summary>
/// MakeThumbnails 的摘要说明
/// </summary>
public class MakeThumbnails
{
public MakeThumbnails()
{
}
/// <summary>
/// 生成缩略图(最终图片固定大小,图片按比例缩小,并为缩略图加上边框,以jpg格式保存)
/// </summary>
/// <param name="sourceImg">原图片(物理路径)</param>
/// <param name="toPath">缩略图存放地址(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="backColor">如果图片按比例缩小后不能填充满缩略图,则使用此颜色填充(比如"#FFFFFF")</param>
/// <param name="borderColor">边框颜色(比如"#999999")</param>
public static void MakePic(string sourceImg, string toPath, int width, int height, string backColor, string borderColor)
{
string[] files = Directory.GetFiles(sourceImg, "*.JPG");
string[] toPathFilename = new string[files.Length];
int[] nArray = new int[files.Length];
string mode;
int i = -1;
//for (int i = 0; i < files.Length; i++)
foreach (int j in nArray)
{
i++;
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(files[i]);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
toPathFilename[i] = files[i].Substring(files[i].LastIndexOf(@"\") + 1);
if (ow < towidth && oh < toheight)
{
towidth = ow;
toheight = oh;
}
else
{
if (originalImage.Width / originalImage.Height >= width / height)
{
mode = "W";
}
else
{
mode = "H";
}
switch (mode)
{
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
default:
break;
}
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置默认插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
////设置高速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
//清空画布并以指定颜色填充
g.Clear(ColorTranslator.FromHtml(backColor));
//在指定位置并且按指定大小绘制原图片的指定部分
int top = (height - toheight) / 2;
int left = (width - towidth) / 2;
g.DrawImage(originalImage, new System.Drawing.Rectangle(left, top, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
Pen pen = new Pen(ColorTranslator.FromHtml(borderColor));
g.DrawRectangle(pen, 0, 0, width - 1, height - 1);
try
{
//以jpg格式保存缩略图
bitmap.Save(toPath + "\\" + toPathFilename[i], System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
if (i % 3 == 0)
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
}
}
using System.Data;
using System.Configuration;
using System.Drawing;
using using System.Drawing.Drawing2D;
/// <summary>
/// MakeThumbnails 的摘要说明
/// </summary>
public class MakeThumbnails
{
public MakeThumbnails()
{
}
/// <summary>
/// 生成缩略图(最终图片固定大小,图片按比例缩小,并为缩略图加上边框,以jpg格式保存)
/// </summary>
/// <param name="sourceImg">原图片(物理路径)</param>
/// <param name="toPath">缩略图存放地址(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="backColor">如果图片按比例缩小后不能填充满缩略图,则使用此颜色填充(比如"#FFFFFF")</param>
/// <param name="borderColor">边框颜色(比如"#999999")</param>
public static void MakePic(string sourceImg, string toPath, int width, int height, string backColor, string borderColor)
{
string[] files = Directory.GetFiles(sourceImg, "*.JPG");
string[] toPathFilename = new string[files.Length];
int[] nArray = new int[files.Length];
string mode;
int i = -1;
//for (int i = 0; i < files.Length; i++)
foreach (int j in nArray)
{
i++;
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(files[i]);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
toPathFilename[i] = files[i].Substring(files[i].LastIndexOf(@"\") + 1);
if (ow < towidth && oh < toheight)
{
towidth = ow;
toheight = oh;
}
else
{
if (originalImage.Width / originalImage.Height >= width / height)
{
mode = "W";
}
else
{
mode = "H";
}
switch (mode)
{
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
default:
break;
}
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置默认插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
////设置高速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
//清空画布并以指定颜色填充
g.Clear(ColorTranslator.FromHtml(backColor));
//在指定位置并且按指定大小绘制原图片的指定部分
int top = (height - toheight) / 2;
int left = (width - towidth) / 2;
g.DrawImage(originalImage, new System.Drawing.Rectangle(left, top, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
Pen pen = new Pen(ColorTranslator.FromHtml(borderColor));
g.DrawRectangle(pen, 0, 0, width - 1, height - 1);
try
{
//以jpg格式保存缩略图
bitmap.Save(toPath + "\\" + toPathFilename[i], System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
if (i % 3 == 0)
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
}
}
[ 本帖最后由 迪拜axq 于 2015-8-28 14:56 编辑 ]