加水印的功能代码如下所示
复制代码 代码如下:
/// <summary>
/// 图片修改类,主要是用来保护图片版权的,版权归原作者所有
/// </summary>
public class picmark
{
#region "member fields"
private string modifyImagePath = null;
private string drawedImagePath = null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent = 70;
private string outPath = null;
#endregion
public picmark()
{
}
#region "propertys"
/// <summary>
/// 获取或设置要修改的图像路径
/// </summary>
public string ModifyImagePath
{
get { return this.modifyImagePath; }
set { this.modifyImagePath = value; }
}
/// <summary>
/// 获取或设置在画的图片路径(水印图片)
/// </summary>
public string DrawedImagePath
{
get { return this.drawedImagePath; }
set { this.drawedImagePath = value; }
}
/// <summary>
/// 获取或设置水印在修改图片中的右边距
/// </summary>
public int RightSpace
{
get { return this.rightSpace; }
set { this.rightSpace = value; }
}
//获取或设置水印在修改图片中距底部的高度
public int BottoamSpace
{
get { return this.bottoamSpace; }
set { this.bottoamSpace = value; }
}
/// <summary>
/// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
/// </summary>
public int LucencyPercent
{
get { return this.lucencyPercent; }
set
{
if (value >= 0 && value <= 100)
this.lucencyPercent = value;
}
}
/// <summary>
/// 获取或设置要输出图像的路径
/// </summary>
public string OutPath
{
get { return this.outPath; }
set { this.outPath = value; }
}
#endregion
#region "methods"
/// <summary>
/// 开始绘制水印
/// </summary>
public void DrawImage()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
//建立图形对象
modifyImage = Image.FromFile(this.ModifyImagePath);
drawedImage = Image.FromFile(this.DrawedImagePath);
g = Graphics.FromImage(modifyImage);
//获取要绘制图形坐标
int x = modifyImage.Width - this.rightSpace;
int y = modifyImage.Height - this.BottoamSpace;
//设置颜色矩阵
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr = new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
//绘制阴影图像
g.DrawImage(
drawedImage,
new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
0, 0, drawedImage.Width, drawedImage.Height,
GraphicsUnit.Pixel, imgAttr);
//保存文件
string[] allowImageType = { ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo file = new FileInfo(this.ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (file.Extension.ToLower())
{
case ".jpg":
imageType = ImageFormat.Jpeg;
break;
case ".gif":
imageType = ImageFormat.Gif;
break;
case ".png":
imageType = ImageFormat.Png;
break;
case ".bmp":
imageType = ImageFormat.Bmp;
break;
case ".tif":
imageType = ImageFormat.Tiff;
break;
case ".wmf":
imageType = ImageFormat.Wmf;
break;
case ".ico":
imageType = ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms = new MemoryStream();
modifyImage.Save(ms, imageType);
byte[] imgData = ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs = null;
if (this.OutPath == null || this.OutPath == "")
{
File.Delete(this.ModifyImagePath);
fs = new FileStream(this.ModifyImagePath, FileMode.Create, FileAccess.Write);
}
else
{
fs = new FileStream(this.OutPath, FileMode.Create, FileAccess.Write);
}
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
#endregion
}
前台代码如下所示
复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="demo.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
cs类的代码如下所示
复制代码 代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string extension = Path.GetExtension(this.FileUpload1.FileName).ToUpper();
string fileName = Guid.NewGuid().ToString();
string savePath = Server.MapPath("../upfile/" + fileName+ extension);
if (!Directory.Exists(Path.GetDirectoryName(savePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
}
this.FileUpload1.SaveAs(savePath);
//实例化类
picmark wm = new picmark();
wm.DrawedImagePath = Server.MapPath("/upfile/" + "backlogo.gif") ;
wm.ModifyImagePath = savePath;
wm.RightSpace = 145;
wm.BottoamSpace =17;
wm.LucencyPercent = 50;
wm.OutPath = Server.MapPath("/upfile/" + fileName.Replace("-","").ToUpper() + extension);
wm.DrawImage();
//fileName = "_New_" + fileName;
//string sPath = Server.MapPath("../upfile/" + fileName + extension);
//this.FileUpload1.SaveAs(sPath);
//保存加水印过后的图片,删除原始图片
if (File.Exists(savePath))
{
File.Delete(savePath);
//File.Delete(wm.OutPath);
}
复制代码 代码如下:
/// <summary>
/// 图片修改类,主要是用来保护图片版权的,版权归原作者所有
/// </summary>
public class picmark
{
#region "member fields"
private string modifyImagePath = null;
private string drawedImagePath = null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent = 70;
private string outPath = null;
#endregion
public picmark()
{
}
#region "propertys"
/// <summary>
/// 获取或设置要修改的图像路径
/// </summary>
public string ModifyImagePath
{
get { return this.modifyImagePath; }
set { this.modifyImagePath = value; }
}
/// <summary>
/// 获取或设置在画的图片路径(水印图片)
/// </summary>
public string DrawedImagePath
{
get { return this.drawedImagePath; }
set { this.drawedImagePath = value; }
}
/// <summary>
/// 获取或设置水印在修改图片中的右边距
/// </summary>
public int RightSpace
{
get { return this.rightSpace; }
set { this.rightSpace = value; }
}
//获取或设置水印在修改图片中距底部的高度
public int BottoamSpace
{
get { return this.bottoamSpace; }
set { this.bottoamSpace = value; }
}
/// <summary>
/// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
/// </summary>
public int LucencyPercent
{
get { return this.lucencyPercent; }
set
{
if (value >= 0 && value <= 100)
this.lucencyPercent = value;
}
}
/// <summary>
/// 获取或设置要输出图像的路径
/// </summary>
public string OutPath
{
get { return this.outPath; }
set { this.outPath = value; }
}
#endregion
#region "methods"
/// <summary>
/// 开始绘制水印
/// </summary>
public void DrawImage()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
//建立图形对象
modifyImage = Image.FromFile(this.ModifyImagePath);
drawedImage = Image.FromFile(this.DrawedImagePath);
g = Graphics.FromImage(modifyImage);
//获取要绘制图形坐标
int x = modifyImage.Width - this.rightSpace;
int y = modifyImage.Height - this.BottoamSpace;
//设置颜色矩阵
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr = new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
//绘制阴影图像
g.DrawImage(
drawedImage,
new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
0, 0, drawedImage.Width, drawedImage.Height,
GraphicsUnit.Pixel, imgAttr);
//保存文件
string[] allowImageType = { ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo file = new FileInfo(this.ModifyImagePath);
ImageFormat imageType = ImageFormat.Gif;
switch (file.Extension.ToLower())
{
case ".jpg":
imageType = ImageFormat.Jpeg;
break;
case ".gif":
imageType = ImageFormat.Gif;
break;
case ".png":
imageType = ImageFormat.Png;
break;
case ".bmp":
imageType = ImageFormat.Bmp;
break;
case ".tif":
imageType = ImageFormat.Tiff;
break;
case ".wmf":
imageType = ImageFormat.Wmf;
break;
case ".ico":
imageType = ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms = new MemoryStream();
modifyImage.Save(ms, imageType);
byte[] imgData = ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs = null;
if (this.OutPath == null || this.OutPath == "")
{
File.Delete(this.ModifyImagePath);
fs = new FileStream(this.ModifyImagePath, FileMode.Create, FileAccess.Write);
}
else
{
fs = new FileStream(this.OutPath, FileMode.Create, FileAccess.Write);
}
if (fs != null)
{
fs.Write(imgData, 0, imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch { ;}
}
}
#endregion
}
前台代码如下所示
复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="demo.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
cs类的代码如下所示
复制代码 代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string extension = Path.GetExtension(this.FileUpload1.FileName).ToUpper();
string fileName = Guid.NewGuid().ToString();
string savePath = Server.MapPath("../upfile/" + fileName+ extension);
if (!Directory.Exists(Path.GetDirectoryName(savePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
}
this.FileUpload1.SaveAs(savePath);
//实例化类
picmark wm = new picmark();
wm.DrawedImagePath = Server.MapPath("/upfile/" + "backlogo.gif") ;
wm.ModifyImagePath = savePath;
wm.RightSpace = 145;
wm.BottoamSpace =17;
wm.LucencyPercent = 50;
wm.OutPath = Server.MapPath("/upfile/" + fileName.Replace("-","").ToUpper() + extension);
wm.DrawImage();
//fileName = "_New_" + fileName;
//string sPath = Server.MapPath("../upfile/" + fileName + extension);
//this.FileUpload1.SaveAs(sPath);
//保存加水印过后的图片,删除原始图片
if (File.Exists(savePath))
{
File.Delete(savePath);
//File.Delete(wm.OutPath);
}
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月16日
2024年11月16日
- 齐豫.2003-THE.UNHEARD.OF.CHYI.3CD【苏活音乐】【WAV+CUE】
- 黄乙玲1986-讲什么山盟海誓[日本东芝版][WAV+CUE]
- 曾庆瑜1991-柔情陷阱[台湾派森东芝版][WAV+CUE]
- 陈建江《享受男声》DTS-ES6.1【WAV】
- 群星《闪光的夏天 第5期》[FLAC/分轨][392.38MB]
- 徐小凤《三洋母带》1:1母盘直刻[WAV+CUE][981M]
- 王菲1995《菲靡靡之音》[香港首版][WAV+CUE][1G]
- 《双城之战》主题小游戏现已上线 扮演金克丝探索秘密基地
- 《霍格沃茨之遗》PS5Pro画面对比:光追性能显著提升
- 《怪猎荒野》PS5Pro主机版对比:B测性能都不稳定
- 黄宝欣.1992-黄宝欣金装精选2CD【HOMERUN】【WAV+CUE】
- 群星.1996-宝丽金流行爆弹精丫宝丽金】【WAV+CUE】
- 杜德伟.2005-独领风骚新歌精选辑3CD【滚石】【WAV+CUE】
- 安与骑兵《心无疆界》[低速原抓WAV+CUE]
- 柏菲唱片-群星〈胭花四乐〉2CD[原抓WAV+CUE]