C#图片水印类
这个是学习用的呃,主要看一下水印在修改图片中距左边的宽度和高度是杂弄的就哦客了。
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ECHX.BLL
{
/// <summary>
/// WaterMark 的摘要说明
/// </summary>
///
/// <param name="strCopyright">要加入的文字</param>
/// <param name="strMarkPath">水印图片路径</param>
/// <param name="strPhotoPath">要加水印的图片路径</param>
/// <param name="strSavePath">处理后的图片路径</param>
/// <param name="iMarkRightSpace">水印在修改图片中距左边的宽度</param>
/// <param name="iMarkButtomSpace">水印在修改图片中距底部的高度</param>
/// <param name="iDiaphaneity">水印图片的透明度</param>
/// <param name="iFontRightSpace">文字</param>
/// <param name="iFontButtomSpace">文字</param>
/// <param name="iFontDiaphaneity">文字</param>
/// <param name="bShowCopyright">是否显示文字</param>
/// <param name="bShowMarkImage">是否显示水印图片</param> public class WaterMark
{
#region param
private string strCopyright, strMarkPath, strPhotoPath, strSavePath;
private int iMarkRightSpace, iMarkButtomSpace, iDiaphaneity;
private int iFontRightSpace = , iFontButtomSpace = , iFontDiaphaneity = ;
private int iFontSize = ;
private bool bShowCopyright = true, bShowMarkImage = true;
#endregion #region WaterMark
public WaterMark()
{
this.strCopyright = "";
this.strMarkPath = null;
this.strPhotoPath = null;
this.strSavePath = null;
this.iDiaphaneity = ;
this.iMarkRightSpace = ;
this.iMarkButtomSpace = ;
} /// <summary>
/// 主要用两样都加的
/// </summary>
/// <param name="copyright">要加入的文字</param>
/// <param name="markPath">水印图片路径</param>
/// <param name="photoPath">要加水印的图片路径</param>
/// <param name="savePath">处理后的图片路径</param>
public WaterMark(string copyright, string markPath, string photoPath, string savePath)
{
this.strCopyright = copyright;
this.strMarkPath = markPath;
this.strPhotoPath = photoPath;
this.strSavePath = savePath;
this.iDiaphaneity = ;
this.iMarkRightSpace = ;
this.iMarkButtomSpace = ;
}
#endregion #region property /// <summary>
/// 设置是否显示水印文字
/// </summary>
public bool ShowCopyright
{
set { this.bShowCopyright = value; }
} /// <summary>
/// 设置是否显示水印图片
/// </summary>
public bool ShowMarkImage
{
set { this.bShowMarkImage = value; }
}
/// <summary>
/// 获取或设置要加入的文字
/// </summary>
public string Copyright
{
set { this.strCopyright = value; }
} /// <summary>
/// 获取或设置加水印后的图片路径
/// </summary>
public string SavePath
{
get { return this.strSavePath; }
set { this.strSavePath = value; }
} /// <summary>
/// 获取或设置水印图片路径
/// </summary>
public string MarkPath
{
get { return this.strMarkPath; }
set { this.strMarkPath = value; }
} /// <summary>
/// 获取或设置要加水印图片的路径
/// </summary>
public string PhotoPath
{
get { return this.strPhotoPath; }
set { this.strPhotoPath = value; }
} /// <summary>
/// 设置水印图片的透明度
/// </summary>
public int Diaphaneity
{
set
{
if (value > && value <= )
this.iDiaphaneity = value;
}
} /// <summary>
/// 设置水印字体的透明度0-255
/// </summary>
public int FontDiaphaneity
{
set
{
if (value >= && value <= )
this.iFontDiaphaneity = value;
}
} /// <summary>
/// 设置水印图片在修改图片中距左边的高度
/// </summary>
public int MarkRightSpace
{
set { this.iMarkRightSpace = value; }
} /// <summary>
/// 设置水印图片在修改图片中距底部的高度
/// </summary>
public int MarkButtomSpace
{
set { this.iMarkButtomSpace = value; }
} /// <summary>
/// 设置水印字体在修改图片中距左边的距离
/// </summary>
public int FontRightSpace
{
set { iFontRightSpace = value; }
} /// <summary>
/// 设置水印字体在修改图片中距底部的高度
/// </summary>
public int FontButtomSpace
{
set { iFontButtomSpace = value; }
} #endregion /// <summary>
/// 生成水印图片
/// </summary>
/// <returns></returns>
public void createMarkPhoto()
{
Bitmap bmWatermark = null;
Image gPhoto = Image.FromFile(this.strPhotoPath);
int PhotoWidth = gPhoto.Width;
int PhotoHeight = gPhoto.Height;
Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight, PixelFormat.Format24bppRgb);
bitPhoto.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution); try
{
if (bShowCopyright)
{
Graphics grPhoto = Graphics.FromImage(bitPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.DrawImage(gPhoto, new Rectangle(, , PhotoWidth, PhotoHeight), , , PhotoWidth, PhotoHeight, GraphicsUnit.Pixel); Font crFont = new Font("楷体", iFontSize, FontStyle.Bold);
SizeF crSize = grPhoto.MeasureString(strCopyright, crFont); //设置字体在图片中的位置
float yPosFromBottom = PhotoHeight - iFontButtomSpace - (crSize.Height); //float xCenterOfImg = (phWidth/2);
float xCenterOfImg = PhotoWidth - iFontRightSpace - (crSize.Width / );
//设置字体居中 StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center; //设置绘制文本的颜色和纹理 (Alpha=153)
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(this.iFontDiaphaneity, , , )); //将版权信息绘制到图象上
grPhoto.DrawString(strCopyright, crFont, semiTransBrush2, new PointF(xCenterOfImg, yPosFromBottom), StrFormat); gPhoto = bitPhoto;
grPhoto.Dispose();
} if (bShowMarkImage)
{
//创建一个需要填充水银的Image对象
Image imgWatermark = new Bitmap(strMarkPath);
int iMarkWidth = imgWatermark.Width;
int iMarkmHeight = imgWatermark.Height; Graphics grWatermark = null;
if (bShowCopyright)
{
//在原来修改过的bmPhoto上创建一个水银位图
bmWatermark = new Bitmap(bitPhoto);
bmWatermark.SetResolution(gPhoto.HorizontalResolution, gPhoto.VerticalResolution);
}
else
{
bmWatermark = new Bitmap(gPhoto);
} //将位图bmWatermark加载到Graphics对象
grWatermark = Graphics.FromImage(bmWatermark);
ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(, , , );
colorMap.NewColor = Color.FromArgb(, , , ); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, (float)iDiaphaneity/100f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); grWatermark.DrawImage(imgWatermark, new Rectangle((PhotoWidth - iMarkRightSpace - (iMarkWidth / )), (PhotoHeight - iMarkButtomSpace - (iMarkmHeight / )), iMarkWidth, iMarkmHeight), , , iMarkWidth, iMarkmHeight, GraphicsUnit.Pixel, imageAttributes); gPhoto = bmWatermark;
grWatermark.Dispose();
imgWatermark.Dispose();
}
gPhoto.Save(strSavePath, ImageFormat.Jpeg);
}
finally
{ if (bitPhoto != null)
bitPhoto.Dispose(); if (bmWatermark != null)
bmWatermark.Dispose(); gPhoto.Dispose();
} }
}
}
C#图片水印类的更多相关文章
- PHP 图片水印类
<?php /** * 加水印类,支持文字图片水印的透明度设置.水印图片背景透明. * $obj = new WaterMask($imgFileName); //实例化对象 * $obj-&g ...
- 不错.net图片水印类
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Draw ...
- 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- java常用开发工具类之 图片水印,文字水印,缩放,补白工具类
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- php使用GD库实现图片水印和缩略图——封装成类
学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来 ...
- PHP的图片处理类(缩放、加图片水印和剪裁)
<!--test.php文件内容--> <?php //包含这个类image.class.php include "image.class.php"; $img ...
- 图片水印工具类java
关于jar包的导入我就不多说了,我会把引入的jar包显示出来,大家自行Google package com.net.util; import java.awt.AlphaComposite; impo ...
- pdo文字水印类,验证码类,缩略图类,logo类
文字水印类 image.class.php <?php /** * webrx.cn qq:7031633 * @author webrx * @copyright copyright (c) ...
- PHP水印类
<?php /** * 水印类 * @author zhaoyingnan 2015/07/16 **/ include_once('extend.php'); class Watermark_ ...
随机推荐
- PHP CURL 错误码说明
curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));//一般不加 <?php return [ '1'=> ...
- leetcode解题报告(27):Reverse Linked List
描述 Reverse a singly linked list. 分析 一开始写的时候总感觉没抓到要点,然后想起上数据结构课的教材上有这道题,翻开书一看完就回忆起来了,感觉解法挺巧妙的,不比讨论区的答 ...
- Educational Codeforces Round 64 部分题解
Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...
- leetcode 712
这道题的思路:我是根据最长公共子序列的思路得来的. 最长公共子序列是: d[i][j]表示字符串s1前i个(0-i-1)字符,和字符串s2前j个(0-j-1)字符的最长公共子序列. 分情况讨论: 当s ...
- 《挑战30天C++入门极限》C++中类的多态与虚函数的使用
C++中类的多态与虚函数的使用 类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持 ...
- 两个对象key相同但是value不同,将value不同的键值对以对象形式输出
let obj={ name:'jack', age:18, sex:'girl' } let obj2={ name:'rose', age:18, sex:'boy' } var str={} f ...
- spaceclaim脚本(内摆线)
import math #导入数学模块,因为会使用π def x_comp(k,r,t): #定义x坐标的计算函数 return r * (k -1) * math.cos(t) + r * math ...
- php手记之05-tp5获取器与修改器
获取器 命名规范为: getFieldNameAttr 例如,我们需要对状态值进行转换,可以使用: <?php class User extends Model { public functio ...
- mysql排序字段为空的排在最后面
排序字段为orderid; 1.使用order by orderid desc实现降序时,orderid 为null数据的会排在数据的最后面: 但是,order by orderid升序时,order ...
- storcli64和smartctl定位硬盘的故障信息
storcli64可对LSIRAID卡基本操作进行管理,本文主要是对LSIRAID卡常使用到的命令进行介绍 https://www.cnblogs.com/wangl-blog/archive/201 ...