问题描述
想给网站上图片加水印。只需要给有些图片加水印。没有上传图片的功能。求代码,,,方法
解决方案
解决方案二:
文字水印百度搜一下吧就几句代码很简单的
解决方案三:
引用1楼longlong881129的回复:
文字水印百度搜一下吧就几句代码很简单的
我很菜的
解决方案四:
///<summary>///添加水印方法(JPG图片)///</summary>///<paramname="filepath">原始图片路径</param>///<parmname="path">水印图片存储路径</param>stringaddshuiyin(stringfilepath,stringpath){System.Drawing.Imageimage=System.Drawing.Image.FromFile(filepath);stringcopypath=Server.MapPath("/manager/skin/default/watermark.gif");System.Drawing.ImagecopyImage=System.Drawing.Image.FromFile(copypath);Graphicsg=Graphics.FromImage(image);Rectanglerect=newRectangle(image.Width-copyImage.Width,image.Height-copyImage.Height,copyImage.Width,copyImage.Height);g.DrawImage(copyImage,rect,0,0,copyImage.Width,copyImage.Height,GraphicsUnit.Pixel);g.Dispose();stringfilename=Guid.NewGuid().ToString()+".jpg";stringserverPath=Server.MapPath(path);image.Save(serverPath+filename);image.Dispose();returnpath+filename;}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zx75991/archive/2011/02/17/6190991.aspx
解决方案五:
前台界面<%@PageLanguage="C#"CodeFile="Default.aspx.cs"Inherits="_Default"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>Asp.net上传图片并添加水印</title><linkhref="css/yx.css"rel="stylesheet"type="text/css"/></head><body><formid="form1"runat="server"><divid="top">Asp.Net上传并添加水印DemoCodeby天赐淡雅香</div><divid="content">选择上传图片:<asp:FileUploadID="upFileTest"runat="server"/><br/><br/><br/><asp:RequiredFieldValidatorID="vUpFile"runat="server"ControlToValidate="upFileTest"ErrorMessage="请选择要上传的文件!"></asp:RequiredFieldValidator><br/><br/><br/><br/><asp:ButtonID="btnText"runat="server"OnClick="btnText_Click"Text="上传并添加文字水印"/> <br/><br/><br/><br/><asp:LabelID="lblStatus"runat="server"></asp:Label></div></form></body></html>
后台代码[code=C#]usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.IO;publicpartialclass_Default:System.Web.UI.Page{///<summary>///程序开发:天赐淡雅香///</summary>///<paramname="sender"></param>///<paramname="e"></param>protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){lblStatus.Visible=false;}}privatevoidShow(stringstr){Response.Write("<scriptlanguage='javascript'>alert('"+str+"')</script>");}privatevoidupLoad(){stringfullName=upFileTest.PostedFile.FileName;stringfileName=fullName.Substring(fullName.LastIndexOf("\")+1);stringmarks=fullName.Substring(fullName.LastIndexOf(".")+1);if(marks!="jpg"&&marks!="gif"&&marks!="JPG"&&marks!="GIF"){Show("上传的文件格式不正确,请重新选择!");}else{stringupFileName=Server.MapPath("upLoadFiles")+"\"+System.DateTime.Now.ToString("yyyyMMddhhmmss")+"."+marks;Session["filePath"]=upFileName;Session["marks"]=marks;upFileTest.PostedFile.SaveAs(upFileName);}}protectedvoidbtnText_Click(objectsender,EventArgse){upLoad();//开始加文字水印System.Drawing.Imageimg=System.Drawing.Image.FromFile(Session["filePath"].ToString());Graphicsg=Graphics.FromImage(img);g.DrawImage(img,0,0,img.Width,img.Height);Fontf=newFont("Verdana",16);Brushb=newSolidBrush(Color.Red);stringstr="天赐淡雅香";g.DrawString(str,f,b,10,10);g.Dispose();stringnewFilePath=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="水印绘制成功!";}}[/codePs,调用system.drawing.image.save方法,可能因为没有读写权限而报错,添加iis访问权限即可
解决方案六:
引用3楼zx75991的回复:
C#code///<summary>///添加水印方法(JPG图片)///</summary>///<paramname="filepath">原始图片路径</param>///<parmname="path">水印图片存储路径</param>stringaddshuiyin……
有个案例会更好