ASP.NET(C#)上传图片加上文字或图片水印 using System; using System.Web; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Reflection; namespace Chris { ////// 文件上传类,重载了上传,缩略图,水印 /// public class water { private string BasePath; ////// 无参构造,默认为程序运行根目录 /// public water() { } ////// 给图片上水印 /// /// 原图片地址 /// 水印图片地址 public void MarkWater(string filePath,string waterFile) { //GIF不水印 int i = filePath.LastIndexOf("."); string ex = filePath.Substring(i,filePath.Length - i); if(string.Compare(ex,".gif",true) == 0) { return; } string ModifyImagePath = BasePath + filePath;//修改的图像路径 int lucencyPercent=25; Image modifyImage=null; Image drawedImage=null; Graphics g=null; try { //建立图形对象 modifyImage=Image.FromFile(ModifyImagePath,true); drawedImage=Image.FromFile(BasePath + waterFile,true); g=Graphics.FromImage(modifyImage); //获取要绘制图形坐标 int x=modifyImage.Width-drawedImage.Width; int y=modifyImage.Height-drawedImage.Height; //设置颜色矩阵 float[][] matrixItems ={ new float[] {1, 0, 0, 0, 0}, new float[] {0, 1, 0, 0, 0}, new float[] {0, 0, 1, 0, 0}, new float[] {0, 0, 0, (float)lucencyPercent/100f, 0}, new float[] {0, 0, 0, 0, 1}}; ColorMatrix colorMatrix = new ColorMatrix(matrixItems); ImageAttributes imgAttr=new ImageAttributes(); imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap); //绘制阴影图像 g.DrawImage(drawedImage,new Rectangle(x,y,drawedImage.Width,drawedImage.Height),10,10,drawedImage.Width,drawedImage.Height,GraphicsUnit.Pixel,imgAttr); //保存文件 string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"}; FileInfo fi=new FileInfo(ModifyImagePath); ImageFormat imageType=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; } MemoryStream ms=new MemoryStream(); modifyImage.Save(ms,imageType); byte[] imgData=ms.ToArray(); modifyImage.Dispose(); drawedImage.Dispose(); g.Dispose(); FileStream fs=null; File.Delete(ModifyImagePath); fs=new FileStream(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{;} } } } } 另外来个upload.asp文件 <% Function GetFileName(ByVal strFile) If strFile <> "" Then GetFileName = mid(strFile,InStrRev(strFile, "")+1) Else GetFileName = "" End If End function strFileName = Request.Form("file1") Set objStream = Server.CreateObject("ADODB.Stream") objStream.Type = 1 ' adTypeBinary objStream.Open objStream.LoadFromFile strFileName objStream.SaveToFile Server.MapPath(GetFileName(strFileName)),2 objStream.Close %> upload.htm文件
ASP.NET(C#)上传图片加上文字或图片水印
最新推荐文章于 2021-11-09 11:48:42 发布