亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2016年07月25日 15:20:16   作者:熊哥  
這篇文章主要為大家詳細介紹了C#.NET 圖片水印添加代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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 構(gòu)造器
  /// <summary>
  /// 構(gòu)造函數(shù)
  /// </summary>
  public Watermark() { }
  #endregion

  #region 屬性
  /// <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;}
 }
 /// <summary>
  /// 獲取或設(shè)置水印在修改圖片中距底部的高度
 /// </summary>
 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 開始繪制水印 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 ={//設(shè)置顏色矩陣
   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
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Unity實現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量

    Unity實現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • WCF實現(xiàn)雙向通信

    WCF實現(xiàn)雙向通信

    這篇文章介紹了WCF實現(xiàn)雙向通信的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • C#(4.0)不常見的語法

    C#(4.0)不常見的語法

    今天工作遇到4.0新增語法 不解 請教 遂知,簡單的整理下,需要的朋友可以參考下
    2012-02-02
  • C#超市收銀系統(tǒng)設(shè)計

    C#超市收銀系統(tǒng)設(shè)計

    這篇文章主要為大家詳細介紹了C#超市收銀系統(tǒng)設(shè)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#如何給PDF文件添加水印

    C#如何給PDF文件添加水印

    這篇文章主要為大家詳細介紹了C#如何給PDF文件添加水印的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Unity3D實現(xiàn)控制攝像機移動

    Unity3D實現(xiàn)控制攝像機移動

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)控制攝像機移動 ,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#函數(shù)式程序設(shè)計之用閉包封裝數(shù)據(jù)的實現(xiàn)代碼

    C#函數(shù)式程序設(shè)計之用閉包封裝數(shù)據(jù)的實現(xiàn)代碼

    如果一個程序設(shè)計語言能夠用高階函數(shù)解決問題,則意味著數(shù)據(jù)作用域問題已十分突出。當函數(shù)可以當成參數(shù)和返回值在函數(shù)之間進行傳遞時,編譯器利用閉包擴展變量的作用域,以保證隨時能得到所需要的數(shù)據(jù)
    2014-03-03
  • c# 實現(xiàn)位圖算法(BitMap)

    c# 實現(xiàn)位圖算法(BitMap)

    這篇文章主要介紹了c# 如何實現(xiàn)位圖算法(BitMap),文中講解非常細致,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • C#串口接收程序的實現(xiàn)

    C#串口接收程序的實現(xiàn)

    本文主要介紹了C#串口接收程序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-05-05
  • C#使用Interlocked實現(xiàn)線程同步

    C#使用Interlocked實現(xiàn)線程同步

    今天小編就為大家分享一篇關(guān)于C#使用Interlocked實現(xiàn)線程同步,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10

最新評論