-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
251 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
ImageSharp/DrawingTextAlongAPath/DrawingTextAlongAPath.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters