C# 水印透明度图片
/// <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# 水印透明度图片的更多相关文章
- 本图片处理类功能非常之强大可以实现几乎所有WEB开发中对图像的处理功能都集成了,包括有缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等功能
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- php生成文字水印和图片水印
生成文字水印 //文字水印 /*打开图片*/ //1.配置图片路径 $src = "4.jpg"; //2.获取图片的信息(得到图片的基本信息) $info = getimag ...
- PDF怎么添加文字水印与图片水印
现在是个知识分享时代,但不可避免的盗版也无处不在,不知道在我们大家身边有没有遇到过这样的情况:自己煞费苦心制作的PDF文档不知道在什么时候就会被别人给盗用了,那么如何才能尽量避免这个问题呢?今天带大家 ...
- PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转
[强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...
- C# 处理PPT水印(一)——添加水印效果(文字水印、图片水印)
对文档添加水印可以有效声明和保护文档,是保护重要文件的方式之一.在PPT文档中同样也可以设置水印,包括文本水印和图片水印,本文将讲述如何通过Spire.Presentation for .NET来对P ...
- Spire.Cloud.Word 添加Word水印(文本水印、图片水印)
概述 Spire.Cloud.Word提供了watermarksApi接口可用于添加水印,包括添加文本水印(SetTextWatermark).图片水印(SetImageWatermark),本文将对 ...
- Java 添加Word文本水印、图片水印
水印是一种常用于各种文档的声明.防伪手段,一般可设置文字水印或者加载图片作为水印.以下内容将分享通过Java编程给Word文档添加水印效果的方法,即 文本水印 图片水印 使用工具:Free Spire ...
- Swift - 给图片添加图片水印(图片上绘制另一张图,并可设透明度)
我前面写了篇文章讲解如何给图片添加文字水印,而如果想要添加图片类型的水印也很简单,只要把原来代码里添加文字的部分改成图片即可. 1,效果图如下: (在图片左上角添加了一个半透明的logo图片) 2,为 ...
- PHP图片加文字水印和图片水印方法(鉴于李老师博客因没加水印被盗,特搜集的办法。希望能有用!)
$dst_path = 'dst.jpg'; //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字 ...
随机推荐
- js中call、apply、bind的用法
原文链接:http://www.cnblogs.com/xljzlw/p/3775162.html var zlw = { name: "zlw", sayHello: funct ...
- 当云遇见APP性能测试会怎样?
一个测试人员应该明白,无论是在云内部或是外部,对一个应用程序而言,需要进行测试的地方都是一样的.最终用户只关注的是一个无缺陷的高性能的应用程序,而不关心应用程序位于哪里.在大多数的云服务协议里,都会提 ...
- 两个list 合并成新一个list
- .ashx datatable转excel
using System;using System.Collections;using System.Collections.Generic;using System.Data;using Syste ...
- 【转】[特征选择] An Introduction to Feature Selection 翻译
中文原文链接:http://www.cnblogs.com/AHappyCat/p/5318042.html 英文原文链接: An Introduction to Feature Selection ...
- 【原创】xgboost 特征评分的计算原理
xgboost是基于GBDT原理进行改进的算法,效率高,并且可以进行并行化运算: 而且可以在训练的过程中给出各个特征的评分,从而表明每个特征对模型训练的重要性, 调用的源码就不准备详述,本文主要侧重的 ...
- BADI_MATERIAL_CHECK(物料主数据表的增强检查)
多次调用的BADI,'业务加载项定义的多重使用意味着可以有多个活动的 业务加载项实施.在进行调用时,将调用所有上述实施,但调用顺序不可预期. 因此,如果使用多重使用 业务加载项定义,那么顺序不得起到任 ...
- rem
"em"是相对于其父元素来设置字体大小的,这样就会存在一个问题,进行任何元素设置,都有可能需要知道他父元素的大小,在我们多次使用时,就会带来无法预知的错误风险.而rem是相对于根元 ...
- 课堂作业二 PAT1025 反转链表
MyGitHub 终于~奔溃了无数次后,看到这个结果 ,感动得不要不要的::>_<:: 题目在这里 题目简述:该题可大致分为 输入链表 -> 链表节点反转 -> 两个步骤 输入 ...
- 二:基础概述netty
如果不了解netty的,可以百度下,netty社区现在也比较活跃. 现在所谓的大数据,flume,storm等底层都是netty. netty的性能模型: io模型---->异步非阻塞io ...