最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#圖片添加水印的實(shí)例代碼

 更新時(shí)間:2013年07月23日 10:10:11   作者:  
這篇文章介紹了c#圖片添加水印的實(shí)例代碼,有需要的朋友可以參考一下
復(fù)制代碼 代碼如下:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ImageDrawing
{
 /// <summary>
 /// 圖片修改類,主要是用來保護(hù)圖片版權(quán)的
 /// </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>
  /// 獲取或設(shè)置要修改的圖像路徑
  /// </summary>
  public string ModifyImagePath
  {
   get{return this.modifyImagePath;}
   set{this.modifyImagePath=value;}
  }
  /// <summary>
  /// 獲取或設(shè)置在畫的圖片路徑(水印圖片)
  /// </summary>
  public string DrawedImagePath
  {
   get{return this.drawedImagePath;}
   set{this.drawedImagePath=value;}
  }
  /// <summary>
  /// 獲取或設(shè)置水印在修改圖片中的右邊距
  /// </summary>
  public int RightSpace
  {
   get{return this.rightSpace;}
   set{this.rightSpace=value;}
  }
  //獲取或設(shè)置水印在修改圖片中距底部的高度
  public int BottoamSpace
  {
   get{return this.bottoamSpace;}
   set{this.bottoamSpace=value;}
  }
  /// <summary>
  /// 獲取或設(shè)置要繪制水印的透明度,注意是原來圖片透明度的百分比
  /// </summary>
  public int LucencyPercent
  {
   get{return this.lucencyPercent;}
   set
   {
    if(value>=0&&value<=100)
     this.lucencyPercent=value;
   }
  }
  /// <summary>
  /// 獲取或設(shè)置要輸出圖像的路徑
  /// </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
   {   
    //建立圖形對(duì)象
    modifyImage=Image.FromFile(this.ModifyImagePath);
    drawedImage=Image.FromFile(this.DrawedImagePath);
    g=Graphics.FromImage(modifyImage);
    //獲取要繪制圖形坐標(biāo)
    int x=modifyImage.Width-this.rightSpace;
    int y=modifyImage.Height-this.BottoamSpace;
    //設(shè)置顏色矩陣
    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
 }
}

相關(guān)文章

  • C# 創(chuàng)建控制臺(tái)應(yīng)用程序

    C# 創(chuàng)建控制臺(tái)應(yīng)用程序

    這篇文章主要介紹了C# 創(chuàng)建控制臺(tái)應(yīng)用程序,在學(xué)習(xí)C#語言的時(shí)候,首先要學(xué)習(xí)控制臺(tái)的應(yīng)用程序,這樣才能專注于語言的學(xué)習(xí),減少學(xué)習(xí)的梯度,也有利于輸出自己需要輸出的內(nèi)容,一定要先使用控制臺(tái)的應(yīng)用程序的方式,下面就和小編一起學(xué)習(xí)該內(nèi)容吧
    2021-10-10
  • Unity延時(shí)執(zhí)行的多種方法小結(jié)

    Unity延時(shí)執(zhí)行的多種方法小結(jié)

    本文主要介紹了4種延時(shí)執(zhí)行的方法,主要包括Update計(jì)時(shí)器,Invoke,協(xié)程,DoTween,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法

    DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法

    這篇文章主要介紹了DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法,在項(xiàng)目開發(fā)中有應(yīng)用價(jià)值,需要的朋友可以參考下
    2014-08-08
  • WPF實(shí)現(xiàn)自定義Panel面板的示例詳解

    WPF實(shí)現(xiàn)自定義Panel面板的示例詳解

    WPF中的Panel(面板),是繼承自FrameworkElement的抽象類,表示一個(gè)可以用來排列子元素的面板,本文主要來和大家聊聊WPF如何實(shí)現(xiàn)自定義Panel,感興趣的可以了解下
    2023-09-09
  • C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源

    C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源

    本文主要介紹了C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕

    C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕

    這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • C#嵌套類的訪問方法

    C#嵌套類的訪問方法

    這篇文章主要介紹了C#嵌套類的訪問方法,本文給出了嵌套類代碼和訪問方法代碼,不會(huì)的同學(xué)照搬對(duì)照中的方法即可,需要的朋友可以參考下
    2015-04-04
  • C#實(shí)現(xiàn)簡單的五子棋游戲

    C#實(shí)現(xiàn)簡單的五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡單的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • C#手動(dòng)操作DataGridView使用各種數(shù)據(jù)源填充表格實(shí)例

    C#手動(dòng)操作DataGridView使用各種數(shù)據(jù)源填充表格實(shí)例

    本文主要介紹了C#手動(dòng)操作DataGridView使用各種數(shù)據(jù)源填充表格實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • C#中使用Spire.XLS來操作Excel數(shù)據(jù)的實(shí)現(xiàn)

    C#中使用Spire.XLS來操作Excel數(shù)據(jù)的實(shí)現(xiàn)

    本文主要介紹了C#中使用Spire.XLS來操作Excel數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評(píng)論

岳池县| 上林县| 昆山市| 惠水县| 古浪县| 丹凤县| 汾西县| 隆昌县| 永泰县| 修水县| 玉溪市| 昌乐县| 揭东县| 瑞安市| 耒阳市| 夏河县| 湖北省| 新河县| 莱阳市| 新泰市| 武胜县| 永川市| 永新县| 伊金霍洛旗| 新宁县| 府谷县| 鲜城| 昌平区| 贡觉县| 通化市| 德令哈市| 界首市| 兴安县| 监利县| 平和县| 杭锦后旗| 驻马店市| 郧西县| 安岳县| 桐庐县| 苍梧县|