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_ ...
随机推荐
- Decode Ways II
Description A message containing letters from A-Z is being encoded to numbers using the following ma ...
- 项目架构&架构部署&网站分析&网站优化
一.架构演变 一个项目至少由三层内容组成:web访问层.数据库层.存储层 初级阶段 单体阶段 常见场景:项目初期 部署特点:所有应用服务都在一台主机 应用特点:开发简单 应用/数据分离阶段 常见场景: ...
- grpc提供http服务
package main import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials&qu ...
- 数据库Count 语句详解
数据库查询相信很多人都不陌生,所有经常有人调侃程序员就是CRUD专员,这所谓的CRUD指的就是数据库的增删改查.在数据库的增删改查操作中,使用最频繁的就是查询操作.而在所有查询操作中,统计数量操作更是 ...
- js遍历删除数组中不符合条件的元素
//一般解决方法 let arr = [1,2,3]; for(let i=0; i<arr.length; i++){ if(arr[i]==2){ arr.splice(i, 1); i-- ...
- jsp+ ueditor word粘贴上传
最近公司做项目需要实现一个功能,在网页富文本编辑器中实现粘贴Word图文的功能. 我们在网站中使用的Web编辑器比较多,都是根据用户需求来选择的.目前还没有固定哪一个编辑器 有时候用的是UEditor ...
- RookeyFrame 隐藏 首次加载菜单 的伸缩动画
一进入系统,然后点击菜单“系统管理”,会看到展开的“系统设置”菜单,又缩回去了,每次都会有(处女座看到就想改). 隐藏这个动画的JS:jquery.easyui.min.js,这个JS里面有个方法“_ ...
- 二维$MLE$线段树
关于二维线段树,ta死了 先来看看两种二维线段树的打法 1.四叉树 然而ta死了,ta是$\Theta (n)$的,加上线段树的常数,$T$飞稳 2.线段树套线段树 我尽量画出来... 图中每个方块是 ...
- codevs 1683 车厢重组
1683 车厢重组 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 白银 Silver 题目描述 Description 在一个旧式的火车站旁边有一座桥,其桥面可以绕河中心的桥 ...
- 像素迷踪,当Unity的Frame Debugger力不从心时
http://www.manew.com/thread-92382-1-1.html 从版本5开始,Unity包含了一个全新的可视化帧调试工具,Frame Debugger.该工具能帮你解决很多图形方 ...