当前位置: 编程技术>.net/c#/asp.net
C# 添加图片水印的代码示例
来源: 互联网 发布时间:2014-08-30
本文导语: 代码如下: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing.Drawing2D; using System.Drawing; using System.Drawing.Imaging; namespace MyWaterMark { /// /// WaterMark 的摘要说明 /// /// edit by www. ...
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Drawing.Imaging;
namespace MyWaterMark
{
///
/// WaterMark 的摘要说明
///
/// edit by www.
/// 要加入的文字
/// 要加入的文字颜色
/// 水印图片路径
/// 要加水印的图片路径
/// 处理后的图片路径
/// 水印在修改图片中距左边的宽度
/// 水印在修改图片中距底部的高度
/// 水印图片的透明度
/// 文字
/// 文字
/// 文字
/// 是否显示文字
/// 是否显示水印图片
public class WaterMark
{
#region param
private string strCopyright, strMarkPath, strPhotoPath, strSavePath;
private int iMarkRightSpace, iMarkButtomSpace, iDiaphaneity;
private int iFontRightSpace = 0, iFontButtomSpace = 0, iFontDiaphaneity = 80;
private int iFontSize = 10;
private bool bShowCopyright = true, bShowMarkImage = true;
private int red = 0, greed = 0, blue = 0;
Image imageBit=null;
#endregion
#region WaterMark
public WaterMark()
{
this.strCopyright = "";
this.strMarkPath = null;
this.strPhotoPath = null;
this.strSavePath = null;
this.iDiaphaneity = 70;
this.iMarkRightSpace = 0;
this.iMarkButtomSpace = 0;
}
///
/// 主要用两样都加的
///
public WaterMark(string copyright, string copyrightColor, string markPath, string photoPath,Image imageBitT, string savePath,int apha)
{
this.strCopyright = copyright;
this.strMarkPath = markPath;
this.strPhotoPath = photoPath;
this.strSavePath = savePath;
this.iDiaphaneity = apha;
this.iMarkRightSpace = 0;
this.iMarkButtomSpace = 0;
this.imageBit = imageBitT;
try
{
Color my = new Color();
if (copyrightColor.IndexOf("#") >= 0)
{
my = ColorTranslator.FromHtml(copyrightColor);
}
else
{
my = Color.FromName(copyrightColor);
}
this.red = my.R;
this.greed = my.G;
this.blue = my.B;
}
catch (Exception) { }
}
#endregion
#region property
///
/// 设置是否显示水印文字
///
public bool ShowCopyright
{
set { this.bShowCopyright = value; }
}
///
/// 设置是否显示水印图片
///
public bool ShowMarkImage
{
set { this.bShowMarkImage = value; }
}
///
/// 获取或设置要加入的文字
///
public string Copyright
{
set { this.strCopyright = value; }
}
///
/// 设置要加入的文字颜色
///
public bool strCopyrightColor
{
set
{
Color my = new Color();
if (value.ToString().IndexOf("#") >= 0)
{
my = ColorTranslator.FromHtml(value.ToString());
}
else
{
my = Color.FromName(value.ToString());
}
this.red = my.R;
this.greed = my.G;
this.blue = my.B;
}
}
///
/// 获取或设置加水印后的图片路径
///
public string SavePath
{
get { return this.strSavePath; }
set { this.strSavePath = value; }
}
///
/// 获取或设置水印图片路径
///
public string MarkPath
{
get { return this.strMarkPath; }
set { this.strMarkPath = value; }
}
///
/// 获取或设置要加水印图片的路径
///
public string PhotoPath
{
get { return this.strPhotoPath; }
set { this.strPhotoPath = value; }
}
///
/// 设置水印图片的透明度
///
public int Diaphaneity
{
set
{
if (value > 0 && value = 0 && value