Skip to content

Commit

Permalink
image watermark
Browse files Browse the repository at this point in the history
  • Loading branch information
LazZiya committed Feb 17, 2019
1 parent d0d3219 commit 58d5349
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
52 changes: 52 additions & 0 deletions LazZiya.ImageResize/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,58 @@ public static void TextWatermark(this Image img,
graphics.Dispose();
}


/// <summary>
/// add image watermark over the uploaded image
/// </summary>
/// <param name="img"></param>
/// <param name="wmFileName"></param>
/// <param name="spot"></param>
/// <param name="stickToBorder"></param>
public static void ImageWatermark(this Image img, string wmFileName, TargetSpot spot = TargetSpot.TopRight, bool stickToBorder = false)
{
var graphics = Graphics.FromImage(img);

graphics.SmoothingMode = SmoothingMode.None;
graphics.CompositingMode = CompositingMode.SourceOver;

var wmImage = Image.FromFile(wmFileName);

var wmW = wmImage.Width;
var wmH = wmImage.Height;

var drawingPoint = ImageWatermarkPos(img.Width, img.Height, wmW, wmH, spot, stickToBorder);

graphics.DrawImage(wmImage, drawingPoint);

graphics.Dispose();
}

private static PointF ImageWatermarkPos(int imgWidth, int imgHeight, int wmWidth, int wmHeight, TargetSpot spot, bool stickToBorder)
{
float marginW = stickToBorder ? 0F : imgWidth * 0.05F;
float marginH = stickToBorder ? 0F : imgHeight * 0.05F;

PointF point;

switch (spot)
{
case TargetSpot.BottomLeft: point = new PointF(marginW, imgHeight - wmHeight - marginH); break;
case TargetSpot.BottomMiddle: point = new PointF(imgWidth / 2 - wmWidth / 2, imgHeight - wmHeight - marginH); break;
case TargetSpot.BottomRight: point = new PointF(imgWidth - wmWidth - marginW, imgHeight - wmHeight - marginH); break;
case TargetSpot.MiddleLeft: point = new PointF(marginW, imgHeight / 2 - wmHeight / 2); break;
case TargetSpot.Center: point = new PointF(imgWidth / 2 - wmWidth / 2, imgHeight / 2 - wmHeight / 2); break;
case TargetSpot.MiddleRight: point = new PointF(imgWidth - wmWidth - marginW, imgHeight / 2 - wmHeight / 2); break;
case TargetSpot.TopLeft: point = new PointF(marginW, marginH); break;
case TargetSpot.TopMiddle: point = new PointF(imgWidth / 2 - wmWidth / 2, marginH); break;

case TargetSpot.TopRight:
default: point = new PointF(imgWidth - wmWidth - marginW, marginH); break;
}

return point;
}

/// <summary>
/// watermark text pos
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions LazZiya.ImageResize/LazZiya.ImageResize.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
<RepositoryUrl>https://github.com/LazZiya/ImageResize</RepositoryUrl>
<PackageProjectUrl>http://ziyad.info/en/articles/29-Image_Resize_for_dotNetCore</PackageProjectUrl>
<Platforms>AnyCPU;x64</Platforms>
<AssemblyVersion>1.0.0.7</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<AssemblyVersion>1.1.0.8</AssemblyVersion>
<FileVersion>1.1.0.1</FileVersion>
<Version>1.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions LazZiya.ImageResize/LazZiya.ImageResize.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>LazZiya.ImageResize</id>
<version>1.0.0</version>
<version>1.1.0</version>
<title>LazZiya.ImageResize</title>
<authors>Ziyad.info</authors>
<owners>Ziya Mollamahmut</owners>
Expand All @@ -11,7 +11,7 @@
<iconUrl>https://raw.githubusercontent.com/LazZiya/ImageResize/master/LazZiya.ImageResize/files/icon.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Simple tool for image resize, and add text over image for ASP.NET Core</description>
<releaseNotes>v1.0.0: Resize uploaded images, add text watermark</releaseNotes>
<releaseNotes>v1.1.0: add image watermark to the uploaded image</releaseNotes>
<copyright>­­2019 Ziyad.info</copyright>
<tags>asp.net,core,.net,dotnet,image,resize,text,watermark</tags>
<repository url="https://github.com/LazZiya/ImageResize" />
Expand Down

0 comments on commit 58d5349

Please sign in to comment.