前台代碼:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Asp.net上傳圖片並添加浮水印</title>
</head>
<body>
<form id="form1" runat="server">
<div id="top">
Asp.Net上傳並添加浮水印
</div>
<div id="content">
選擇上傳圖片:<asp:FileUpload ID="upFileTest" runat="server" /><br />
<br />
<br />
<asp:RequiredFieldValidator ID="vUpFile" runat="server" ControlToValidate="upFileTest"
ErrorMessage="請選擇要上傳的檔案!"></asp:RequiredFieldValidator><br />
<br />
<br />
<br />
<asp:Button ID="btnText" runat="server" OnClick="btnText_Click" Text="上傳並添加文字浮水印" />
<br />
<br />
<br />
<br />
<asp:Label ID="lblStatus" runat="server"></asp:Label></div>
</form>
</body>
</html>
後台cs代碼:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace 圖片添加浮水印
{
public partial class WaterMarkTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblStatus.Visible = false;
}
}
private void Show(string str)
{
Response.Write("<script language='javascript'>alert('" + str + "')</script>");
}
private void upLoad()
{
string fullName = upFileTest.PostedFile.FileName;
string fileName = fullName.Substring(fullName.LastIndexOf("\\") + 1);
string marks = fullName.Substring(fullName.LastIndexOf(".") + 1); //圖片格式
if (marks.ToLower() != "jpg" && marks.ToLower() != "gif")
{
Show("上傳的檔案格式不正確,請重新選擇!");
}
else
{
string upFileName = Server.MapPath("upLoadFiles") + "\\" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + "." + marks;
Session["filePath"] = upFileName;
Session["marks"] = marks;
upFileTest.PostedFile.SaveAs(upFileName);
}
}
protected void btnText_Click(object sender, EventArgs e)
{
upLoad();
//開始加文字浮水印
System.Drawing.Image img = System.Drawing.Image.FromFile(Session["filePath"].ToString());
Graphics g = Graphics.FromImage(img);
g.DrawImage(img, 0, 0, img.Width, img.Height);
Font f = new Font("黑體", 10); //字型的型號大小
Brush b = new SolidBrush(Color.FromArgb(70, Color.WhiteSmoke));//畫筆的顏色
string str = "浮水印名稱";
g.DrawString(str, f, b, img.Width/2, img.Height*9/10);//浮水印的位置
g.Dispose();
string newFilePath = Server.MapPath("upLoadFiles") + "\\" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + "."
+ Session["marks"].ToString();
img.Save(newFilePath);
img.Dispose();
if (File.Exists(Session["filePath"].ToString()))
{
File.Delete(Session["filePath"].ToString());
}
lblStatus.Visible = true;
lblStatus.Text = "浮水印繪製成功!";
}
}
}
在編輯的過程中。出現過 GDI+ 中發生一般性錯誤。網上查了資料後發現,原因可能是:Bitmap 對象建立後沒有釋放。
Bitmap 對象或一個 映像 對象從一個檔案, 構造時該檔案仍保留鎖定對於對象的生存期。 因此, 無法更改映像並將其儲存回它產生相同的檔案。
替代方法
• 建立非索引映像。
• 建立索引映像。
這兩種情況下, 原始 位元影像 上調用 Bitmap.Dispose() 方法刪除該檔案上鎖或刪除要求, 流或記憶體保持活動。
建立非索引映像
即使原始映像被索引格式中該方法要求新映像位於每像素 (超過 8 位 -) -, 非索引像素格式。 此變通方法使用 Graphics.DrawImage() 方法來將映像複製到新 位元影像 對象:
1. 構造從流、 從記憶體, 或從檔案原始 位元影像 。
2. 建立新 位元影像 的相同大小, 帶有是超過 8 位 - - 像素 (BPP) 每像素格式。
3. 使用 Graphics.FromImage() 方法以擷取有關二 位元影像 Graphics 對象。
4. 用於 Graphics.DrawImage() 繪製首 位元影像 到二 位元影像 。
5. 用於 Graphics.Dispose() 處置是 圖形 。
6. 用於 Bitmap.Dispose() 是首 位元影像 處置。
建立索引映像
此解決辦法在索引格式建立一個 Bitmap 對象:
1. 構造從流、 從記憶體, 或從檔案原始 位元影像 。
2. 建立新 位元影像 具有相同的大小和像素格式作為首 位元影像 。
3. 使用 Bitmap.LockBits() 方法來鎖定整個映像對於兩 Bitmap 對象以其本機像素格式。
4. 使用 Marshal.Copy 函數或其他記憶體複製函數來從首 位元影像 複製到二 位元影像 映像位。
5. 使用 Bitmap.UnlockBits() 方法可以解鎖兩 Bitmap 對象。
6. 用於 Bitmap.Dispose() 是首 位元影像 處置。
由於外國人的思維和我們不一樣,我重新用執行個體解釋一下,我這裡使用的是建立非索引映像。
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//建立一個bitmap類型的bmp變數來讀取檔案。
Bitmap bmp = new Bitmap(openFileDialog1 .FileName );
//建立第二個bitmap類型的bmp2變數,我這裡是根據我的程式需要設定的。
Bitmap bmp2 = new Bitmap(1024, 768, PixelFormat.Format16bppRgb555);
//將第一個bmp拷貝到bmp2中
Graphics draw = Graphics.FromImage(bmp2);
draw.DrawImage(bmp,0,0);
pictureBox1.Image = (Image)bmp2 ;//讀取bmp2到picturebox
FILE = openFileDialog1.FileName;
openFileDialog1.Dispose();
draw.Dispose();
bmp.Dispose();//釋放bmp檔案資源
}
}
通過以上的讀取檔案,在儲存的時候就不會出現錯誤了。
但是也不是這個問題,最後發現,問題其實出在。圖片在加浮水印前和加浮水印後儲存的檔案夾是同一個,導致錯誤。
解決的方法是建立一個儲存加浮水印後的圖片的檔案夾。及:
將代碼:
string newFilePath = Server.MapPath("upLoadFiles") + "\\" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + "." + Session["marks"].ToString();
改為:string newFilePath = Server.MapPath("newMarkFiles") + "\\" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + "."+ Session["marks"].ToString();
建立檔案夾的名稱是:newMarkFiles