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

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)文章

  • 基于C#調(diào)用c++Dll結(jié)構(gòu)體數(shù)組指針的問題詳解

    基于C#調(diào)用c++Dll結(jié)構(gòu)體數(shù)組指針的問題詳解

    下面小編就為大家分享一篇基于C#調(diào)用c++Dll結(jié)構(gòu)體數(shù)組指針的問題詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • C#實現(xiàn)快速查詢文件的方法

    C#實現(xiàn)快速查詢文件的方法

    這篇文章介紹了C#實現(xiàn)快速查詢文件的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#操作Word打印的示例

    C#操作Word打印的示例

    這篇文章主要介紹了C#操作Word打印的示例,幫助大家利用c#打印文件,提高辦公效率,感興趣的朋友可以了解下
    2020-10-10
  • 詳解Unity使用ParticleSystem粒子系統(tǒng)模擬藥水在血管中流動(粒子碰撞)

    詳解Unity使用ParticleSystem粒子系統(tǒng)模擬藥水在血管中流動(粒子碰撞)

    這篇文章主要介紹了Unity使用ParticleSystem粒子系統(tǒng)模擬藥水在血管中流動(粒子碰撞),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-05-05
  • rsa加密算法使用示例分享

    rsa加密算法使用示例分享

    這篇文章主要介紹了rsa加密算法使用示例,代碼中有注釋,大家參考使用吧
    2014-01-01
  • C#網(wǎng)絡(luò)編程中常用特性介紹

    C#網(wǎng)絡(luò)編程中常用特性介紹

    這篇文章介紹了C#網(wǎng)絡(luò)編程中常用特性,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • 淺談C#索引器

    淺談C#索引器

    這篇文章主要簡單介紹C#索引器,索引器使你可從語法上方便地創(chuàng)建類、結(jié)構(gòu)或接口,以便客戶端應(yīng)用程序可以像訪問數(shù)組一樣訪問它們。編譯器將生成一個 Item 屬性和適當?shù)脑L問器方法,在主要目標是封裝內(nèi)部集合或數(shù)組的類型中,常常要實現(xiàn)索引器,下面我們一起來看看具體內(nèi)容吧
    2021-11-11
  • C#實現(xiàn)TIF圖像轉(zhuǎn)PDF文件的方法

    C#實現(xiàn)TIF圖像轉(zhuǎn)PDF文件的方法

    這篇文章主要介紹了C#實現(xiàn)TIF圖像轉(zhuǎn)PDF文件的方法,涉及C#使用TIFtoPDF工具實現(xiàn)pdf文件轉(zhuǎn)換的技巧,需要的朋友可以參考下
    2015-07-07
  • 深入淺析Restful接口的兩種使用方式

    深入淺析Restful接口的兩種使用方式

    restful接口常用的兩種方式是get和post.接下來通過本文給大家介紹Restful接口的兩種使用方式,本文給大家介紹的非常詳細,需要的朋友參考下吧
    2018-09-09
  • Unity實現(xiàn)圖片生成灰白圖的方法

    Unity實現(xiàn)圖片生成灰白圖的方法

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)圖片生成灰白圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04

最新評論

宽甸| 平昌县| 鄂尔多斯市| 石首市| 青神县| 扶余县| 洪洞县| 兴海县| 千阳县| 新宁县| 东乡| 施秉县| 手游| 岳西县| 上林县| 临泽县| 德清县| 霍城县| 金华市| 临汾市| 山西省| 台东市| 安吉县| 大庆市| 开封市| 乐清市| 巴青县| 贵德县| 平武县| 全州县| 淳安县| 洛宁县| 昌黎县| 富源县| 洱源县| 清远市| 信丰县| 宝山区| 九江县| 松溪县| 廊坊市|