问题描述
asp.net在图片上传的时候怎么给上传的图片在右下角添加图片水印,希望有具体的的实现步骤和可行的代码实例
解决方案
解决方案二:
解决方案三:
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Drawing;usingSystem.IO;usingSystem.Drawing.Imaging;usingSystem.Web;usingSystem.Drawing.Drawing2D;usingSystem.Reflection;namespaceChen{publicclasswarterPic{///<summary>///给图片上水印///</summary>///<paramname="filepath">原图片地址</param>///<paramname="waterfile">水印图片地址</param>///publicvoidmarkwater(stringfilepath,stringwaterfile){//gif不水印inti=filepath.LastIndexOf(".");stringex=filepath.Substring(i,filepath.Length-i);if(string.Compare(ex,".gif",true)==0){return;}stringmodifyimagepath=filepath;//修改的图像路径intlucencypercent=25;Imagemodifyimage=null;Imagedrawedimage=null;Graphicsg=null;try{//建立图形对象modifyimage=Image.FromFile(modifyimagepath,true);drawedimage=Image.FromFile(waterfile,true);g=Graphics.FromImage(modifyimage);//获取要绘制图形坐标intx=modifyimage.Width-drawedimage.Width;inty=modifyimage.Height-drawedimage.Height;//设置颜色矩阵float[][]matrixitems={newfloat[]{1,0,0,0,0},newfloat[]{0,1,0,0,0},newfloat[]{0,0,1,0,0},newfloat[]{0,0,0,(float)lucencypercent/100f,0},newfloat[]{0,0,0,0,1}};ColorMatrixcolormatrix=newColorMatrix(matrixitems);ImageAttributesimgattr=newImageAttributes();imgattr.SetColorMatrix(colormatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);//绘制阴影图像g.DrawImage(drawedimage,newRectangle(x,y,drawedimage.Width,drawedimage.Height),10,10,drawedimage.Width,drawedimage.Height,GraphicsUnit.Pixel,imgattr);//保存文件string[]allowimagetype={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};FileInfofi=newFileInfo(modifyimagepath);ImageFormatimagetype=ImageFormat.Gif;switch(fi.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;}MemoryStreamms=newMemoryStream();modifyimage.Save(ms,imagetype);byte[]imgdata=ms.ToArray();modifyimage.Dispose();drawedimage.Dispose();g.Dispose();FileStreamfs=null;//File.Delete(modifyimagepath);fs=newFileStream(modifyimagepath,FileMode.Create,FileAccess.Write);if(fs!=null){fs.Write(imgdata,0,imgdata.Length);fs.Close();}}finally{try{drawedimage.Dispose();modifyimage.Dispose();g.Dispose();}catch{}}}}}
解决方案四:
网上很多代码ASP.NET:Adding'Watermark'toimagesonthefly
解决方案五:
asp.net图片上传时添加水印图片示例//设置水印文字//<paramname="path">要设置水印图片的路径</param>//<paramname="str">水印文字</param>//<paramname="i">水印文位置</param>publicvoidWaterLetter(stringpath,stringstr,inti)//i表示文字位置0:左上角1:右上角2:左下角3:右下角4:居中{intFwidth;intFheight;//获取文件扩展名stringextension=Path.GetExtension(path).ToUpper();//设置临时文件名称stringfileName=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();//加文字水印System.Drawing.Imageimage=System.Drawing.Image.FromFile(path);Graphicsg=Graphics.FromImage(image);g.DrawImage(image,0,0,image.Width,image.Height);Fontf=newFont("Verdana",18);Brushb=newSolidBrush(Color.Red);SizeFXMaxSize=g.MeasureString(str,f);Fwidth=(int)XMaxSize.Width;Fheight=(int)XMaxSize.Height;switch(i){case0://左上角g.DrawString(str,f,b,10,10);break;case1://右上角g.DrawString(str,f,b,(int)(image.Width-Fwidth),10);break;case2://左下角g.DrawString(str,f,b,10,(int)(image.Height-Fheight)-10);break;case3://右下角g.DrawString(str,f,b,(int)(image.Width-Fwidth),(int)(image.Height-Fheight)-10);break;case4://居中g.DrawString(str,f,b,(int)(image.Width-Fwidth)/2,(int)(image.Height-Fheight)/2);break;}//codego.net/15/1/1/g.Dispose();//保存加水印过后的图片,删除原始图片stringnewPath=HttpContext.Current.Server.MapPath(".")+"/File/temp"+fileName+"_new"+extension;image.Save(newPath);image.Dispose();//将水印图片替换为原来图片File.Copy(newPath,path,true);//删除水印if(File.Exists(newPath)){File.Delete(newPath);}}