C#给图片加文字和图片的水印
/// <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>
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;
FileStream fileStream = new FileStream(this.strPhotoPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Image gPhoto = Image.FromStream(fileStream);
int PhotoWidth = gPhoto.Width;
int PhotoHeight = gPhoto.Height;
//Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight, PixelFormat.Format24bppRgb);
Bitmap bitPhoto = new Bitmap(PhotoWidth, PhotoHeight);
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();
}
//设置输出图片质量,默认输出是60%的清晰度
EncoderParameter ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
EncoderParameters eps = new EncoderParameters();
eps.Param[] = ep;
ImageCodecInfo ic = GetCodecInfo("image/jpeg");
fileStream.Close();
gPhoto.Save(strSavePath, ic, eps);
bmWatermark.Dispose();
bitPhoto.Dispose();
gPhoto.Dispose();
ep.Dispose();
eps.Dispose();
}
finally
{
if (bitPhoto != null)
bitPhoto.Dispose();
if (bmWatermark != null)
bmWatermark.Dispose();
fileStream.Close();
gPhoto.Dispose();
}
} private ImageCodecInfo GetCodecInfo(String mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null;
}
}
C#给图片加文字和图片的水印的更多相关文章
- ASP.NET(C#)图片加文字、图片水印,神啊,看看吧
ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...
- Android给图片加文字和图片水印
我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行. /** * 图片工具类 * @author 水寒 * 欢迎访问水寒的个人博客:ht ...
- PHP给图片加文字水印
<?php /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg'; $dst = ...
- Java图片加文字水印
Java图片加文字水印 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.I ...
- C#给图片加文字水印
public class TxtWaterMark { public enum WaterPositionMode { LeftTop,//左上 LeftBottom,//左下 RightTop,// ...
- 一种基于重载的高效c#上图片添加文字图形图片的方法
在做图片监控显示的时候,需要在图片上添加文字,如果用graphics类绘制图片上的字体,实现图像上添加自定义标记,这种方法经验证是可行的,并且在visual c#2005 编程技巧大全上有提到,但是, ...
- Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...
- php给图片加文字
在图片上加文字是论坛,博客,新闻网站上最喜欢用的功能,防止盗图.这里看看代码是如何实现的. 首先还是upload_image.php这个文件,注意这里的caption文本框中输入的内容最终会写到图片上 ...
- PHP图片加文字水印和图片水印方法(鉴于李老师博客因没加水印被盗,特搜集的办法。希望能有用!)
$dst_path = 'dst.jpg'; //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字 ...
随机推荐
- MYSQL子查询例题以及答案
More Subqueries Quizzes Above is the ERD for the database again - it might come in handy as you tack ...
- 9.Python安装scrapy教程
1.在命令行中输入:pip3 install scrapy(pip3是因为本人python版本是3.6),报错如下: 2.解决方法:在https://www.lfd.uci.edu/~gohlke/ ...
- CMapStringToPtr添加与释放
// 创建MapStringToPtr CMapStringToPtr m_prjFiles; CStringList m_fileList; m_fileList.AddTail(_T(" ...
- 【学习记录】二分查找的C++实现,代码逐步优化
二分查找的思想很简单,它是针对于有序数组的,相当于数组(设为int a[N])排成一颗二叉平衡树(左子节点<=父节点<=右子节点),然后从根节点(对应数组下标a[N/2])开始判断,若值& ...
- Pdnovel 在线阅读体验
pdnovel是discuz的一款小说阅读插件,本身是用php开发的,数据存储于mysql,小说文本存储于file文件.pdnovel本身已有添加书籍.连载章节的功能,但为了批量添加全本txt书籍又开 ...
- “FPGA+云"助力高性能计算
用AI防鲨鱼.用AI学写中国书法.用AI预测人类死亡时间.用AI审判罪犯……在人工智能方兴未艾的今天,越来越廉价和普及的AI领域真的是什么都不值钱,除了想象力.那在这无所不能的AI盛世,一定没道理让算 ...
- Spring Cloud 服务网关Zuul
Spring Cloud 服务网关Zuul 服务网关是分布式架构中不可缺少的组成部分,是外部网络和内部服务之间的屏障,例如权限控制之类的逻辑应该在这里实现,而不是放在每个服务单元. Spring Cl ...
- nested exception is com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1109 > 1024
MySQL的一个系统参数:max_allowed_packet >mysql -u root -p //root登录 1. 查看系统参数:show VARIABLES like '%max_al ...
- Nmon、nmon analyse安装及使用
性能监控算是性能测试中的一部分,测试人员需要去分析各类系统指标,CPU.网络.内存.磁盘I/O等等.嗯.通常linux系统下有诸如top.netstat.iostat等命令进行查看:而有时需要看某数据 ...
- 仅用CSS3创建h5预加载跳动圈
<head> <meta charset="UTF-8"> <title></title> <style type=" ...