网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#.NET添加图片水印

using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.IO;namespace Pub.Class{    ////// 添加水印类 只支持添加图片水印    ///publicclass Watermark    {        #region 私有成员
        privatestring modifyImagePath=null;  privatestring drawedImagePath=null;  privateint rightSpace;  privateint bottoamSpace;  privateint lucencyPercent=70;  privatestring outPath=null;        #endregion#region 构造器
        ////// 构造函数        ///public Watermark() { }        #endregion#region 属性
        ////// 获取或设置要修改的图像路径  ///publicstring ModifyImagePath  {   get{returnthis.modifyImagePath;}   set{this.modifyImagePath=value;}  }  ////// 获取或设置在画的图片路径(水印图片)  ///publicstring DrawedImagePath  {   get{returnthis.drawedImagePath;}   set{this.drawedImagePath=value;}  }  ////// 获取或设置水印在修改图片中的右边距  ///publicint RightSpace  {   get{returnthis.rightSpace;}   set{this.rightSpace=value;}  }  ////// 获取或设置水印在修改图片中距底部的高度  ///publicint BottoamSpace  {   get{returnthis.bottoamSpace;}   set{this.bottoamSpace=value;}  }  ////// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比  ///publicint LucencyPercent  {   get{returnthis.lucencyPercent;}   set { if(value>=0&&value<=100) this.lucencyPercent=value; }  }  ////// 获取或设置要输出图像的路径  ///publicstring OutPath  {   get{returnthis.outPath;}   set{this.outPath=value;}        }        #endregion#region 开始绘制水印 DrawImage
        ////// 开始绘制水印  ////////////     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); }         //////publicvoid 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 ={//设置颜色矩阵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    }}
 

本文题目:C#.NET添加图片水印
当前地址:http://bjjierui.cn/article/jdchop.html

其他资讯