//使用图片为水印
public static string ReSizePhoto(string filePath, string LogoPath, int width, int height)
{
string fileName = System.IO.Path.GetFileName(filePath).ToLower();
string fileExtension = System.IO.Path.GetExtension(filePath).ToLower();
string document = System.IO.Path.GetDirectoryName(filePath) + "\\";
bool check = false;
switch (fileExtension)
{
case ".jpg": check = true;
break;
case ".gif": check = true;
break;
default: check = false;
break;
} //图片按比例缩放算法
if (check)
{
string saveName = Squirrel.Component.doEncrypt.SMD5(Guid.NewGuid().ToString().Replace("-", "")).Replace("-", "").ToLower().Substring(8, 16) + fileExtension;
System.Drawing.Image image = System.Drawing.Image.FromFile(filePath); double h = Convert.ToDouble(image.Height.ToString());
double w = Convert.ToDouble(image.Width.ToString());
double bl = h / w;
if (w > width)
{
if (width == 0 && height != 0)
{
h = height;
w = height * (1 / bl);
}
else if (width != 0 && height == 0)
{
w = width;
h = width * bl;
}
else
{
w = width;
h = height;
}
} //取得图片大小
System.Drawing.Size size = new Size((int)w, (int)h);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.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(Color.White);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel); //图片水印
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(LogoPath));
Graphics a = Graphics.FromImage(bitmap);
a.DrawImage(copyImage, new Rectangle(bitmap.Width - copyImage.Width, bitmap.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); copyImage.Dispose();
a.Dispose();
copyImage.Dispose(); //保存高清晰度的缩略图
string Path = document + saveName;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose();
image.Dispose();
bitmap.Dispose(); //删除源图片
Squirrel.Component.doIO.DeletePath(filePath);
return saveName;
}
else
{
return fileName;
}
}随便贴个混点分
public static string ReSizePhoto(string filePath, string LogoPath, int width, int height)
{
string fileName = System.IO.Path.GetFileName(filePath).ToLower();
string fileExtension = System.IO.Path.GetExtension(filePath).ToLower();
string document = System.IO.Path.GetDirectoryName(filePath) + "\\";
bool check = false;
switch (fileExtension)
{
case ".jpg": check = true;
break;
case ".gif": check = true;
break;
default: check = false;
break;
} //图片按比例缩放算法
if (check)
{
string saveName = Squirrel.Component.doEncrypt.SMD5(Guid.NewGuid().ToString().Replace("-", "")).Replace("-", "").ToLower().Substring(8, 16) + fileExtension;
System.Drawing.Image image = System.Drawing.Image.FromFile(filePath); double h = Convert.ToDouble(image.Height.ToString());
double w = Convert.ToDouble(image.Width.ToString());
double bl = h / w;
if (w > width)
{
if (width == 0 && height != 0)
{
h = height;
w = height * (1 / bl);
}
else if (width != 0 && height == 0)
{
w = width;
h = width * bl;
}
else
{
w = width;
h = height;
}
} //取得图片大小
System.Drawing.Size size = new Size((int)w, (int)h);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.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(Color.White);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.GraphicsUnit.Pixel); //图片水印
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(LogoPath));
Graphics a = Graphics.FromImage(bitmap);
a.DrawImage(copyImage, new Rectangle(bitmap.Width - copyImage.Width, bitmap.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); copyImage.Dispose();
a.Dispose();
copyImage.Dispose(); //保存高清晰度的缩略图
string Path = document + saveName;
bitmap.Save(Path, System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose();
image.Dispose();
bitmap.Dispose(); //删除源图片
Squirrel.Component.doIO.DeletePath(filePath);
return saveName;
}
else
{
return fileName;
}
}随便贴个混点分
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货