Skip to content

Commit

Permalink
add readmes and add wordart example
Browse files Browse the repository at this point in the history
  • Loading branch information
tocsoft committed Sep 16, 2017
1 parent f0c90c9 commit d895915
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 2 deletions.
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceRoot}/ImageSharp/AvatarWithRoundedCorner/bin/Debug/netcoreapp1.1/AvatarWithRoundedCorner.dll",
"args": [],
"cwd": "${workspaceRoot}/ImageSharp/AvatarWithRoundedCorner",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
16 changes: 16 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"${workspaceRoot}/ImageSharp/AvatarWithRoundedCorner/AvatarWithRoundedCorner.csproj"
],
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
4 changes: 2 additions & 2 deletions ImageSharp/DrawWaterMarkOnImage/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ static void Main(string[] args)
System.IO.Directory.CreateDirectory("output");
using (var img = Image.Load("fb.jpg"))
{
// for production application we would recomend you create a FontCollection
// For production application we would recomend you create a FontCollection
// singleton and manually install the ttf fonts yourself as using SystemFonts
//can be expensive and you risk font existing or not existing on a deployment
// can be expensive and you risk font existing or not existing on a deployment
// by deployment basis.
Font font = SystemFonts.CreateFont("Arial", 10); // for scaling water mark size is largly ignored.

Expand Down
13 changes: 13 additions & 0 deletions ImageSharp/DrawingTextAlongAPath/DrawingTextAlongAPath.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta0001" />
</ItemGroup>


</Project>
155 changes: 155 additions & 0 deletions ImageSharp/DrawingTextAlongAPath/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Numerics;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Brushes;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.Primitives;
using SixLabors.Shapes;

namespace AvatarWithRoundedCorner
{
static class Program
{
static void Main(string[] args)
{
System.IO.Directory.CreateDirectory("output");
using (Image<Rgba32> img = new Image<Rgba32>(1500, 500))
{
PathBuilder pathBuilder = new PathBuilder();
pathBuilder.SetOrigin(new PointF(500, 0));
pathBuilder.AddBezier(new PointF(50, 450), new PointF(200, 50), new PointF(300, 50), new PointF(450, 450));
// add more complex paths and shapes here.

IPath path = pathBuilder.Build();

// For production application we would recomend you create a FontCollection
// singleton and manually install the ttf fonts yourself as using SystemFonts
// can be expensive and you risk font existing or not existing on a deployment
// by deployment basis.
var font = SystemFonts.CreateFont("Arial", 39, FontStyle.Regular);

string text = "Hello World Hello World Hello World Hello World Hello World";
img.Mutate(ctx => ctx
.Fill(Rgba32.White) // white background image
.Draw(Rgba32.Gray, 3, path) // draw the path so we can see what the text is supposed to be following
.DrawText(text, font, Rgba32.Black, path, new TextGraphicsOptions(true) // draw the text along the path wrapping at the end of the line
{
WrapTextWidth = path.Length
}));

img.Save("output/wordart.png");
}
}

public static IImageProcessingContext<TPixel> ApplyScalingWaterMark<TPixel>(this IImageProcessingContext<TPixel> processingContext, Font font, string text, TPixel color, float padding, bool wordwrap)
where TPixel : struct, IPixel<TPixel>
{
if (wordwrap)
{
return processingContext.ApplyScalingWaterMarkWordWrap(font, text, color, padding);
}
else
{
return processingContext.ApplyScalingWaterMarkSimple(font, text, color, padding);
}
}

public static IImageProcessingContext<TPixel> ApplyScalingWaterMarkSimple<TPixel>(this IImageProcessingContext<TPixel> processingContext, Font font, string text, TPixel color, float padding)
where TPixel : struct, IPixel<TPixel>
{
return processingContext.Apply(img =>
{
float targetWidth = img.Width - (padding * 2);
float targetHeight = img.Height - (padding * 2);

// measure the text size
SizeF size = TextMeasurer.Measure(text, new RendererOptions(font));

//find out how much we need to scale the text to fill the space (up or down)
float scalingFactor = Math.Min(img.Width / size.Width, img.Height / size.Height);

//create a new font
Font scaledFont = new Font(font, scalingFactor * font.Size);

var center = new PointF(img.Width / 2, img.Height / 2);

img.Mutate(i => i.DrawText(text, scaledFont, color, center, new TextGraphicsOptions(true)
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
}));
});
}

public static IImageProcessingContext<TPixel> ApplyScalingWaterMarkWordWrap<TPixel>(this IImageProcessingContext<TPixel> processingContext, Font font, string text, TPixel color, float padding)
where TPixel : struct, IPixel<TPixel>
{
return processingContext.Apply(img =>
{
float targetWidth = img.Width - (padding * 2);
float targetHeight = img.Height - (padding * 2);

float targetMinHeight = img.Height - (padding * 3); // must be with in a margin width of the target height

// now we are working i 2 dimensions at once and can't just scale because it will cause the text to
// reflow we need to just try multiple times

var scaledFont = font;
SizeF s = new SizeF(float.MaxValue, float.MaxValue);

float scaleFactor = (scaledFont.Size / 2);// everytime we change direction we half this size
int trapCount = (int)scaledFont.Size * 2;
if (trapCount < 10)
{
trapCount = 10;
}

bool isTooSmall = false;

while ((s.Height > targetHeight || s.Height < targetMinHeight) && trapCount > 0)
{
if (s.Height > targetHeight)
{
if (isTooSmall)
{
scaleFactor = scaleFactor / 2;
}

scaledFont = new Font(scaledFont, scaledFont.Size - scaleFactor);
isTooSmall = false;
}

if (s.Height < targetMinHeight)
{
if (!isTooSmall)
{
scaleFactor = scaleFactor / 2;
}
scaledFont = new Font(scaledFont, scaledFont.Size + scaleFactor);
isTooSmall = true;
}
trapCount--;

s = TextMeasurer.Measure(text, new RendererOptions(scaledFont)
{
WrappingWidth = targetWidth
});
}

var center = new PointF(padding, img.Height / 2);
img.Mutate(i => i.DrawText(text, scaledFont, color, center, new TextGraphicsOptions(true)
{
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
WrapTextWidth = targetWidth
}));
});
}
}
}
19 changes: 19 additions & 0 deletions ImageSharp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ImageSharp Samples
Various ImageSharp related samples.

1. [Avatar with rounded corners](./AvatarWithRoundedCorner/)

Crops rounded corners of a source image leaving a nice rounded avatar.

2. [Draw watermark on image](./DrawWaterMarkOnImage/)

Draw water mark over an image automaticaly scaling the font size to fill the avalible space.

3. [Change default encoder options](./ChangeDefaultEncoderOptions/)

Provides an example on how you go about switching out the registered encoder for a file format and changing its default options in the process.


2. [Draw text along a path](./DrawingTextAlongAPath/)

Draw some text following the contours of a path.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Samples
General samples for various Six Labors projects

## ImageSharp
Various ImageSharp related samples can be found in the [ImageSharp](./ImageSharp/) folder
15 changes: 15 additions & 0 deletions Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChangeDefaultEncoderOptions
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvatarWithRoundedCorner", "ImageSharp\AvatarWithRoundedCorner\AvatarWithRoundedCorner.csproj", "{855B2106-EF59-4AA0-ADBE-5C874ADA027E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DrawingTextAlongAPath", "ImageSharp\DrawingTextAlongAPath\DrawingTextAlongAPath.csproj", "{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +59,18 @@ Global
{855B2106-EF59-4AA0-ADBE-5C874ADA027E}.Release|x64.Build.0 = Release|Any CPU
{855B2106-EF59-4AA0-ADBE-5C874ADA027E}.Release|x86.ActiveCfg = Release|Any CPU
{855B2106-EF59-4AA0-ADBE-5C874ADA027E}.Release|x86.Build.0 = Release|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Debug|x64.ActiveCfg = Debug|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Debug|x64.Build.0 = Debug|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Debug|x86.ActiveCfg = Debug|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Debug|x86.Build.0 = Debug|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Release|Any CPU.Build.0 = Release|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Release|x64.ActiveCfg = Release|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Release|x64.Build.0 = Release|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Release|x86.ActiveCfg = Release|Any CPU
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -65,6 +79,7 @@ Global
{4E41A12C-5C57-45FB-BE91-35A0D5E5BA39} = {CA7BA24E-2681-4ECF-93FB-404D9505D25F}
{123E7349-67DC-4F35-8A3B-57399053B90F} = {CA7BA24E-2681-4ECF-93FB-404D9505D25F}
{855B2106-EF59-4AA0-ADBE-5C874ADA027E} = {CA7BA24E-2681-4ECF-93FB-404D9505D25F}
{26CDFAA6-E8EE-4A77-A478-DA1BCFA62F3A} = {CA7BA24E-2681-4ECF-93FB-404D9505D25F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7830D804-EBC6-48A3-9189-BA0B67811CEA}
Expand Down

0 comments on commit d895915

Please sign in to comment.