国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#.NET 圖片水印添加代碼

2019-10-29 21:23:51
字體:
來源:轉載
供稿:網友

本文實例為大家分享了C#.NET添加 圖片水印的方法,供大家參考,具體內容如下

using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;namespace Pub.Class{ /// <summary> /// 添加水印類 只支持添加圖片水印 /// </summary> public class Watermark {  #region 私有成員  private string modifyImagePath=null; private string drawedImagePath=null; private int rightSpace; private int bottoamSpace; private int lucencyPercent=70; private string outPath=null;  #endregion  #region 構造器  /// <summary>  /// 構造函數  /// </summary>  public Watermark() { }  #endregion  #region 屬性  /// <summary> /// 獲取或設置要修改的圖像路徑 /// </summary> public string ModifyImagePath { get{return this.modifyImagePath;} set{this.modifyImagePath=value;} } /// <summary> /// 獲取或設置在畫的圖片路徑(水印圖片) /// </summary> public string DrawedImagePath { get{return this.drawedImagePath;} set{this.drawedImagePath=value;} } /// <summary> /// 獲取或設置水印在修改圖片中的右邊距 /// </summary> public int RightSpace { get{return this.rightSpace;} set{this.rightSpace=value;} } /// <summary>  /// 獲取或設置水印在修改圖片中距底部的高度 /// </summary> public int BottoamSpace { get{return this.bottoamSpace;} set{this.bottoamSpace=value;} } /// <summary> /// 獲取或設置要繪制水印的透明度,注意是原來圖片透明度的百分比 /// </summary> public int LucencyPercent { get{return this.lucencyPercent;} set { if(value>=0&&value<=100) this.lucencyPercent=value; } } /// <summary> /// 獲取或設置要輸出圖像的路徑 /// </summary> public string OutPath { get{return this.outPath;} set{this.outPath=value;}  }  #endregion  #region 開始繪制水印 DrawImage  /// <summary> /// 開始繪制水印 /// </summary>  /// <example>  /// <code>  ///  Watermark wm = new Watermark();  ///  wm.DrawedImagePath= Server.MapPath("") + "/upfile/" + "backlogo.gif";  ///  wm.ModifyImagePath=path;   ///  wm.RightSpace=184;  ///  wm.BottoamSpace=81;  ///  wm.LucencyPercent=50;  ///  wm.OutPath=Server.MapPath("") + "/upfile/" + fileName + "_new" + extension;  ///  wm.DrawImage();  ///    ///  //保存加水印過后的圖片,刪除原始圖片   ///  mFileName=fileName + "_new" + extension;  ///  if(File.Exists(path)) { File.Delete(path); }   /// </code>  /// </example> public void DrawImage() { Image modifyImage=null; Image drawedImage=null; Graphics g=null; try {  modifyImage=Image.FromFile(this.ModifyImagePath);//建立圖形對象 drawedImage=Image.FromFile(this.DrawedImagePath); g=Graphics.FromImage(modifyImage);  int x=modifyImage.Width-this.rightSpace;//獲取要繪制圖形坐標 int y=modifyImage.Height-this.BottoamSpace;  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)this.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),  0,0,drawedImage.Width,drawedImage.Height,  GraphicsUnit.Pixel,imgAttr);  string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};//保存文件 FileInfo file=new FileInfo(this.ModifyImagePath); ImageFormat imageType=ImageFormat.Gif; switch(file.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; if(this.OutPath==null || this.OutPath=="") {  File.Delete(this.ModifyImagePath);  fs=new FileStream(this.ModifyImagePath,FileMode.Create,FileAccess.Write); } else {  fs=new FileStream(this.OutPath,FileMode.Create,FileAccess.Write); } if(fs!=null) {  fs.Write(imgData,0,imgData.Length);  fs.Close(); } } finally { try {  drawedImage.Dispose();  modifyImage.Dispose();  g.Dispose(); } catch{} } }  #endregion }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 徐汇区| 灵石县| 海门市| 武隆县| 望奎县| 大化| 晋中市| 永德县| 农安县| 延安市| 嵊泗县| 垦利县| 义马市| 观塘区| 沭阳县| 芮城县| 蛟河市| 南江县| 大丰市| 昭通市| 临西县| 阳西县| 江安县| 故城县| 临安市| 沈丘县| 兴文县| 郑州市| 广水市| 荆州市| 灵川县| 曲松县| 弥渡县| 宣威市| 巴南区| 保亭| 榆社县| 合肥市| 衡水市| 海丰县| 那坡县|