/// <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. Linux 系统时间和硬件时间

    linux 的系统时间有时跟硬件时间是不同步的 Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟.系统时钟是指当前Linux Kernel ...

  2. 不使用插件实现对WordPress默认编辑器的增强

    四处寻觅无果.无意看了一下wordpress官方的API函数.苍天有眼啊!原来,后台的编辑器可以插入很多增强功能.果断卸载掉CK and SyntaxHighlighter编辑器插件.事实上,Word ...

  3. null

    期末考备考最后三天. 加油,把这学期学的知识给它搞透了.

  4. haligong2016

    A 采用递推的方法,由于要到达棋盘上的一个点,只能从左边或者上边过来,根据加法原则,到达某一点的路径数目,就等于到达其相邻的上点和左点的路径数目的总和.所有海盗能达到的点将其路径数置为0即可. #in ...

  5. angular遇到问题

    一.一个js中只有一个angunlar.module,但可以有多个controller,从而控制多个不同的作用域,每个作用域都有独立的$scope.不同作用域之间又有$rootScope这个桥梁 二. ...

  6. 如何解决子元素设了margin-top之后父元素所受的影响

    解决方法: 1.在父元素上加:overflow:hidden. 2.给父元素加border; 3.外容器上加上padding.

  7. JS实现 点击button(copy) 复制对应的网址——类似于复制推广链接

    <form action=""> <input type="text" class="share-input" value ...

  8. 总结js中数据类型的bool值及其比较

    首先需要知道的是,js中有6个值为false,分别是: 0, '', null, undefined, NaN 和 false, 其他(包括{}, [], Infinity)为true. 可以使用Bo ...

  9. ASP通过ADODB读取Access数据库

    <% On Error Resume Next set conn=server.createobject("adodb.connection") mypath=server. ...

  10. Oracle数据库如何创建DATABASE LINK?

    Oracle数据库如何创建DATABASE LINK? 2011-08-09 14:54 taowei20061122 CSDN博客  http://blog.csdn.net/taowei20061 ...