今天講一個上傳圖片添加水印的方法,直接上代碼吧
protected void Button1_Click(object sender, EventArgs e)
{
int location = Convert.ToInt32(this.DropDownList1.SelectedValue);//獲取水印放置位置
//判斷FileUpload裡是否有文件地址
if (FileUpload1.HasFile)
{
if ((FileUpload1.PostedFile.ContentType == "image/pjpeg") || (FileUpload1.PostedFile.ContentType == "image/jpeg") || (FileUpload1.PostedFile.ContentType == "image/gif") || (FileUpload1.PostedFile.ContentType == "image/bmp") || (FileUpload1.PostedFile.ContentType == "image/x-png") || (FileUpload1.PostedFile.ContentType == "image/png"))//獲取客戶端發送的文件的MIME內容類型
{
//上傳文件總大小
int fileLength = 0;
fileLength = fileLength + FileUpload1.PostedFile.ContentLength;
//大小不能超過maxLengthk
int maxLength = 2048;
int sysLength = maxLength * 1024;
if (fileLength > sysLength)
{
Response.Write("<script>alert(''''該圖片大小超過2M限制'''')</script>");
}
else
{
string[] strSpil = FileUpload1.FileName.Split(''''.'''');//將此地址用.號進行分割(img/1.jpg)
string strEnd = strSpil[strSpil.Length - 1].ToLower();//得到後面的("jpg", "gif", "bmp", "png","jpeg","JPG","GIF","BMP","PNG","JPEG")
string[] strPic = new string[] { "jpg", "gif", "bmp", "png", "jpeg", "JPG", "GIF", "BMP", "PNG", "JPEG" };//定義一個數組裡面放文件格式
List<string> arry = new List<string>();
arry.AddRange(strPic); //定義一個可變數組,用於放文件格式
if (arry.Contains(strEnd))//判斷這個數組中是否有("jpg", "gif", "bmp", "png","jpeg","JPG","GIF","BMP","PNG","JPEG")
{
Random rand = new Random();//定義一個隨機數,為了防止你要上傳得圖片重名
string strName = DateTime.Now.ToString("yyyymmmddhhss") + rand.Next(100, 9999).ToString();//得到不同得名字
string strPointEnd = "." + strEnd;
string strFile = Server.MapPath("~/upfile");//獲取其相對地址
FileUpload1.SaveAs(strFile + "/" + strName + strPointEnd);//保存原始圖片
string src = strFile + "/" + strName + strPointEnd;
//進行水印添加處理--------------------------------------------
//水印圖片
string shuiyin = "~/watermark/logo.png";
//加載文件
System.Drawing.Image Cover;
Cover = System.Drawing.Image.FromFile(src);
//加載水印文件
System.Drawing.Image water = System.Drawing.Image.FromFile(Request.MapPath(shuiyin));
//創建畫布
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(Cover);
if (location == 1)//左上方
{
//在image上繪制水印
g.DrawImage(water, new Rectangle(0, 0, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel);
}
else if (location == 2)//左下方
{
//在image上繪制水印
g.DrawImage(water, new Rectangle(0, Cover.Height - water.Height, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel);
}
else if (location == 3)//右上方
{
//在image上繪制水印
g.DrawImage(water, new Rectangle(Cover.Width - water.Width, 0, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel);
}
else if (location == 4)//右下方
{
//在image上繪制水印
g.DrawImage(water, new Rectangle(Cover.Width - water.Width, Cover.Height - water.Height, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel);
}
else if (location == 5)//正中間
{
//在image上繪制水印
g.DrawImage(water, new Rectangle((Cover.Width - water.Width) / 2, (Cover.Height - water.Height) / 2, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel);
}
else
{
//在image上繪制水印
g.DrawImage(water, new Rectangle(Cover.Width - water.Width, Cover.Height - water.Height, water.Width, water.Height), 0, 0, water.Width, water.Height, GraphicsUnit.Pixel);
}
//釋放畫布
g.Dispose();
//釋放水印圖片
water.Dispose();
Cover.Save(HttpContext.Current.Server.MapPath("~/upfile/") + strName+"s"+ "." + strEnd);//保存打過水印的圖片
Cover.Dispose();
string src1 = "upfile/" + strName + "s" + "." + strEnd;
Response.Write("<a href="+src1+">點擊查看水印圖片</a>");
}
else
{
Response.Write("<script>alert(''''該圖片格式不能上傳!'''')</script>");
}
}
}
else
{
Response.Write("<script>alert(''''該圖片格式不能上傳!'''')</script>");
}
}
else
{
Response.Write("<script>alert(''''請選擇圖片路徑!'''')</script>");
}
}