/// <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#给图片加文字和图片的水印的更多相关文章

  1. ASP.NET(C#)图片加文字、图片水印,神啊,看看吧

    ASP.NET(C#)图片加文字.图片水印 一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Im ...

  2. Android给图片加文字和图片水印

    我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行. /** * 图片工具类 * @author 水寒 * 欢迎访问水寒的个人博客:ht ...

  3. PHP给图片加文字水印

    <?php /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg'; $dst = ...

  4. Java图片加文字水印

    Java图片加文字水印 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.I ...

  5. C#给图片加文字水印

    public class TxtWaterMark { public enum WaterPositionMode { LeftTop,//左上 LeftBottom,//左下 RightTop,// ...

  6. 一种基于重载的高效c#上图片添加文字图形图片的方法

    在做图片监控显示的时候,需要在图片上添加文字,如果用graphics类绘制图片上的字体,实现图像上添加自定义标记,这种方法经验证是可行的,并且在visual c#2005 编程技巧大全上有提到,但是, ...

  7. Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片

    Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...

  8. php给图片加文字

    在图片上加文字是论坛,博客,新闻网站上最喜欢用的功能,防止盗图.这里看看代码是如何实现的. 首先还是upload_image.php这个文件,注意这里的caption文本框中输入的内容最终会写到图片上 ...

  9. PHP图片加文字水印和图片水印方法(鉴于李老师博客因没加水印被盗,特搜集的办法。希望能有用!)

    $dst_path = 'dst.jpg'; //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字 ...

随机推荐

  1. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  2. BZOJ5369 [Pkusc2018]最大前缀和

    题意 小C是一个算法竞赛爱好者,有一天小C遇到了一个非常难的问题:求一个序列的最大子段和. 但是小C并不会做这个题,于是小C决定把序列随机打乱,然后取序列的最大前缀和作为答案. 小C是一个非常有自知之 ...

  3. Python 函数 memoryview()

    memoryview() 函数返回给定参数的内存查看对象(Momory view). 所谓内存查看对象,是指对支持缓冲区协议的数据进行包装,在不需要复制对象基础上允许Python代码访问.返回元组列表 ...

  4. 谈谈JS中的高级函数

    博客原文地址:Claiyre的个人博客如需转载,请在文章开头注明原文地址 在JavaScript中,函数的功能十分强大.它们是第一类对象,也可以作为另一个对象的方法,还可以作为参数传入另一个函数,不仅 ...

  5. VB.NET实现32位、64位远线程运行ASM,注入非托管、托管DLL

    这是一个老话题,远线程函数给我们提供了机会在其他进程中启动一个新线程,所以我们可以做很多事情.但事情远远没有结束,如果我们要做的事情非常复杂,那么将面临编写大量的ASM代码,虽然我们可以用VC之类的工 ...

  6. coding style 的两点

    通俗介绍coding style两点建议: 模块划分 这个如果做得不好,简直不能忍.有的代码非常莫名其妙,有些东西本身不复杂,非要将其拆成好几个部分,然后做成一个一个碎散的模块,这样并不好.举个例子, ...

  7. 可复用的自定义Adapter

    public abstract class MyAdapter<T> extends BaseAdapter { private ArrayList<T> mData; pri ...

  8. python 面向对象(类的特殊成员)

    python 面向对象: (思维导图 ↑↑↑↑) 类的特殊成员 python的类成员存在着一些具有特殊含义的成员 1.__init__: 类名() 自动执行 __init__ class Foo(ob ...

  9. Hibernate学习8—Hibernate 映射关系(一对一)

    第一节:Hibernate 一对一映射关系实现 假设一个用户对应一个地址:   1)按照主键映射: User.java: package com.cy.model; public class User ...

  10. python稀疏矩阵得到每列最大k项的值,对list内为类对象的排序(scipy.sparse.csr.csr_matrix)

    print(train_set.tdm) print(type(train_set.tdm)) 输出得到: (0, 3200) 0.264940780338 (0, 1682) 0.356545827 ...