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)); //打上文字 ...
随机推荐
- EasyUI的combobox控件使用onchange 问题
在项目中几次都遇到了同样的问题,现在都不知道怎样解决了! 路过的朋友们帮我看看嘛!谢谢了! 最后我想要实现的效果是这样的. 在下拉列表中不存在值.(这里的是下拉列表中存在值的!) 但是在我输入值 ...
- latex 竖排子图的生成
latex命令如下: 需要的包为: \usepackage{graphicx} \usepackage{subfigure} \begin{figure*}%加*的作用是跨栏(双栏和单栏latex的区 ...
- c++11的右值引用、移动语义
对于c++11来说移动语义是一个重要的概念,一直以来我对这个概念都似懂非懂.最近翻翻资料感觉突然开窍,因此记下.其实搞懂之后就会发现这个概念很简单,并无什么高深的地方. 先说说右值引用.右值一般指的是 ...
- 【App测试】怎么测试启动时间?
版权声明:本文由何小伟原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/687066001482481827 来源:腾云阁 h ...
- netperf安装及使用
一.简介 Netperf是由惠普公司开发的,测试网络栈.即测试不同类型的网络性能的benchmark工具,大多数网络类型TCP/UPD端对端的性能,得到网络上不同类型流量的性能参数.Netperf根据 ...
- REST架构之Apache Wink
Apache Wink是一个使用简单,稳定的Java框架,用于创建RESTful web services应用程序.Wink包括了一个服务器端模块和一个客户端模块,用于帮助开发者快速高效的开发REST ...
- css学习笔记 5
将css引入到html页面中的方法: 用style属性设置样式 用<style>标签设置样式 用<link>标签引入外部样式文件 用@import引入外部样式文件 <li ...
- html中给表格添加斜线
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- windows7 64,32位下scrapy爬虫框架的环境搭建
适用于python 2.7 64位安装 一.操作系统:WIN7 64位 二.python版本:2.7 64位(scrapy目前不支持3.x) 不确定位数的,看图 三.安装相关软件:(可以从我的百度网盘 ...
- table中的td内容超出隐藏
<table style="table-layout: fixed;width: XXX px"> <tr> <td style="whit ...