a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

c#圖片添加水印的實例代碼

2020-01-24 03:11:54
字體:
來源:轉載
供稿:網友
復制代碼 代碼如下:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ImageDrawing
{
 /// <summary>
 /// 圖片修改類,主要是用來保護圖片版權的
 /// </summary>
 public class ImageModification
 {
  #region "member fields"
  private string modifyImagePath=null;
  private string drawedImagePath=null;
  private int rightSpace;
  private int bottoamSpace;
  private int lucencyPercent=70;
  private string outPath=null;
  #endregion
  public ImageModification()
  {
  }
  #region "propertys"
  /// <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;}
  }
  //獲取或設置水印在修改圖片中距底部的高度
  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 "methods"
  /// <summary>
  /// 開始繪制水印
  /// </summary>
  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
 }
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品久久久久久久久 | 久久99国产一区二区三区 | 国产在线专区 | 欧美精品福利视频 | av一区二区三区四区 | 夜夜夜久久久 | 国产精品2区 | 国产精品成人久久久久 | 久久国产区 | 久久不卡日韩美女 | а天堂中文最新一区二区三区 | 国产精品伦理 | 久久精品99国产精品酒店日本 | 天天插天天操天天干 | 欧美成人影院 | 成人在线三级 | 欧美久久久久 | jjzz日本| 国产欧美精品区一区二区三区 | 全黄大全大色全免费大片 | 中国91视频| 亚洲人成网亚洲欧洲无码 | 天天亚洲| 羞羞av在线 | 国产成人午夜精品5599 | 国产一二三区在线播放 | 日韩av在线免费看 | 国产成人久久 | 精品国产第一国产综合精品 | 怡红院免费在线视频 | 91丁香 | 九九久久99 | 黄色在线免费观看 | 日韩成人免费电影 | 亚洲毛片在线 | 青青草久草在线 | 久久久91精品国产一区二区 | 成人免费视频视频在线观看 免费 | 精品久久久久久久久久久久久久 | 亚洲高清一区二区三区 | 国产98色在线 |