Asp.net上傳圖片同時生成文字水印圖,圖片水印圖,縮略圖
.aspx
<body>
<form runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="上傳圖片" />
</div>
</form>
</body>
.cs
protected void Button1_Click(object sender, EventArgs e)
{
string fileContentType = this.FileUpload1.PostedFile.ContentType;
if (this.FileUpload1.HasFile)
{
if (fileContentType == "image/bmp" || fileContentType == "image/gif" ||
fileContentType == "image/pjpeg")
{
// 文件名,如1.jpeg
string filename = this.FileUpload1.FileName;
// 控件裏的源路徑,如C://1.jpeg
string fullfilename = this.FileUpload1.PostedFile.FileName;
// 上傳至服務器的圖片路徑
string webfilename = Server.MapPath("image/" + filename);
// 上傳至服務器的文字水印圖片路徑
string textwaterwebfilename = Server.MapPath("textwater/" + filename);
// 上傳至服務器的圖片水印圖片路徑
string picwaterfilename = Server.MapPath("picwater/" + filename);
// 水印圖片
string waterpic = Server.MapPath("picwater/waterpic.png");
// 縮略圖
string thumbnailfilename = Server.MapPath("thumbnailpic/" + filename);
// 檢驗文件尺寸
if (IsPecificSize(fullfilename, 150, 200))
{
if (!File.Exists(webfilename))
{
this.FileUpload1.SaveAs(webfilename);
SaveTextWaterImage(webfilename, textwaterwebfilename); // 給圖片添
加文字水印
SavePicWaterImage(webfilename, picwaterfilename, waterpic);// 給圖片添
加圖片水印
GetThumbnailPic(webfilename, thumbnailfilename, 80, 80);
}
else
{
Response.Write("<script>alert('圖片已經存在')</script>");
}
}
else
{
Response.Write("<script>alert('文件大小不符,必須是50*50的大小')
</script>");
}
}
else
{
Response.Write("<script>alert('文件類型不符')</script>");
}
}
}
/// <summary>
/// 是否符合規定大小
/// </summary>
/// <param name="path">源圖片路徑</param>
/// <param name="width">寬,單位為像素</param>
/// <param name="height">高,單位為像素</param>
/// <returns></returns>
private bool IsPecificSize(string path, int width, int height)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
if (image.Width == width && image.Height == height)
return true;
else
return false;
}
/// <summary>
/// 在圖片上增加文字水印
/// </summary>
/// <param name="path">原服務器圖片路徑</param>
/// <param name="newpath">生成的帶文字水印的圖片路徑</param>
private void SaveTextWaterImage(string path, string newpath)
{
string addText = "xuyue";
System.Drawing.Image image = System.Drawing.Image.FromFile(path); // 從指定的文件
創建
System.Drawing.Image
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
System.Drawing.Font f = new System.Drawing.Font("Verdana", 15);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
g.DrawString(addText, f, b,70,170);
g.Dispose();
image.Save(newpath);
image.Dispose();
}
/// <summary>
/// 在圖片上生成圖片水印
/// </summary>
/// <param name="path">源圖片路徑</param>
/// <param name="newpath">新圖片路徑</param>
/// <param name="waterpicpath">水印圖片路徑</param>
private void SavePicWaterImage(string path, string newpath, string waterpicpath)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
System.Drawing.Image waterimage = System.Drawing.Image.FromFile(waterpicpath);
g.DrawImage(waterimage, new System.Drawing.Rectangle
(image.Width - waterimage.Width, image.Height - waterimage.Height,
waterimage.Width, waterimage.Height), 0, 0, waterimage.Width,
waterimage.Height, System.Drawing.GraphicsUnit.Pixel);
g.Dispose();
image.Save(newpath);
image.Dispose();
}
/// <summary>
/// 獲得縮略圖
/// </summary>
/// <param name="originalpicpath">源圖路徑</param>
/// <param name="smallpicpath">縮略成小圖後的新地址</param>
/// <param name="smallwidth">小圖的寬度</param>
/// <param name="smallheight">小圖的高度</param>
private void GetThumbnailPic(string originalpicpath,string smallpicpath,int
smallwidth,int smallheight )
{
// 定義變量
System.Drawing.Image.GetThumbnailImageAbort callb = null;
System.Drawing.Image imageSmall, imageBig;
// 加載大圖片
imageBig = System.Drawing.Image.FromFile(originalpicpath);
//開始生成
imageSmall = imageBig.GetThumbnailImage(smallwidth, smallheight, callb, new
System.IntPtr());
//保存縮略圖
imageSmall.Save(smallpicpath);
//釋放圖片對象資源
imageSmall.Dispose();
imageBig.Dispose();
}
最後更新:2017-04-02 06:52:24