欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2013年07月23日 10:10:11   作者:  
這篇文章介紹了c#圖片添加水印的實例代碼,有需要的朋友可以參考一下
復(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
   {   
    //建立圖形對象
    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)文章

  • 使用VS2010 C#開發(fā)ActiveX控件(下),完整代碼打包下載

    使用VS2010 C#開發(fā)ActiveX控件(下),完整代碼打包下載

    我們介紹了開發(fā)、打包、發(fā)布、使用ActiveX控件的全過程。在演示程序中,我們沒有調(diào)用串口通信和讀卡器Dll程序,由于我們讀卡器的原始Dll是使用其它語言進(jìn)行開發(fā)的,對C#來說,是非托管代碼,因此我們還需要在代碼級別進(jìn)行非托管代碼的安全性設(shè)置
    2011-05-05
  • C#使用StreamWriter寫入文件的方法

    C#使用StreamWriter寫入文件的方法

    這篇文章主要介紹了C#使用StreamWriter寫入文件的方法,涉及C#中StreamWriter類操作文件的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • C#入門之結(jié)構(gòu)類型Struct

    C#入門之結(jié)構(gòu)類型Struct

    這篇文章介紹了C#入門之結(jié)構(gòu)類型Struct,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • c#字符串編碼編碼(encoding)使用方法示例

    c#字符串編碼編碼(encoding)使用方法示例

    System.Text提供了Encoding的抽象類,這個類提供字符串編碼的方法。使Unicode字符數(shù)組的字符串,轉(zhuǎn)換為指定編碼的字節(jié)數(shù)組,或者反之,看下面的例子
    2013-12-12
  • c# 獲取已安裝的打印機(jī)并調(diào)用打印文件

    c# 獲取已安裝的打印機(jī)并調(diào)用打印文件

    這篇文章主要介紹了c#如何獲取已安裝的打印機(jī)并調(diào)用打印文件,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-10-10
  • C# ConfigHelper 輔助類介紹

    C# ConfigHelper 輔助類介紹

    ConfigHelper(包含AppConfig和WebConfig), app.config和web.config的[appSettings]和[connectionStrings]節(jié)點進(jìn)行新增、修改、刪除和讀取相關(guān)的操作。
    2013-04-04
  • C#中Dictionary的作用及用法講解

    C#中Dictionary的作用及用法講解

    這篇文章主要介紹了C#中Dictionary的作用及用法講解,本文還對dictionary類用什么接口實現(xiàn)、Dictionary的基本用法做了講解,需要的朋友可以參考下
    2014-10-10
  • c#讀寫注冊表示例分享

    c#讀寫注冊表示例分享

    這篇文章主要介紹了c#讀寫注冊表示例,示例中有詳細(xì)注釋,大家參考使用吧
    2014-01-01
  • c#中Empty()和DefalutIfEmpty()用法分析

    c#中Empty()和DefalutIfEmpty()用法分析

    這篇文章主要介紹了c#中Empty()和DefalutIfEmpty()用法,以實例形式分析了針對不同情況下Empty()和DefalutIfEmpty()用法區(qū)別,需要的朋友可以參考下
    2014-11-11
  • C#文件上傳與下載的實現(xiàn)方法

    C#文件上傳與下載的實現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了C#文件上傳與下載的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評論