上传图片生成缩略图指定生成图片宽高度边框颜色和填充颜色
/// <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 bool MakePic(string sourceImg, string toPath, int width, int height, string backColor, string borderColor)
{
bool flag = false;
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(sourceImg);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
string mode;
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.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以指定颜色填充
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, System.Drawing.Imaging.ImageFormat.Jpeg);
flag = true;
}
catch (System.Exception e)
{
HttpContext.Current.Response.Write(" <script>alert('生成缩略图出错!') </script>");
return false;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
return flag;
}
public static string TransToTwo(string theStr)
{
if (theStr.Length == 1)
{
return "0"+theStr;
}
else
{
return theStr;
}
}
/// <summary>
/// fulFile控件名称
/// DirectionName 上传文件缩放的路径
/// DirectionName///格式限制
/// suo缩略图
///
/// </summary>
/// <param name="fulFile"></param>
/// <param name="DirectionName"></param>
/// <param name="ExtName"></param>
/// <returns></returns>
public static string UpLoadIMG(FileUpload fulFile, string DirectionName, string ExtName, string suo, int width, int height, string backColor, string borderColor)//smallPic=55*22|88*69|102*106
{
try
{
string returnStr = "";
string FileSavePath = System.Web.HttpContext.Current.Server.MapPath(DirectionName); //获取文件存放的根路径
if (fulFile.HasFile) //判断是否选择了要上传的文件
{
string name = fulFile.PostedFile.FileName;
string DirectionName2 = DateTime.Now.Year.ToString() + TransToTwo(DateTime.Now.Month.ToString());
//string DirectionName3 = TransToTwo(DateTime.Now.Day.ToString()) + TransToTwo(DateTime.Now.Hour.ToString()) + TransToTwo(DateTime.Now.Minute.ToString()) + TransToTwo(DateTime.Now.Second.ToString()) + DateTime.Now.Millisecond.ToString();
string DirectionName3 = TransToTwo(DateTime.Now.Day.ToString()) + TransToTwo(DateTime.Now.Hour.ToString()) + TransToTwo(DateTime.Now.Minute.ToString()) + TransToTwo(DateTime.Now.Second.ToString());
FileInfo file = new FileInfo(name);
string CreatePath = "";
CreatePath = FileSavePath + DirectionName2;
FileSavePath += DirectionName2 + "/" + DirectionName3 + file.Extension;
if (ExtName.Contains(file.Extension.ToUpper())) //符合类型
{
try
{
try
{
if (!Directory.Exists(CreatePath))
{
Directory.CreateDirectory(CreatePath);
}
fulFile.SaveAs(FileSavePath);
if (suo.Length > 0)
{
string FileSavePath1 = System.Web.HttpContext.Current.Server.MapPath(suo); //获取文件存放的根路径
string CreatePath1 = "";
CreatePath1= FileSavePath1 + DirectionName2;
FileSavePath1 += DirectionName2 + "/" + DirectionName3 + file.Extension;
// HttpContext.Current.Response.Write(" <script>document.write('"+FileSavePath1+"') </script>");
if (!Directory.Exists(CreatePath1))
{
Directory.CreateDirectory(CreatePath1);
}
bool flag = MakePic(FileSavePath, FileSavePath1, width, height, backColor, borderColor);
if (flag == false)
{
HttpContext.Current.Response.Write(" <script>alert('生成缩略图出错!') </script>");
return "";
}
}
}
catch
{
throw;
}
// HttpContext.Current.Response.Write(" <script>alert('" + FileSavePath + "') </script>");
}
catch (Exception Error)
{
HttpContext.Current.Response.Write(" <script>alert('" + Error.Message + "') </script>");
return "";
}
returnStr = DirectionName2 + "/" + DirectionName3;
//returnStr = AppDomain.CurrentDomain.BaseDirectory + DirectionName + returnStr;
return returnStr + file.Extension;
}
else
{
HttpContext.Current.Response.Write(" <script>alert('该文件格式不正确!') </script>");
return "";
}
}
else
{
return "";
}
}
catch
{
return "";
}
}
调用:UpLoadIMG(this.FileUpload1, "upfile/", ".GIF.JPG.PNG.SWF","upfile/pic/",300,300,"#ffffff","#000000");