/*

*

*    使用说明:

*  建议先定义一个WaterImage实例

*  然后利用实例的属性,去匹配需要进行操作的参数

*  然后定义一个WaterImageManage实例

*  利用WaterImageManage实例进行DrawImage(),印图片水印

*  DrawWords()印文字水印

*

-*/

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO; namespace ABC
{ /// <summary>
/// 图片位置
/// </summary>
public enum ImagePosition
{
LeftTop, //左上
LeftBottom, //左下
RightTop, //右上
RigthBottom, //右下
TopMiddle, //顶部居中
BottomMiddle, //底部居中
Center //中心
} /// <summary>
/// 水印图片的操作管理 Design by Gary Gong From Demetersoft.com
/// </summary>
public class WaterImageManage
{
/// <summary>
/// 生成一个新的水印图片制作实例
/// </summary>
public WaterImageManage()
{
//
// TODO: Add constructor logic here
//
} /// <summary>
/// 添加图片水印
/// </summary>
/// <param name="sourcePicture">源图片文件名</param>
/// <param name="waterImage">水印图片文件名</param>
/// <param name="alpha">透明度(0.1-1.0数值越小透明度越高)</param>
/// <param name="position">位置</param>
/// <param name="PicturePath" >图片的路径</param>
/// <returns>返回生成于指定文件夹下的水印文件名</returns>
public string DrawImage(string sourcePicture,
string waterImage,
float alpha, ImagePosition position,
string PicturePath)
{
//
// 判断参数是否有效
//
if (sourcePicture == string.Empty || waterImage == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
{
return sourcePicture;
} //
// 源图片,水印图片全路径
//
string sourcePictureName = PicturePath + sourcePicture;
string waterPictureName = PicturePath + waterImage;
string fileSourceExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();
string fileWaterExtension = System.IO.Path.GetExtension(waterPictureName).ToLower();
//
// 判断文件是否存在,以及类型是否正确
//
if (System.IO.File.Exists(sourcePictureName) == false ||
System.IO.File.Exists(waterPictureName) == false || (
fileSourceExtension != ".gif" &&
fileSourceExtension != ".jpg" &&
fileSourceExtension != ".png") || (
fileWaterExtension != ".gif" &&
fileWaterExtension != ".jpg" &&
fileWaterExtension != ".png")
)
{
return sourcePicture;
} // // 目标图片名称及全路径
//
string targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + "_1101.jpg"; //
// 将需要加上水印的图片装载到Image对象中
//
Image imgPhoto = Image.FromFile(sourcePictureName);
//
// 确定其长宽
//
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height; //
// 封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。
//
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb); //
// 设定分辨率
//
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); //
// 定义一个绘图画面用来装载位图
//
Graphics grPhoto = Graphics.FromImage(bmPhoto); //
//同样,由于水印是图片,我们也需要定义一个Image来装载它
//
Image imgWatermark = new Bitmap(waterPictureName); //
// 获取水印图片的高度和宽度
//
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height; //SmoothingMode:指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘。
// 成员名称 说明
// AntiAlias 指定消除锯齿的呈现。
// Default 指定不消除锯齿。 // HighQuality 指定高质量、低速度呈现。
// HighSpeed 指定高速度、低质量呈现。
// Invalid 指定一个无效模式。
// None 指定不消除锯齿。
grPhoto.SmoothingMode = SmoothingMode.AntiAlias; //
// 第一次描绘,将我们的底图描绘在绘图画面上
//
grPhoto.DrawImage(imgPhoto,
new Rectangle(, , phWidth, phHeight),
,
,
phWidth,
phHeight,
GraphicsUnit.Pixel); //
// 与底图一样,我们需要一个位图来装载水印图片。并设定其分辨率
//
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); //
// 继续,将水印图片装载到一个绘图画面grWatermark
//
Graphics grWatermark = Graphics.FromImage(bmWatermark); //
//ImageAttributes 对象包含有关在呈现时如何操作位图和图元文件颜色的信息。
// ImageAttributes imageAttributes = new ImageAttributes(); //
//Colormap: 定义转换颜色的映射
//
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}, // red红色
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green绿色
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue蓝色
new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f}, //透明度
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};// // ColorMatrix:定义包含 RGBA 空间坐标的 5 x 5 矩阵。
// ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。
ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
ColorAdjustType.Bitmap); //
//上面设置完颜色,下面开始设置位置
//
int xPosOfWm;
int yPosOfWm; switch (position)
{
case ImagePosition.BottomMiddle:
xPosOfWm = (phWidth - wmWidth) / ;
yPosOfWm = phHeight - wmHeight - ;
break; case ImagePosition.Center:
xPosOfWm = (phWidth - wmWidth) / ;
yPosOfWm = (phHeight - wmHeight) / ;
break;
case ImagePosition.LeftBottom:
xPosOfWm = ;
yPosOfWm = phHeight - wmHeight - ;
break;
case ImagePosition.LeftTop:
xPosOfWm = ;
yPosOfWm = ;
break;
case ImagePosition.RightTop:
xPosOfWm = phWidth - wmWidth - ;
yPosOfWm = ;
break;
case ImagePosition.RigthBottom:
xPosOfWm = phWidth - wmWidth - ;
yPosOfWm = phHeight - wmHeight - ;
break;
case ImagePosition.TopMiddle:
xPosOfWm = (phWidth - wmWidth) / ;
yPosOfWm = ;
break;
default:
xPosOfWm = ;
yPosOfWm = phHeight - wmHeight - ;
break;
} // // 第二次绘图,把水印印上去
//
grWatermark.DrawImage(imgWatermark,
new Rectangle(xPosOfWm,
yPosOfWm,
wmWidth,
wmHeight),
,
,
wmWidth,
wmHeight,
GraphicsUnit.Pixel,
imageAttributes); imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose(); //
// 保存文件到服务器的文件夹里面
//
imgPhoto.Save(targetImage, ImageFormat.Jpeg);
imgPhoto.Dispose();
imgWatermark.Dispose();
return targetImage.Replace(PicturePath, "");
} /*
*
* 使用说明:
*  建议先定义一个WaterImage实例
*  然后利用实例的属性,去匹配需要进行操作的参数
*  然后定义一个WaterImageManage实例
*  利用WaterImageManage实例进行DrawImage(),印图片水印
*  DrawWords()印文字水印
*
-*/ /// <summary>
/// 在图片上添加水印文字
/// </summary>
/// <param name="sourcePicture">源图片文件(文件名,不包括路径)</param> /// <param name="waterWords">需要添加到图片上的文字</param>
/// <param name="alpha">透明度</param>
/// <param name="position">位置</param>
/// <param name="PicturePath">文件路径</param>
/// <returns></returns>
public string DrawWords(string sourcePicture,
string waterWords,
float alpha,
ImagePosition position,
string PicturePath)
{
//
// 判断参数是否有效
//
if (sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
{
return sourcePicture;
} //
// 源图片全路径
//
if (PicturePath.Substring(PicturePath.Length - , ) != "/")
PicturePath += "/";
string sourcePictureName = PicturePath + sourcePicture;
string fileExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower(); //
// 判断文件是否存在,以及文件名是否正确
//
if (System.IO.File.Exists(sourcePictureName) == false || (
fileExtension != ".gif" &&
fileExtension != ".jpg" && fileExtension != ".png"))
{
return sourcePicture;
} //
// 目标图片名称及全路径
//
string targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + "_0703.jpg"; //创建一个图片对象用来装载要被添加水印的图片
Image imgPhoto = Image.FromFile(sourcePictureName); //获取图片的宽和高
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height; //
//建立一个bitmap,和我们需要加水印的图片一样大小
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb); //SetResolution:设置此 Bitmap 的分辨率
//这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); //Graphics:封装一个 GDI+ 绘图图面。
Graphics grPhoto = Graphics.FromImage(bmPhoto); //设置图形的品质
grPhoto.SmoothingMode = SmoothingMode.AntiAlias; //将我们要添加水印的图片按照原始大小描绘(复制)到图形中
grPhoto.DrawImage(
imgPhoto, // 要添加水印的图片
new Rectangle(, , phWidth, phHeight), // 根据要添加的水印图片的宽和高
, // X方向从0点开始描绘
, // Y方向 phWidth, // X方向描绘长度
phHeight, // Y方向描绘长度
GraphicsUnit.Pixel); // 描绘的单位,这里用的是像素 //根据图片的大小我们来确定添加上去的文字的大小
//在这里我们定义一个数组来确定
int[] sizes = new int[] { , , , , , , }; //字体
Font crFont = null;
//矩形的宽度和高度,SizeF有三个属性,分别为Height高,width宽,IsEmpty是否为空
SizeF crSize = new SizeF(); //利用一个循环语句来选择我们要添加文字的型号
//直到它的长度比图片的宽度小
for (int i = ; i < ; i++)
{
crFont = new Font("arial", sizes[i], FontStyle.Bold); //测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。
crSize = grPhoto.MeasureString(waterWords, crFont); // ushort 关键字表示一种整数数据类型
if ((ushort)crSize.Width < (ushort)phWidth)
break;
} //截边5%的距离,定义文字显示(由于不同的图片显示的高和宽不同,所以按百分比截取)
int yPixlesFromBottom = (int)(phHeight * .); //定义在图片上文字的位置
float wmHeight = crSize.Height;
float wmWidth = crSize.Width; float xPosOfWm; float yPosOfWm; switch (position)
{
case ImagePosition.BottomMiddle:
xPosOfWm = phWidth / ;
yPosOfWm = phHeight - wmHeight - ;
break;
case ImagePosition.Center:
xPosOfWm = phWidth / ;
yPosOfWm = phHeight / ;
break;
case ImagePosition.LeftBottom:
xPosOfWm = wmWidth;
yPosOfWm = phHeight - wmHeight - ;
break;
case ImagePosition.LeftTop:
xPosOfWm = wmWidth / ;
yPosOfWm = wmHeight / ;
break;
case ImagePosition.RightTop:
xPosOfWm = phWidth - wmWidth - ;
yPosOfWm = wmHeight;
break;
case ImagePosition.RigthBottom:
xPosOfWm = phWidth - wmWidth - ;
yPosOfWm = phHeight - wmHeight - ;
break;
case ImagePosition.TopMiddle:
xPosOfWm = phWidth / ;
yPosOfWm = wmWidth; break;
default:
xPosOfWm = wmWidth;
yPosOfWm = phHeight - wmHeight - ;
break;
} //封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。
StringFormat StrFormat = new StringFormat(); //定义需要印的文字居中对齐
StrFormat.Alignment = StringAlignment.Center; //SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
//这个画笔为描绘阴影的画笔,呈灰色
int m_alpha = Convert.ToInt32( * alpha);
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, , , )); //描绘文字信息,这个图层向右和向下偏移一个像素,表示阴影效果
//DrawString 在指定矩形并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。
grPhoto.DrawString(waterWords, //string of text
crFont, //font
semiTransBrush2, //Brush
new PointF(xPosOfWm + , yPosOfWm + ), //Position
StrFormat); //从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构,这里设置透明度为153
//这个画笔为描绘正式文字的笔刷,呈白色
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(, , , )); //第二次绘制这个图形,建立在第一次描绘的基础上
grPhoto.DrawString(waterWords, //string of text
crFont, //font
semiTransBrush, //Brush
new PointF(xPosOfWm, yPosOfWm), //Position
StrFormat); //imgPhoto是我们建立的用来装载最终图形的Image对象
//bmPhoto是我们用来制作图形的容器,为Bitmap对象
imgPhoto = bmPhoto;
//释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满
grPhoto.Dispose(); //将grPhoto保存
imgPhoto.Save(targetImage, ImageFormat.Jpeg);
imgPhoto.Dispose(); return targetImage.Replace(PicturePath, "");
}
} /// <summary>
/// 装载水印图片的相关信息
/// </summary>
public class WaterImage
{
public WaterImage()
{ } private string m_sourcePicture;
/// <summary>
/// 源图片地址名字(带后缀) /// </summary>
public string SourcePicture
{
get { return m_sourcePicture; }
set { m_sourcePicture = value; }
} private string m_waterImager;
/// <summary>
/// 水印图片名字(带后缀)
/// </summary>
public string WaterPicture
{
get { return m_waterImager; }
set { m_waterImager = value; }
} private float m_alpha;
/// <summary>
/// 水印图片文字的透明度
/// </summary>
public float Alpha
{
get { return m_alpha; }
set { m_alpha = value; }
} private ImagePosition m_postition;
/// <summary>
/// 水印图片或文字在图片中的位置
/// </summary>
public ImagePosition Position
{
get { return m_postition; }
set { m_postition = value; }
} private string m_words;
/// <summary>
/// 水印文字的内容
/// </summary>
public string Words
{
get { return m_words; }
set { m_words = value; }
} }
}

MS效率有点低,今天再研究研究有没有别的好方法。

C#(.net)水印图片的生成的更多相关文章

  1. PHP生成随机水印图片

    基于PHP的GD图形库,自己生成一张图片.仅限初识GD库,实例学习. 一.需求 网站的布局用到了类似慕课网课程列表的风格,每一个课程是一个banner图,图下面是标题加简介.因为课程的数量较大没有为所 ...

  2. 前端水印图片及文字js教程

    前端水印图片文字教程如下,复制代码修改图片地址即可看到效果,工作中遇到总结的,喜欢就关注下哦: <!DOCTYPE html><html> <head> <m ...

  3. Atitit 图片 验证码生成attilax总结

    Atitit 图片 验证码生成attilax总结 1.1. 图片验证码总结1 1.2. 镂空文字  打散 干扰线 文字扭曲 粘连2 1.1. 图片验证码总结 因此,CAPTCHA在图片验证码这一应用点 ...

  4. C#-WebForm-★ 上传水印图片 ★

    上传水印图片就是一个选择图片.添加水印.进行上传的过程 绘制图片需要准备: 画布 - 大小 笔 - 颜色.粗细.样式 用什么字体 要画什么(李献策lxc) 步骤: 一.引用 System.Drawin ...

  5. PHP实现文字水印图片

    php实现简单的文字水印图片,使用前需要开启php配置中的gd2功能 <?php/*打开图片*/ //1.配置图片路径 $src="image/55.jpg";//这个路径改 ...

  6. WindowsPhone8中实现圆形图片的生成显示

    原文 WindowsPhone8中实现圆形图片的生成显示 很多软件中(比如QQ)用到了许多圆形图片,作为用户头像等等,原始图片往往是方形的,那么怎么样将方形的图片显示成圆形呢? 一种方法是当背景为固定 ...

  7. C#放缩、截取、合并图片并生成高质量新图的类

    原文:C#放缩.截取.合并图片并生成高质量新图的类 using System;using System.Drawing;using System.Drawing.Imaging;using Syste ...

  8. 开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...

  9. 深度学习之 GAN 进行 mnist 图片的生成

    深度学习之 GAN 进行 mnist 图片的生成 mport numpy as np import os import codecs import torch from PIL import Imag ...

随机推荐

  1. Scrum 5.0(继4.0)

    一,组员任务完成情况 首页设计初步完成但是需要优化界面,只能简单的输出信息和在首页进行登录.界面极其简单. 鸡汤版面设计有困难,问题在于用何种形式来管理用户的数据上传,但是经过小组间的讨论确定设计方向 ...

  2. [BUAA_SE_2017]个人阅读作业 + 总结

    个人阅读作业 银弹 银弹是指能让狼人一枪毙命的致命子弹,对于软件工程而言,我觉得是不存在银弹的.每一项软件开发都是极为特殊的,有特定的需求.特定的功能,如果存在银弹能够直击要害解决问题,那么软件的开发 ...

  3. [转帖 cnblog 的news ]技术实力超群的Netflix,为何没有CTO

    技术实力超群的Netflix,为何没有CTO https://news.cnblogs.com/n/581824/ 投递人 itwriter 发布于 2017-11-05 16:12 评论(2) 有1 ...

  4. 使用Word 进行UTF8 以及字符串编码的转换操作

    1. 使用Word文档能够实现 字符串和utf8编码的转换. 快捷键是 ALT+X 在知乎的一个里面看到一个说法: ㍾ ㍽ ㍼ ㍻ - 这四个在Unicode表里是倒序排列的,而且只预留了这四个年号, ...

  5. mysql索引利弊分析

    转载自:http://blog.csdn.net/linminqin/article/details/44342205  索引的利弊与如何判定,是否需要索引 相信读者都知道索引能够极大地提高数据检索的 ...

  6. DataTable List 相互转换

    This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it ...

  7. 深入理解JAVA虚拟机阅读笔记6——线程安全与锁优化

    线程安全:如果一个对象可以安全的被多个线程同时使用,那它就是线程安全的. 一.Java中的线程安全 1.不可变 不可变的对象一定是线程安全的.String.枚举类型.java.lang.Number的 ...

  8. C# 单例模式的多种简单实现

    什么是单例模式? 这里我就不做过多的解释了, 毕竟关于Singleton的资料实在是太多太多了.点击这里 1.简单的思路就是, 创建对象单例的动作转移到另外的行为上面, 利用一个行为去创建对象自身, ...

  9. CSU1392(NCPC2013)_Number Trick

    给一个小数X,找个A使得:AX=(A循环左移一位) 首先,假设A为一个满足题目条件的数,有n个数位,且最高位数字为A0. 那么可列出方程:AX=(A-A0*10n-1)*10+A0 ————>& ...

  10. 【uoj#142】【UER #5】万圣节的南瓜灯 乱搞+并查集

    题目描述 给出一张 $n\times m$ 的网格图,两个格子之间有一条双向边,当且仅当它们相邻,即在网格图中有一条公共边. 特殊地,对于 $1\le x\le n​$ ,$(x,1)​$ 和 $(x ...