溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#.NET 添加圖片水印

發(fā)布時(shí)間:2020-06-30 07:33:31 來源:網(wǎng)絡(luò) 閱讀:446 作者:hbloushi 欄目:編程語言
using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;namespace Pub.Class{    ///<summary>/// 添加水印類 只支持添加圖片水印    ///</summary>publicclass Watermark    {        #region 私有成員
        privatestring modifyImagePath=null;  privatestring drawedImagePath=null;  privateint rightSpace;  privateint bottoamSpace;  privateint lucencyPercent=70;  privatestring outPath=null;        #endregion#region 構(gòu)造器
        ///<summary>/// 構(gòu)造函數(shù)        ///</summary>public Watermark() { }        #endregion#region 屬性
        ///<summary>/// 獲取或設(shè)置要修改的圖像路徑  ///</summary>publicstring ModifyImagePath  {   get{returnthis.modifyImagePath;}   set{this.modifyImagePath=value;}  }  ///<summary>/// 獲取或設(shè)置在畫的圖片路徑(水印圖片)  ///</summary>publicstring DrawedImagePath  {   get{returnthis.drawedImagePath;}   set{this.drawedImagePath=value;}  }  ///<summary>/// 獲取或設(shè)置水印在修改圖片中的右邊距  ///</summary>publicint RightSpace  {   get{returnthis.rightSpace;}   set{this.rightSpace=value;}  }  ///<summary>/// 獲取或設(shè)置水印在修改圖片中距底部的高度  ///</summary>publicint BottoamSpace  {   get{returnthis.bottoamSpace;}   set{this.bottoamSpace=value;}  }  ///<summary>/// 獲取或設(shè)置要繪制水印的透明度,注意是原來圖片透明度的百分比  ///</summary>publicint LucencyPercent  {   get{returnthis.lucencyPercent;}   set { if(value>=0&&value<=100) this.lucencyPercent=value; }  }  ///<summary>/// 獲取或設(shè)置要輸出圖像的路徑  ///</summary>publicstring OutPath  {   get{returnthis.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>publicvoid DrawImage()  {   Image modifyImage=null;   Image drawedImage=null;   Graphics g=null;   try {     modifyImage=Image.FromFile(this.ModifyImagePath);//建立圖形對(duì)象
    drawedImage=Image.FromFile(this.DrawedImagePath);    g=Graphics.FromImage(modifyImage);        int x=modifyImage.Width-this.rightSpace;//獲取要繪制圖形坐標(biāo)int y=modifyImage.Height-this.BottoamSpace;        float[][] matrixItems ={//設(shè)置顏色矩陣newfloat[] {1, 0, 0, 0, 0},            newfloat[] {0, 1, 0, 0, 0},            newfloat[] {0, 0, 1, 0, 0},            newfloat[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},            newfloat[] {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 p_w_picpathType=ImageFormat.Gif;    switch(file.Extension.ToLower()) {     case".jpg": p_w_picpathType=ImageFormat.Jpeg; break;     case".gif": p_w_picpathType=ImageFormat.Gif; break;     case".png": p_w_picpathType=ImageFormat.Png; break;     case".bmp": p_w_picpathType=ImageFormat.Bmp; break;     case".tif": p_w_picpathType=ImageFormat.Tiff; break;     case".wmf": p_w_picpathType=ImageFormat.Wmf; break;     case".ico": p_w_picpathType=ImageFormat.Icon; break;     default: break;    }    MemoryStream ms=new MemoryStream();    modifyImage.Save(ms,p_w_picpathType);    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    }}
 
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI