黑马程序员技术交流社区
标题:
关于缩微图及加水印完美解决方案
[打印本页]
作者:
夏华成
时间:
2012-5-19 09:45
标题:
关于缩微图及加水印完美解决方案
以下是本人QQ空间早期日记. 都是自己实际工作中用到的(分享一下)
public static string UploadFile_wh(HttpPostedFile postedFile, string path, out string err,int w,int h)
{
err = "";
string fileUrlPath = "", pathfilename = "";
fileUrlPath = "/保存图片文件夹/" + path + "/" + DateTime.Now.ToString("yyyyMM") + "/";//生成的文件夹要保存的位置
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(fileUrlPath)))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(fileUrlPath)); //创建文件夹
}
try
{
string fileName, fileExtension, FileType;
int FileLength;
fileName = System.IO.Path.GetFileName(postedFile.FileName).ToLower();
fileExtension = System.IO.Path.GetExtension(fileName);
FileType = postedFile.ContentType.ToLower();
FileLength = postedFile.ContentLength;
Random rd = new Random();
string newName = "";
string str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(fileName.Replace(fileExtension, ""), "MD5");
if (str.Length > 10)
{
str = str.Substring(0, 10);
}
if (System.IO.File.Exists(System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + str + fileExtension))
{
newName = str + (DateTime.Now.Date.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Millisecond + Convert.ToInt32(rd.NextDouble() * 10000)).ToString();
}
else
{
newName = str;
}
//string newName = (DateTime.Now.Date.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Millisecond + Convert.ToInt32(rd.NextDouble() * 10000)).ToString() ;
string newNamex = newName + "x" + fileExtension;
newName = newName + fileExtension;
pathfilename = fileUrlPath + newName;
if (fileName != "")
{
if (FileType == "image/bmp" || FileType == "image/jpg" || FileType == "image/gif" || FileType == "image/pjpeg" || FileType == "image/jpeg" || FileType == "image/x-png" || FileType == "application/x-shockwave-flash")
{
//if (FileLength / 1024 < 300) //上传文件不能大于300kb
//{
string fullfilename = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + newName;
string fullfilenamex = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + newNamex;
string fullfilename1="", fullfilenamex1="";
postedFile.SaveAs(fullfilename);
System.Drawing.Image image100, image60,image30, syImage;
//三种不同尺寸的水印图(根据实际情况,定义水印图的尺寸)
image100 = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Request.MapPath("~/100.png"));
image60 = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Request.MapPath("~/60.png"));
image30 = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Request.MapPath("~/30.png"));
syImage = System.Drawing.Image.FromFile(fullfilename);
if (syImage.Height < 50 || syImage.Width < 50)
{
fullfilename1 = fullfilename;
fullfilenamex1 = fullfilenamex;
}
else if (syImage.Height < 100 || syImage.Width < 100)
{
fullfilename1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newName;
fullfilenamex1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newNamex;
Graphics g = Graphics.FromImage(syImage); // 绘制水印的 位置坐标
g.DrawImage(image30, syImage.Width - image30.Width, syImage.Height - image30.Height, image30.Width, image30.Height);
g.Dispose();
syImage.Save(fullfilename1);
syImage.Dispose();
image30.Dispose();
image100.Dispose();
image60.Dispose();
}
else if (syImage.Height < 250 || syImage.Width < 250)
{
fullfilename1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newName;
fullfilenamex1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newNamex;
Graphics g = Graphics.FromImage(syImage); // 绘制水印的 位置坐标
g.DrawImage(image60, syImage.Width - image60.Width, syImage.Height - image60.Height, image60.Width, image60.Height);
g.Dispose();
syImage.Save(fullfilename1);
syImage.Dispose();
image30.Dispose();
image100.Dispose();
image60.Dispose();
}
else
{
fullfilename1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newName;
fullfilenamex1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newNamex;
Graphics g = Graphics.FromImage(syImage); // 绘制水印的 位置坐标
g.DrawImage(image100, syImage.Width - image100.Width, syImage.Height - image100.Height, image100.Width, image100.Height);
g.Dispose();
syImage.Save(fullfilename1);
syImage.Dispose();
image30.Dispose();
image100.Dispose();
image60.Dispose();
}
if (FileType != "application/x-shockwave-flash")
{
try
{
//此处是生成缩微图类
WebCommon.ImageClass ic = new ImageClass(fullfilename1);
ic.MakeThumbnail(fullfilename1, fullfilenamex1, w, h);
//ic = null;
}
catch(Exception ex)
{ }
if (File.Exists(fullfilename))
{
File.Delete(fullfilename);
}
if (File.Exists(fullfilenamex))
{
File.Delete(fullfilenamex);
}
File.Move(fullfilename1, fullfilename);
File.Move(fullfilenamex1, fullfilenamex);
}
//}
//else
//{
// err = "文件过大!";
//}
}
else
{
err = "上传文件格式不正确!";
}
}
else
{
err = "请选择上传文件!";
}
}
catch (Exception e)
{
err = e.Message;
pathfilename = "";
}
return pathfilename;
}
//生成缩微图类
public class ImageClass
{
public System.Drawing.Image ResourceImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage;
/// <summary>
/// 类的构造函数
/// </summary>
/// <param name="ImageFileName">图片文件的全路径名称</param>
public ImageClass(string ImageFileName)
{
// ResourceImage = System.Drawing.Image.FromFile(ImageFileName);
ErrMessage = "";
}
public bool ThumbnailCallback()
{
return false;
}
public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width1, int height1)
{
System.Drawing.Image original_image = System.Drawing.Image.FromFile(originalImagePath);
// Calculate the new width and height
int width = original_image.Width;
int height = original_image.Height;
int target_width = width1;
int target_height = height1;
int new_width, new_height;
if (width == target_width && height == target_height)
{
new_height = height;
new_width = width;
}
else
{
float target_ratio = (float)target_width / (float)target_height;
float image_ratio = (float)width / (float)height;
if (target_ratio > image_ratio)
{
new_height = target_height;
new_width = (int)Math.Floor(image_ratio * (float)target_height);
}
else
{
new_height = (int)Math.Floor((float)target_width / image_ratio);
new_width = target_width;
}
new_width = new_width > target_width ? target_width : new_width;
new_height = new_height > target_height ? target_height : new_height;
}
int paste_x = 0; //(target_width - new_width) / 2;
int paste_y =0; //(target_height - new_height) / 2;
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(new_width, new_height);
//System.Drawing.Image bitmap = System.Drawing.Image.FromFile(originalImagePath);
//新建一个画板
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(original_image, paste_x, paste_y, new_width, new_height);
g.DrawImage(original_image, 0, 0, new_width, new_height);
try
{
//以jpg格式保存缩略图
//bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Save(thumbnailPath);
}
catch (System.Exception e)
{
throw e;
}
finally
{
original_image.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
复制代码
作者:
许庭洲
时间:
2014-8-20 08:32
值得学习ing!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2