зеркало из https://github.com/SixLabors/Demos.git
add watermark demo
This commit is contained in:
Родитель
f47683859a
Коммит
b0a1a0edad
|
@ -0,0 +1,22 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.26403.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WaterMarkSample", "WaterMarkSample\WaterMarkSample.csproj", "{06858879-8323-4F32-8260-EFA17EC1DD4F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{06858879-8323-4F32-8260-EFA17EC1DD4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{06858879-8323-4F32-8260-EFA17EC1DD4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{06858879-8323-4F32-8260-EFA17EC1DD4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{06858879-8323-4F32-8260-EFA17EC1DD4F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="myget.org imagesharp" value="https://www.myget.org/F/imagesharp/api/v3/index.json" />
|
||||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
|
||||
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
|
||||
</packageSources>
|
||||
</configuration>
|
|
@ -0,0 +1,3 @@
|
|||
# Demos
|
||||
|
||||
The demos here will mostly be detailed in blog posts at http://sixlabors.com/blog/
|
|
@ -0,0 +1,58 @@
|
|||
using System;
|
||||
using System.Numerics;
|
||||
using ImageSharp;
|
||||
using SixLabors.Fonts;
|
||||
|
||||
namespace WaterMarkSample
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
FontFamily family = FontCollection.SystemFonts.Find("Arial");
|
||||
|
||||
Font font = new Font(family, 100f, FontStyle.Bold);
|
||||
|
||||
ApplyWaterMarkSimple(font, "Copyright Person Name", "small.jpg", "small_simple.jpg");
|
||||
ApplyWaterMarkSimple(font, "Copyright Person Name", "large.jpg", "large_simple.jpg");
|
||||
|
||||
ApplyWaterMarkScaled(font, "Copyright Person Name", "small.jpg", "small_scaled.jpg");
|
||||
ApplyWaterMarkScaled(font, "Copyright Person Name", "large.jpg", "large_scaled.jpg");
|
||||
}
|
||||
|
||||
static void ApplyWaterMarkSimple(Font font, string text, string inputPath, string outputPath)
|
||||
{
|
||||
using (Image img = Image.Load(inputPath))
|
||||
{
|
||||
Color fill = new Color(128, 128, 128, 200);
|
||||
|
||||
img.DrawText(text, font, fill, new Vector2(0, 0));
|
||||
|
||||
img.Save(outputPath);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static void ApplyWaterMarkScaled(Font font, string text, string inputPath, string outputPath)
|
||||
{
|
||||
using (Image img = Image.Load(inputPath))
|
||||
{
|
||||
TextMeasurer measurer = new TextMeasurer();
|
||||
|
||||
// we can now get the dimensions of the bounding box of a piece of text
|
||||
SixLabors.Fonts.Size size = measurer.MeasureText(text, font, 72);
|
||||
|
||||
// calculate the scaling factor we need to change the fontsize by to fit the image
|
||||
float scalingFactor = Math.Min(img.Width / size.Width, img.Height / size.Height);
|
||||
|
||||
Font scaledFont = new Font(font, scalingFactor * font.Size);
|
||||
Color fill = new Color(128, 128, 128, 200);
|
||||
|
||||
img.DrawText(text, scaledFont
|
||||
, fill, new Vector2(0, 0));
|
||||
|
||||
img.Save(outputPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp1.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ImageSharp" Version="1.0.0-alpha5-00054" />
|
||||
<PackageReference Include="ImageSharp.Drawing" Version="1.0.0-alpha5-00048" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 437 KiB |
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 137 KiB |
Загрузка…
Ссылка в новой задаче