谁能给个asp.net的图片水印源码啊
发布网友
发布时间:2022-04-19 04:18
我来回答
共1个回答
热心网友
时间:2022-04-19 05:47
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class ImgHelper
{
/// <summary>
/// 功能生成小图,+版权信息(图片水印)
/// </summary>
/// <param name="path">要保存的实际路径</param>
/// <param name="file">aspx页面中的上传控件</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
/// <param name="mode">裁剪方式:hw指定高宽缩放(自动分析长宽比)、w指定宽,高按比例、h指定高,宽按比例、cut指定高宽裁减</param>
/// <param name="strInfo">版权信息</param>
/// <param name="left">左边位置</param>
/// <param name="top">上边位置</param>
/// <returns>返回经过处理后的文件名</returns>
public static string GetSamilImageAddCopyrightByImg(string path, FileUpload file, int width, int height, string mode, string Path_sypf)
{
string FileName = filename(file);
Path_sypf = path + Path_sypf;
if (File.Exists(FileName) == false)
{
file.PostedFile.SaveAs(path + FileName);//保存原图
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(path + FileName);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(oldimage);
g.DrawImage(copyImage, new System.Drawing.Rectangle(oldimage.Width - copyImage.Width,
oldimage.Height - copyImage.Height, copyImage.Width, copyImage.Height),
0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
string YFileName = "y_" + FileName;
switch (file.PostedFile.FileName.Split(".".ToCharArray())[1].ToUpper())
{
case "JPEG":
oldimage.Save(path + YFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "JPG":
oldimage.Save(path + YFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "GIF":
oldimage.Save(path + YFileName, System.Drawing.Imaging.ImageFormat.Gif);
break;
case "PNG":
oldimage.Save(path + YFileName, System.Drawing.Imaging.ImageFormat.Png);
break;
case "BMP":
oldimage.Save(path + YFileName, System.Drawing.Imaging.ImageFormat.Bmp);
break;
default:
oldimage.Save(path + YFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = oldimage.Width;
int oh = oldimage.Height;
float owh = ((float)ow) / ((float)oh);
switch (mode)
{
case "hw"://指定高宽缩放(自动分析长宽比)
if (ow >= oh)
{
towidth = width;
toheight = Convert.ToInt32((float)towidth / owh);
}
else
{
toheight = height;
towidth = Convert.ToInt32((float)toheight * owh);
}
break;
case "w"://指定宽,高按比例
toheight = oldimage.Height * width / oldimage.Width;
break;
case "h"://指定高,宽按比例
towidth = oldimage.Width * height / oldimage.Height;
break;
case "cut"://指定高宽裁减(不变形)
if ((double)oldimage.Width / (double)oldimage.Height > (double)towidth / (double)toheight)
{
oh = oldimage.Height;
ow = oldimage.Height * towidth / toheight;
y = 0;
x = (oldimage.Width - ow) / 2;
}
else
{
ow = oldimage.Width;
oh = oldimage.Width * height / towidth;
x = 0;
y = (oldimage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片,并制定宽高
System.Drawing.Image thumbnailImage = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics newpic = System.Drawing.Graphics.FromImage(thumbnailImage);
//设置高质量插值法
newpic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
newpic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
newpic.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
newpic.DrawImage(oldimage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
string S_FileName = "s_" + FileName;
switch (file.PostedFile.FileName.Split(".".ToCharArray())[1].ToUpper())
{
case "JPEG":
case "JPG":
thumbnailImage.Save(path + S_FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "GIF":
thumbnailImage.Save(path + S_FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "PNG":
thumbnailImage.Save(path + S_FileName, System.Drawing.Imaging.ImageFormat.Png);
break;
case "BMP":
thumbnailImage.Save(path + S_FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
default:
thumbnailImage.Save(path + S_FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
}
thumbnailImage.Dispose();
copyImage.Dispose();
g.Dispose();
oldimage.Dispose();
File.Delete(path + FileName);
return S_FileName;//返回处理后的图片名称
}
else//如果图片已经存在则返回""
{
return "";
}
}
/// <summary>
/// 生成文件名(包括路径)
/// </summary>
/// <param name="file">上传控件</param>
/// <param name="filesrc">上传到的目录</param>
/// <returns>根据当前时间并加入随机数</returns>
public static string filename(FileUpload file)
{
//得到当前时间精确到毫秒
string FileName = DateTime.Now.ToString("yyMMddHHmmss") + DateTime.Now.Millisecond.ToString();
//得到随机数
char[] s = new char[]{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G'
,'J','K','L','M','N','P','Q','R','T','U','V','W'
,'X','Z'};
string num = "";
Random r = new Random();
for (int i = 0; i < 6; i++)
{
num += s[r.Next(0, s.Length - 1)].ToString();
}
//得到文件上传路径
string[] FileSrc = file.PostedFile.FileName.Split(@"\".ToCharArray());
string YFileName = FileSrc[FileSrc.Length - 1];
//得到文件扩展名
string FileEx = YFileName.Split('.')[1].ToUpper();
//得到文件名并返回
return FileName + num + "." + FileEx;
}
}