asp.net 添加水印的代碼(已測(cè)試)
/// <summary>
/// 圖片修改類,主要是用來(lái)保護(hù)圖片版權(quán)的,版權(quán)歸原作者所有
/// </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>
/// 獲取或設(shè)置要修改的圖像路徑
/// </summary>
public string ModifyImagePath
{
get { return this.modifyImagePath; }
set { this.modifyImagePath = value; }
}
/// <summary>
/// 獲取或設(shè)置在畫的圖片路徑(水印圖片)
/// </summary>
public string DrawedImagePath
{
get { return this.drawedImagePath; }
set { this.drawedImagePath = value; }
}
/// <summary>
/// 獲取或設(shè)置水印在修改圖片中的右邊距
/// </summary>
public int RightSpace
{
get { return this.rightSpace; }
set { this.rightSpace = value; }
}
//獲取或設(shè)置水印在修改圖片中距底部的高度
public int BottoamSpace
{
get { return this.bottoamSpace; }
set { this.bottoamSpace = value; }
}
/// <summary>
/// 獲取或設(shè)置要繪制水印的透明度,注意是原來(lái)圖片透明度的百分比
/// </summary>
public int LucencyPercent
{
get { return this.lucencyPercent; }
set
{
if (value >= 0 && value <= 100)
this.lucencyPercent = value;
}
}
/// <summary>
/// 獲取或設(shè)置要輸出圖像的路徑
/// </summary>
public string OutPath
{
get { return this.outPath; }
set { this.outPath = value; }
}
#endregion
#region "methods"
/// <summary>
/// 開(kāi)始繪制水印
/// </summary>
public void DrawImage()
{
Image modifyImage = null;
Image drawedImage = null;
Graphics g = null;
try
{
//建立圖形對(duì)象
modifyImage = Image.FromFile(this.ModifyImagePath);
drawedImage = Image.FromFile(this.DrawedImagePath);
g = Graphics.FromImage(modifyImage);
//獲取要繪制圖形坐標(biāo)
int x = modifyImage.Width - this.rightSpace;
int y = modifyImage.Height - this.BottoamSpace;
//設(shè)置顏色矩陣
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
}
前臺(tái)代碼如下所示
<%@ 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);
//實(shí)例化類
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);
//保存加水印過(guò)后的圖片,刪除原始圖片
if (File.Exists(savePath))
{
File.Delete(savePath);
//File.Delete(wm.OutPath);
}
- C#(.net)水印圖片的生成完整實(shí)例
- Asp.net 文件上傳類(取得文件后綴名,保存文件,加入文字水印)
- asp.net下用Aspose.Words for .NET動(dòng)態(tài)生成word文檔中的圖片或水印的方法
- .net生成縮略圖及水印圖片時(shí)出現(xiàn)GDI+中發(fā)生一般性錯(cuò)誤解決方法
- asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
- asp.net下GDI+的一些常用應(yīng)用(水印,文字,圓角處理)技巧
- .net c# gif動(dòng)畫如何添加圖片水印實(shí)現(xiàn)思路及代碼
- asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)
- Asp.net簡(jiǎn)單實(shí)現(xiàn)給圖片增加文字水印
- .NET生成水印更好的方法實(shí)例代碼
相關(guān)文章
.NET?6實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用.NET?6實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2022-11-11最鋒利的Visual Studio Web開(kāi)發(fā)工具擴(kuò)展:Web Essentials使用詳解
Web Essentials是目前為止見(jiàn)過(guò)的最好用的VS擴(kuò)展工具了,具體功能請(qǐng)待我一一道來(lái)。2016-06-06asp.net4.0框架下驗(yàn)證機(jī)制失效的原因及處理辦法
asp.net4.0框架下驗(yàn)證機(jī)制失效的原因及處理辦法,需要的朋友可以參考一下2013-06-06ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖
這篇文章介紹了ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10關(guān)于.NET動(dòng)態(tài)代理的介紹和應(yīng)用簡(jiǎn)介
關(guān)于.NET動(dòng)態(tài)代理的介紹和應(yīng)用簡(jiǎn)介...2006-09-09MVC使用Spring.Net應(yīng)用IOC(依賴倒置)學(xué)習(xí)筆記3
這篇文章主要為大家詳細(xì)介紹了MVC使用Spring.Net應(yīng)用IOC(依賴倒置),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09不可忽視的 .NET 應(yīng)用5大性能問(wèn)題
ASP.NET 或是 Windows Forms 容器中,使用 ADO 庫(kù)與運(yùn)行在 CLR 交互,而 CLR 運(yùn)行在操作系統(tǒng)中而該硬件又與其他包含不同技術(shù)堆棧的硬件通過(guò)網(wǎng)絡(luò)相連。在你的應(yīng)用與外部環(huán)境之間,。我們還有 API 管理服務(wù)以及多級(jí)緩存基礎(chǔ)構(gòu)造數(shù)量龐雜,都可能影響應(yīng)用程序的性能!2016-05-05.NET性能優(yōu)化之為集合類型設(shè)置初始大小的方法
這篇文章主要介紹了.NET性能優(yōu)化之為集合類型設(shè)置初始大小的方法,今天要談的一個(gè)性能優(yōu)化的Tips是一個(gè)老生常談的點(diǎn),但是也是很多人沒(méi)有注意的一個(gè)點(diǎn)。在使用集合類型是,你應(yīng)該設(shè)置一個(gè)預(yù)估的初始大小,那么為什么需要這樣做?我們一起來(lái)從源碼的角度說(shuō)一說(shuō)2022-05-05