- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章.NET生成水印更好的方法实例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前言 。
众所周知为了保护知识产权,防止资源被盗用,水印在博客、网店等场景中非常常见.
本文首先演示了基于System.Drawing.Image做正常操作。然后基于Direct2D/WIC/DirectWrite,演示了一种全新、不同的“骚”操作.
方法1-System.Drawing给图片加水印 。
System.Drawing.Image原生属于GDI的一部分,是Windows Only,但随着NuGet包System.Drawing.Common的发布,现在System.Drawing.Image已经支持linux:
1
|
Install-Package System.Drawing.Common -Version 4.5.1
|
以下代码演示了如何从给图片加水印:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// 加水印
var watermarkedStream =
new
MemoryStream();
using
(var img = Image.FromStream(File.OpenRead(
@"D:\_\WatermarkDemo.png"
)))
{
using
(var graphic = Graphics.FromImage(img))
{
var font =
new
Font(
"微软雅黑"
, 30, FontStyle.Bold, GraphicsUnit.Pixel);
var color = Color.FromArgb(128, 255, 255, 255);
var brush =
new
SolidBrush(color);
var point =
new
Point(img.Width - 130, img.Height - 50);
graphic.DrawString(
"水印在此"
, font, brush, point);
img.Save(watermarkedStream, ImageFormat.Png);
}
}
|
效果如图(没有黄色剪头):
附:Edi.Wang做了一个NuGet包,可以轻松地配置水印参数:
NuGet:https://github.com/EdiWang/Edi.ImageWatermark 。
文章:https://edi.wang/post/2018/10/12/add-watermark-to-uploaded-image-aspnet-core 。
方法2-Direct2D/WIC给图片加水印 。
Direct2D源于Windows 8/IE 10,安装IE 10之后,Windows 7也能用。Direct2D基于Direct3D,很显然,是Windows Only的.
Direct2D是Windows下一代的2D渲染库,随着Direct2D一起发布的,还有Windows Imaging Component(简称WIC)和DirectWrite.
相关说明和文档链接:
。
技术 | 说明 | 链接 |
---|---|---|
Direct2D | 基于硬件加速的2D图形渲染 | Go |
WIC | 高性能图片编码、解码 | Go |
DirectWrite | 基于硬件加速的文字渲染 | Go |
。
如果您打开链接看了一眼,就不难看出,这些技术都是基于COM的,但我们使用.NET,不是吗?
好在我们有SharpDX 。
SharpDX对这些DirectX技术做了封装,在这个Demo中,我们需要安装SharpDX.Direct2D1和SharpDX.Mathematics两个包:
1
2
|
Install-Package SharpDX.Direct2D1 -Version 4.2.0
Install-Package SharpDX.Mathematics -Version 4.2.0
|
以下代码演示了如何使用SharpDX.Direct2D1给图片加水印:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
using
D2D = SharpDX.Direct2D1;
using
DWrite = SharpDX.DirectWrite;
using
SharpDX;
using
SharpDX.IO;
using
WIC = SharpDX.WIC;
MemoryStream AddWatermark(Stream fileName,
string
watermarkText)
{
using
(var wic =
new
WIC.ImagingFactory2())
using
(var d2d =
new
D2D.Factory())
using
(var image = CreateWicImage(wic, fileName))
using
(var wicBitmap =
new
WIC.Bitmap(wic, image.Size.Width, image.Size.Height, WIC.PixelFormat.Format32bppPBGRA, WIC.BitmapCreateCacheOption.CacheOnDemand))
using
(var target =
new
D2D.WicRenderTarget(d2d, wicBitmap,
new
D2D.RenderTargetProperties()))
using
(var bmpPicture = D2D.Bitmap.FromWicBitmap(target, image))
using
(var dwriteFactory =
new
SharpDX.DirectWrite.Factory())
using
(var brush =
new
D2D.SolidColorBrush(target,
new
Color(0xff, 0xff, 0xff, 0x7f)))
{
target.BeginDraw();
{
target.DrawBitmap(bmpPicture,
new
RectangleF(0, 0, target.Size.Width, target.Size.Height), 1.0f, D2D.BitmapInterpolationMode.Linear);
target.DrawRectangle(
new
RectangleF(0, 0, target.Size.Width, target.Size.Height), brush);
var textFormat =
new
DWrite.TextFormat(dwriteFactory,
"微软雅黑"
, DWrite.FontWeight.Bold, DWrite.FontStyle.Normal, 30.0f);
target.DrawText(watermarkText, textFormat,
new
RectangleF(target.Size.Width - 130, target.Size.Height - 50,
int
.MaxValue,
int
.MaxValue), brush);
}
target.EndDraw();
var ms =
new
MemoryStream();
SaveD2DBitmap(wic, wicBitmap, ms);
return
ms;
}
}
void
SaveD2DBitmap(WIC.ImagingFactory wicFactory, WIC.Bitmap wicBitmap, Stream outputStream)
{
using
(var encoder =
new
WIC.BitmapEncoder(wicFactory, WIC.ContainerFormatGuids.Png))
{
encoder.Initialize(outputStream);
using
(var frame =
new
WIC.BitmapFrameEncode(encoder))
{
frame.Initialize();
frame.SetSize(wicBitmap.Size.Width, wicBitmap.Size.Height);
var pixelFormat = wicBitmap.PixelFormat;
frame.SetPixelFormat(
ref
pixelFormat);
frame.WriteSource(wicBitmap);
frame.Commit();
encoder.Commit();
}
}
}
WIC.FormatConverter CreateWicImage(WIC.ImagingFactory wicFactory, Stream stream)
{
using
(var decoder =
new
WIC.PngBitmapDecoder(wicFactory))
{
var decodeStream =
new
WIC.WICStream(wicFactory, stream);
decoder.Initialize(decodeStream, WIC.DecodeOptions.CacheOnLoad);
using
(var decodeFrame = decoder.GetFrame(0))
{
var converter =
new
WIC.FormatConverter(wicFactory);
converter.Initialize(decodeFrame, WIC.PixelFormat.Format32bppPBGRA);
return
converter;
}
}
}
|
调用方式:
1
|
File.WriteAllBytes(
@"D:\_\Demo2.png"
, AddWatermark(File.OpenRead(
@"D:\_\WatermarkDemo.png"
),
"水印在此"
).ToArray());
|
效果也是一切正常:
有什么区别?
System.Drawing只花了14行,Direct2D却需要整整60行!复杂程度惊人!为什么要舍简单求复杂呢?
因为System.Drawing没有硬件加速,而且生成的图片也没有反走样(Anti-aliasing),这导致使用System.Drawing相比之下较慢,而且生成图片的效果稍差:
很明显可以看出,Direct2D生成的图片更平滑.
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我的支持.
原文链接:https://www.cnblogs.com/sdflysha/p/better-way-to-generate-watermark.html 。
最后此篇关于.NET生成水印更好的方法实例代码的文章就讲到这里了,如果你想了解更多关于.NET生成水印更好的方法实例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试使用以下 keytool 命令为我的应用程序生成 keystore : keytool -genkey -alias tomcat -keystore tomcat.keystore -ke
编辑:在西里尔正确解决问题后,我注意到只需将生成轴的函数放在用于生成标签的函数下面就可以解决问题。 我几乎读完了 O'Reilly 书中关于 D3.js 的教程,并在倒数第二页上制作了散点图,但是当添
虽然使用 GraphiQL 效果很好,但我的老板要求我实现一个用户界面,用户可以在其中通过 UI 元素(例如复选框、映射关系)检查呈现给他们的元素并获取数据,这样做将为该人生成 graphql 输入,
我尝试在 Netbean 6.8 中使用 ws-import 生成 Java 类。我想重新生成 jax-ws,因为在 ebay.api.paypalapi 包中发现了一个错误(我认为该错误是由于 Pa
我有一个 perl 脚本,它获取系统日期并将该日期写入文件名。 系统日期被分配给 TRH1 变量,然后它被设置为一个文件名。 $TRH1 =`date + %Y%m%d%H%M`; print "TR
我是 Haskell 的新手,需要帮助。我正在尝试构建一种必须具有某种唯一性的新数据类型,因此我决定使用 UUID 作为唯一标识符: data MyType = MyType { uuid ::
我制作了一个脚本,它可以根据 Mysql 数据库中的一些表生成 XML。 该脚本在 PHP 中运行。 public function getRawMaterials($apiKey, $format
所以这是我的项目中的一个问题。 In this task, we will use OpenSSL to generate digital signatures. Please prepare a f
我在 SAS LIFEREG 中有一个加速故障时间模型,我想绘制它。因为 SAS 在绘图方面非常糟糕,我想实际重新生成 R 中曲线的数据并将它们绘制在那里。 SAS 提出了一个尺度(在指数分布固定为
我正在为 Django 后端制作一个样板,并且我需要能够使它到达下一个下载它的人显然无法访问我的 secret key 的地方,或者拥有不同的 key 。我一直在研究一些选项,并在这个过程中进行了实验
我正在创建一个生成采购订单的应用程序。我可以根据用户输入的详细信息创建文本文件。我想生成一个看起来比普通文本文件好得多的 Excel。有没有可以在我的应用程序中使用的开源库? 最佳答案 目前还没有任何
我正在尝试使用 ScalaCheck 为 BST 创建一个 Gen,但是当我调用 .sample 方法时,它给了我 java.lang.NullPointerException。我哪里错了? seal
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我尝试编写一些代码,例如(在verilog中): parameter N = 128; if (encoder_in[0] == 1) begin 23 binary_out = 1;
我正忙于在 Grails 项目中进行从 MySQL 到 Postgres 的相当复杂的数据迁移。 我正在使用 GORM 在 PostGres 中生成模式,然后执行 MySQL -> mysqldump
如何使用纯 XSLT 生成 UUID?基本上是寻找一种使用 XSLT 创建独特序列的方法。该序列可以是任意长度。 我正在使用 XSLT 2.0。 最佳答案 这是一个good example 。基本上,
我尝试安装.app文件,但是当我安装并单击“同步”(在iTunes中)时,我开始在设备上开始安装,然后停止,这是一个问题,我不知道在哪里,但我看到了我无法解决的奇怪的事情: 最佳答案 似乎您没有在Xc
自从我生成 JavaDocs 以来已经有一段时间了,我确信这些选项在过去 10 年左右的时间里已经得到了改进。 我能否得到一些有关生成器的建议,该生成器将输出类似于 .Net 文档结构的 JavaDo
我想学习如何生成 PDF,我不想使用任何第三方工具,我想自己用代码创建它。到目前为止,我所看到的唯一示例是我通过在第 3 方 dll 上打开反射器查看的代码,以查看发生了什么。不幸的是,到目前为止我看
我正在从 Epplus 库生成 excel 条形图。 这是我成功生成的。 我的 table 是这样的 Mumbai Delhi Financial D
我是一名优秀的程序员,十分优秀!