/// <summary>
/// 在一张图片的指定位置处加入一张具有水印效果的图片
/// </summary>
/// <param name="SourceImage">指定源图片的绝对路径</param>
/// <param name="WaterMarkImage">指定水印图片的绝对路径</param>
/// <param name="SaveImage">保存图片的绝对路径</param>
public static void MakeWaterMark(string SourceImage, string WaterMarkImage, string SaveImage)
{
// 创建一个对象用于操作需要加水印的源图片
Image imgPhoto = Image.FromFile(SourceImage);
// 获取该源图片的宽度和高度
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height; // 创建一个BMP格式的空白图片(宽度和高度与源图片一致)
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb); // 设置该新建空白BMP图片的分辨率
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); // 将该BMP图片设置成一个图形对象
Graphics grPhoto = Graphics.FromImage(bmPhoto); // 设置生成图片的质量
grPhoto.SmoothingMode = SmoothingMode.AntiAlias; // 将源图片加载至新建的BMP图片中
grPhoto.DrawImage(
imgPhoto, // Photo Image object
new Rectangle(, , phWidth, phHeight), // Rectangle structure
, // x-coordinate of the portion of the source image to draw.
, // y-coordinate of the portion of the source image to draw.
phWidth, // Width of the portion of the source image to draw.
phHeight, // Height of the portion of the source image to draw.
GraphicsUnit.Pixel); // Units of measure // 创建水印图片的 Image 对象
Image imgWatermark = new Bitmap(WaterMarkImage); // 获取水印图片的宽度和高度
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height; //------------------------------------------------------------
// 第一步: 插入水印图片
//------------------------------------------------------------ //Create a Bitmap based on the previously modified photograph Bitmap
Bitmap bmWatermark = new Bitmap(bmPhoto);
bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
//Load this Bitmap into a new Graphic Object
Graphics grWatermark = Graphics.FromImage(bmWatermark); //To achieve a transulcent watermark we will apply (2) color
//manipulations by defineing a ImageAttributes object and
//seting (2) of its properties.
ImageAttributes imageAttributes = new ImageAttributes(); //The first step in manipulating the watermark image is to replace
//the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
//to do this we will use a Colormap and use this to define a RemapTable
ColorMap colorMap = new ColorMap(); //My watermark was defined with a background of 100% Green this will
//be the color we search for and replace with transparency
colorMap.OldColor = Color.FromArgb(, , , );
colorMap.NewColor = Color.FromArgb(, , , ); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); //The second color manipulation is used to change the opacity of the
//watermark. This is done by applying a 5x5 matrix that contains the
//coordinates for the RGBA space. By setting the 3rd row and 3rd column
//to 0.3f we achive a level of opacity
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, 0.3f, 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); //For this example we will place the watermark in the upper right
//hand corner of the photograph. offset down 10 pixels and to the
//left 10 pixles
int xPosOfWm = ((phWidth - wmWidth) - );
int yPosOfWm = ; grWatermark.DrawImage(imgWatermark,
new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //Set the detination Position
, // x-coordinate of the portion of the source image to draw.
, // y-coordinate of the portion of the source image to draw.
wmWidth, // Watermark Width
wmHeight, // Watermark Height
GraphicsUnit.Pixel, // Unit of measurment
imageAttributes); //ImageAttributes Object //Replace the original photgraphs bitmap with the new Bitmap
imgPhoto.Dispose();
imgPhoto = bmWatermark;
grPhoto.Dispose();
grWatermark.Dispose();
bmPhoto.Dispose(); //------------------------------------------------------------
// 第三步:保存图片
//------------------------------------------------------------
imgPhoto.Save(SaveImage, ImageFormat.Jpeg); // 释放使用中的资源
imgPhoto.Dispose();
imgWatermark.Dispose();
bmWatermark.Dispose();
}

C# 水印透明度图片的更多相关文章

  1. 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能

    import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  2. php生成文字水印和图片水印

    生成文字水印   //文字水印 /*打开图片*/ //1.配置图片路径 $src = "4.jpg"; //2.获取图片的信息(得到图片的基本信息) $info = getimag ...

  3. PDF怎么添加文字水印与图片水印

    现在是个知识分享时代,但不可避免的盗版也无处不在,不知道在我们大家身边有没有遇到过这样的情况:自己煞费苦心制作的PDF文档不知道在什么时候就会被别人给盗用了,那么如何才能尽量避免这个问题呢?今天带大家 ...

  4. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  5. C# 处理PPT水印(一)——添加水印效果(文字水印、图片水印)

    对文档添加水印可以有效声明和保护文档,是保护重要文件的方式之一.在PPT文档中同样也可以设置水印,包括文本水印和图片水印,本文将讲述如何通过Spire.Presentation for .NET来对P ...

  6. Spire.Cloud.Word 添加Word水印(文本水印、图片水印)

    概述 Spire.Cloud.Word提供了watermarksApi接口可用于添加水印,包括添加文本水印(SetTextWatermark).图片水印(SetImageWatermark),本文将对 ...

  7. Java 添加Word文本水印、图片水印

    水印是一种常用于各种文档的声明.防伪手段,一般可设置文字水印或者加载图片作为水印.以下内容将分享通过Java编程给Word文档添加水印效果的方法,即 文本水印 图片水印 使用工具:Free Spire ...

  8. Swift - 给图片添加图片水印(图片上绘制另一张图,并可设透明度)

    我前面写了篇文章讲解如何给图片添加文字水印,而如果想要添加图片类型的水印也很简单,只要把原来代码里添加文字的部分改成图片即可. 1,效果图如下: (在图片左上角添加了一个半透明的logo图片) 2,为 ...

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

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

随机推荐

  1. 【Java】多线程_学习笔记

    多线程 1.进程 进程:当一个程序进入内存运行时,它就成为了进程.进程具有独立性.动态性.并发性. A.独立性:进程是系统中独立存在的实体,它可以拥有自己独立的资源,每一个进程都拥有自己私有的地址空间 ...

  2. 转:OSGi 入门篇:生命周期层

    OSGi 入门篇:生命周期层 前言 生命周期层在OSGi框架中属于模块层上面的一层,它的运作是建立在模块层的功能之上的.生命周期层一个主要的功能就是让你能够从外部管理应用或者建立能够自我管理的应用(或 ...

  3. Android Fragment 深度解析

    1.Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套app,然后拷贝一份,修改布局以适应 ...

  4. [ubuntu]用ubuntu开发的日子----win7 ubuntu双系统

    小子终于忍不了win7某些蛋疼的设定,看群里好多大牛推荐mac,但资金紧张,只好推而求其次使用ubuntu,但是由于公司工作环境是windows,所以还必须保留windows系统,一次决定双系统. 下 ...

  5. linux上使用shell脚本查看内存使用率

    如上的内存利用率 = [-/+buffers/cache:used]/[Mem:total ] =  6293404 / 16333656 对于free命令而言,有如下公式: total = used ...

  6. log4net RemotingAppender 的配置

    Before you even start trying any of the alternatives provided, ask yourself whether you really need ...

  7. linux配置3-安装tomcat

    下载文件:apache-tomcat-7.0.73.tar.gz 通过共享传到Ubuntu, 复制到/tmp 解压 移动解压后的文件到到/opt/tomcat7,完成可见:/opt/tomcat7/a ...

  8. 关于tomcat内路径跳转的一些思考

    初学jsp+servlet时经常碰上的几个错误:404.路径正确但页面没有任何内容.样式和图片丢失. 这几个错误曾经让我在debug时头大,现在总结一下,其实它们都跟路径有关,正是因为没有处理好路径跳 ...

  9. ListView 的使用

    一.ListView:不同视图中一种显示想的集合,显示表格样式的数据信息1.视图:            在其右上方小箭头点击将视图改为Details:或者右键属性在外观View将其改为Details ...

  10. uva 10817(状压dp)

    题意:就是有个学校要招老师.要让没门课至少有两个老师可以上.每个样样例先输入三个数字课程数量s,已经在任的老师数量,和应聘的老师数量.已经在任的一定要聘请. 思路是参考了刘汝佳书上的,关键如何状压. ...