public static void ImgSource(string source, string table, int id)
{
//原图片路径
string type = "";
string ImgType = "";
switch (table)
{
case "inc_ad_pos": ImgType = "adpos"; break;
case "sys_ad": ImgType = "ad"; break;
case "sys_img": ImgType = "sysimg"; break;
}
int SmallImgWidth = Convert.ToInt32(ConfigurationManager.AppSettings["SmallImgWidth"]);
int SmallImgHight = Convert.ToInt32(ConfigurationManager.AppSettings["SmallImgHight"]);
//HttpPostedFile files = null;
//获取原文件,图片格式
string fileExtension = System.IO.Path.GetExtension(source).ToLower();
switch (fileExtension)
{
case ".gif": type = ".gif"; break;
case ".jpg": type = ".jpg"; break;
case ".jpeg": type = ".jpeg"; break;
case ".bmp": type = ".bmp"; break;
case ".png": type = ".png"; break;
case ".swf": type = ".swf"; break;
case ".html": type = ".html"; break;
}
string UploadDirName = "";
//缩略图保存位置和文件名,图片格式
if (type == ".swf")
{
UploadDirName = "Swf";
}
else if (type == ".html")
{
UploadDirName = "Html5";
}
else
{
UploadDirName = "Img";
}
string BaseDir = "E:\\FileDownLoad\\" + UploadDirName;
InitSystem(BaseDir);
// AppDomain.CurrentDomain.BaseDirectory +
// string DirSecond = GetDirSecond();
string[] s = source.Split('/');//获取分割符“/”中间的字符串
string DirSecond = "";
if (s.Length >= 2)
{
int i = s.Length - 2;
DirSecond = s[i];
string FileName = System.IO.Path.GetFileName(source).ToLower();//GetNewFileName() + type;
string NormalImgPath = BaseDir + "\\" + ImgType + "\\" + DirSecond + "\\";
string SmallImgPath = BaseDir + "\\" + ImgType + "\\" + DirSecond + "\\";
string sqlurl = UploadDirName + "/" + ImgType + "/" + DirSecond + "/";
string Result = UploadDirName + "/" + ImgType + "/" + DirSecond.Replace("\\", "/") + "/" + FileName;
IsExistPath(NormalImgPath);
IsExistPath(SmallImgPath);
System.Net.WebClient wc = new System.Net.WebClient();//根据Url下载图片
wc.DownloadFile(source, NormalImgPath + FileName);//将图片写入文件夹
wc.Dispose();
//指定日志文件的目录
string fname = "D:\\UPLoadimageUrl\\" + table + ".txt ";
//定义文件信息对象
StreamWriter sws = new StreamWriter(fname, true, System.Text.Encoding.Default);
sws.WriteLine(Convert.ToString(id) +"--"+ "http://" + sqlurl + FileName);
sws.Close();
if (type != ".swf" && type != ".html")
{
先存到服务器,再打开转换成Stream
FileStream fs = File.Open(NormalImgPath + FileName, FileMode.Open, FileAccess.Read);
//Stream stream = file.
int loaderWidth = 0;
int loaderHeight = 0;
if (fileExtension == ".jpg" || fileExtension == ".bmp" || fileExtension == ".png" || fileExtension == ".gif" || fileExtension == ".jpeg")
{
if (File.Exists(NormalImgPath + FileName))
{
System.Drawing.Image tempimage = System.Drawing.Image.FromStream(fs, true);
loaderWidth = tempimage.Width;//宽
loaderHeight = tempimage.Height;//高
}
}
string mode = string.Empty;
//判断normal 是否大于 small尺寸
if (SizeCompare(loaderWidth, loaderHeight, SmallImgWidth, SmallImgHight, ref mode))
{
MakeThumbnail(NormalImgPath + FileName, SmallImgPath + FileName, fs, SmallImgWidth, SmallImgHight);
MakeThumbsquarenail(NormalImgPath + FileName, SmallImgPath + FileName, fs, SmallImgWidth, SmallImgHight);
}
else
{
MakeThumbnail(NormalImgPath + FileName, SmallImgPath + FileName, fs, SmallImgWidth, SmallImgHight);
MakeThumbsquarenail(NormalImgPath + FileName, SmallImgPath + FileName, fs, SmallImgWidth, SmallImgHight);
}
}
}
}
//检测是否存在默认目录,如果没有则创建bigImg,normalImg,SmallImg
private static void InitSystem(string baseDir)
{
//配置文件必须按照 Normal,thumb 顺序
DirNames = ConfigurationManager.AppSettings["DirNames"].Split(',');
foreach (string s in DirNames)
{
IsExistPath(baseDir + s);
}
}
//判断图片的长和宽(指定使用最大的值最为压缩比例)
private static bool SizeCompare(int leftWidth, int leftHight, int rightWidth, int rightHight, ref string mod)
{
if (leftWidth >= leftHight)
{
mod = "W";
}
else
{
mod = "H";
}
if (leftWidth >= rightWidth || leftHight >= rightHight)
{
return true;
}
else
{
return false;
}
}
//Create FileName
private static string GetNewFileName()
{
Random random = new Random();
int randInt = random.Next(1, 10000);
int YMD = Convert.ToInt32(DateTime.Now.ToString("yyMMdd"));
int Hms = Convert.ToInt32(DateTime.Now.ToString("HHmmss"));
return YMD + "-" + Hms + "-" + randInt;//Guid.NewGuid().ToString();
}
//二级目录加三级目录
private static string GetDirSecond()
{
string YMD = Convert.ToString(DateTime.Now.ToString("yyMMdd"));
//int DH = Convert.ToInt32(DateTime.Now.ToString("HH"));
return YMD;
}
/// <summary>
/// 创建图片文件夹
/// </summary>
/// <param name="path"></param>
private static void IsExistPath(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
/**/
<summary>
/**/
/// 生成等比例缩略图(无空白填充)
/**/
/// </summary>
/**/
/// <param name="originalImagePath">源图路径(物理路径)</param>
/**/
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/**/
/// <param name="width">缩略图宽度</param>
/**/
/// <param name="height">缩略图高度</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, FileStream fs, int width, int height)
{
//FileStream fs = File.Open(originalImagePath, FileMode.Open, FileAccess.Read);
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(fs, true);
//获取原始图片
//System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
//缩略图画布宽高
int towidth = width;
int toheight = height;
//原始图片写入画布坐标和宽高(用来设置裁减溢出部分)
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
//原始图片画布,设置写入缩略图画布坐标和宽高(用来原始图片整体宽高缩放)
int bg_x = 0;
int bg_y = 0;
int bg_w = towidth;
int bg_h = toheight;
//倍数变量
double multiple = 0;
//获取宽长的或是高长与缩略图的倍数
if (originalImage.Width >= originalImage.Height) multiple = (double)originalImage.Width / (double)width;
else multiple = (double)originalImage.Height / (double)height;
//上传的图片的宽和高小等于缩略图
if (ow <= width && oh <= height)
{
//缩略图按原始宽高
bg_w = originalImage.Width;
bg_h = originalImage.Height;
//空白部分用背景色填充
//bg_x = Convert.ToInt32(((double)towidth - (double)ow) / 2);
//bg_y = Convert.ToInt32(((double)toheight - (double)oh) / 2);
}
//上传的图片的宽和高大于缩略图
else
{
//宽高按比例缩放
bg_w = Convert.ToInt32((double)originalImage.Width / multiple);
bg_h = Convert.ToInt32((double)originalImage.Height / multiple);
//空白部分用背景色填充
//bg_y = Convert.ToInt32(((double)height - (double)bg_h) / 2);
//bg_x = Convert.ToInt32(((double)width - (double)bg_w) / 2);
}
//新建一个bmp图片,并设置缩略图大小.
System.Drawing.Image bitmap = new System.Drawing.Bitmap(bg_w, bg_h);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并设置背景色
g.Clear(System.Drawing.ColorTranslator.FromHtml("#F2F2F2"));
//在指定位置并且按指定大小绘制原图片的指定部分
//第一个System.Drawing.Rectangle是原图片的画布坐标和宽高,第二个是原图片写在画布上的坐标和宽高,最后一个参数是指定数值单位为像素
g.DrawImage(originalImage, new System.Drawing.Rectangle(bg_x, bg_y, bg_w, bg_h), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
try
{
//获取图片类型
string fileExtension = System.IO.Path.GetExtension(originalImagePath).ToLower();
//按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
switch (fileExtension)
{
case ".gif": bitmap.Save(thumbnailPath.Replace(".gif", "") + "_thumb.gif", System.Drawing.Imaging.ImageFormat.Gif); break;
case ".jpg": bitmap.Save(thumbnailPath.Replace(".jpg", "") + "_thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); break;
case ".jpeg": bitmap.Save(thumbnailPath.Replace(".jpeg", "") + "_thumb.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg); break;
case ".bmp": bitmap.Save(thumbnailPath.Replace(".bmp", "") + "_thumb.bmp", System.Drawing.Imaging.ImageFormat.Bmp); break;
case ".png": bitmap.Save(thumbnailPath.Replace(".png", "") + "_thumb.png", System.Drawing.Imaging.ImageFormat.Png); break;
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
/**/
<summary>
/**/
/// 生成等比例缩略图(指定宽高,空白位置用底色填充)
/**/
/// </summary>
/**/
/// <param name="originalImagePath">源图路径(物理路径)</param>
/**/
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/**/
/// <param name="width">缩略图宽度</param>
/**/
/// <param name="height">缩略图高度</param>
public static void MakeThumbsquarenail(string originalImagePath, string thumbnailPath, FileStream fs, int width, int height)
{
//FileStream fs = File.Open(originalImagePath, FileMode.Open, FileAccess.Read);
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(fs, true);
//获取原始图片
//System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
//缩略图画布宽高
int towidth = width;
int toheight = height;
//原始图片写入画布坐标和宽高(用来设置裁减溢出部分)
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
//原始图片画布,设置写入缩略图画布坐标和宽高(用来原始图片整体宽高缩放)
int bg_x = 0;
int bg_y = 0;
int bg_w = towidth;
int bg_h = toheight;
//倍数变量
double multiple = 0;
//获取宽长的或是高长与缩略图的倍数
if (originalImage.Width >= originalImage.Height) multiple = (double)originalImage.Width / (double)width;
else multiple = (double)originalImage.Height / (double)height;
//上传的图片的宽和高小等于缩略图
if (ow <= width && oh <= height)
{
//缩略图按原始宽高
bg_w = originalImage.Width;
bg_h = originalImage.Height;
//空白部分用背景色填充
bg_x = Convert.ToInt32(((double)towidth - (double)ow) / 2);
bg_y = Convert.ToInt32(((double)toheight - (double)oh) / 2);
}
//上传的图片的宽和高大于缩略图
else
{
//宽高按比例缩放
bg_w = Convert.ToInt32((double)originalImage.Width / multiple);
bg_h = Convert.ToInt32((double)originalImage.Height / multiple);
//空白部分用背景色填充
bg_y = Convert.ToInt32(((double)height - (double)bg_h) / 2);
bg_x = Convert.ToInt32(((double)width - (double)bg_w) / 2);
}
//新建一个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.HighQualityBilinear;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并设置背景色
g.Clear(System.Drawing.ColorTranslator.FromHtml("#F2F2F2"));
//在指定位置并且按指定大小绘制原图片的指定部分
//第一个System.Drawing.Rectangle是原图片的画布坐标和宽高,第二个是原图片写在画布上的坐标和宽高,最后一个参数是指定数值单位为像素
g.DrawImage(originalImage, new System.Drawing.Rectangle(bg_x, bg_y, bg_w, bg_h), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
try
{
//获取图片类型
string fileExtension = System.IO.Path.GetExtension(originalImagePath).ToLower();
//按原图片类型保存缩略图片,不按原格式图片会出现模糊,锯齿等问题.
switch (fileExtension)
{
case ".gif": bitmap.Save(thumbnailPath.Replace(".gif", "") + "_thumb_s.gif", System.Drawing.Imaging.ImageFormat.Gif); break;
case ".jpg": bitmap.Save(thumbnailPath.Replace(".jpg", "") + "_thumb_s.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); break;
case ".jpeg": bitmap.Save(thumbnailPath.Replace(".jpeg", "") + "_thumb_s.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg); break;
case ".bmp": bitmap.Save(thumbnailPath.Replace(".bmp", "") + "_thumb_s.bmp", System.Drawing.Imaging.ImageFormat.Bmp); break;
case ".png": bitmap.Save(thumbnailPath.Replace(".png", "") + "_thumb_s.png", System.Drawing.Imaging.ImageFormat.Png); break;
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}