Skip to content

Commit

Permalink
animated text-image watermarks
Browse files Browse the repository at this point in the history
  • Loading branch information
LazZiya committed Feb 22, 2021
1 parent b33829d commit 30e893d
Show file tree
Hide file tree
Showing 10 changed files with 568 additions and 29 deletions.
22 changes: 21 additions & 1 deletion LazZiya.ImageResize/Animated/AnimatedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ private AnimatedImage(Image image)
image.Dispose();
}

/// <summary>
/// Make sure to initialize values
/// </summary>
public AnimatedImage(IList<Image> frames)
{
if (frames == null)
throw new NullReferenceException(nameof(frames));

var image = frames[0];

FramesCount = frames.Count;
Size = new Size(image.Width, image.Height);
Frames = frames;
ImageColorFormat = ImageColorFormats.GetColorFormat((Bitmap)image);
PixelFormat = image.PixelFormat;
HorizontalResolution = image.HorizontalResolution;
VerticalResolution = image.VerticalResolution;
RawFormat = image.RawFormat;
}

/// <summary>
/// Create animated image from file
/// </summary>
Expand Down Expand Up @@ -145,7 +165,7 @@ public static AnimatedImage FromHBitmap(IntPtr hbitmap, IntPtr hpalatte)
/// Save animated gif
/// </summary>
/// <param name="path"></param>
/// <param name="delay">Frmae delay in milliseconds</param>
/// <param name="delay">Frame delay in milliseconds</param>
public void SaveAs(string path, int delay = 400)
{
using (var fs = File.Create(path))
Expand Down
71 changes: 71 additions & 0 deletions LazZiya.ImageResize/Animated/AnimatedImageAnimatedTextWatermark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections.Generic;
using System.Drawing;
using System.Net.Http.Headers;

namespace LazZiya.ImageResize.Animated
{
/// <summary>
/// Add animated text watermark over animated image.
/// </summary>
public static class AnimatedImageAnimatedTextWatermark
{
/// <summary>
/// Add animated text watermark over animated image.
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
public static AnimatedImage AddAnimatedTextWatermark(this AnimatedImage img, string text)
{
return AddAnimatedTextWatermark(img, text, new TextWatermarkOptions(), new AnimatedTextWatermarkOptions());
}

/// <summary>
/// Add animated text watermark over animated image.
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
/// <param name="animOps"></param>
public static AnimatedImage AddAnimatedTextWatermark(this AnimatedImage img, string text, AnimatedTextWatermarkOptions animOps)
{
return AddAnimatedTextWatermark(img, text, new TextWatermarkOptions(), animOps);
}

/// <summary>
/// Add animated text watermark over animated image.
/// </summary>
/// <param name="img"></param>
/// <param name="text">text to draw over the image</param>
/// <param name="ops">Text watermark options <see cref="TextWatermarkOptions"/></param>
public static AnimatedImage AddAnimatedTextWatermark(this AnimatedImage img, string text, TextWatermarkOptions ops)
{
return AddAnimatedTextWatermark(img, text, ops, new AnimatedTextWatermarkOptions());
}

/// <summary>
/// Add animated text watermark over animated image.
/// </summary>
/// <param name="img"></param>
/// <param name="text">text to draw over the image</param>
/// <param name="ops">Text watermark options <see cref="TextWatermarkOptions"/></param>
/// <param name="animOps">Animated text options <see cref="AnimatedTextWatermarkOptions"/></param>
public static AnimatedImage AddAnimatedTextWatermark(this AnimatedImage img, string text, TextWatermarkOptions ops, AnimatedTextWatermarkOptions animOps)
{
var fList = new List<Image>();

var charPerFrame = img.FramesCount >= text.Length ? 1 : text.Length / img.FramesCount;

for (int i = 0; i < img.FramesCount; i++)
{
var subLength = charPerFrame * i > text.Length ? text.Length : charPerFrame * i;
var subStr = text.Substring(0, subLength);
var frame = img.Frames[i].AddTextWatermark(subStr, ops);
fList.Add(frame);
}

img.Frames.Clear();
img.Frames = fList;

return img;
}
}
}
10 changes: 5 additions & 5 deletions LazZiya.ImageResize/Animated/AnimatedImageImageWatermark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
namespace LazZiya.ImageResize.Animated
{
/// <summary>
/// Add an image watermark to animated gif
/// Add a static image watermark over animated image.
/// </summary>
public static class AnimatedImageImageWatermark
{
/// <summary>
/// Draw image watermark
/// Add a static image watermark over animated image.
/// </summary>
/// <param name="img">The original image</param>
/// <param name="wmImgPath">Path to the watermark image file e.g. wwwroot\images\watermark.png</param>
Expand All @@ -20,7 +20,7 @@ public static AnimatedImage AddImageWatermark(this AnimatedImage img, string wmI
}

/// <summary>
/// Draw image watermark
/// Add a static image watermark over animated image.
/// </summary>
/// <param name="img">The original image</param>
/// <param name="wmImage">Watermark image</param>
Expand All @@ -30,7 +30,7 @@ public static AnimatedImage AddImageWatermark(this AnimatedImage img, Image wmIm
}

/// <summary>
/// Draw image watermark
/// Add a static image watermark over animated image.
/// </summary>
/// <param name="img">The original image</param>
/// <param name="wmImgPath">Path to the watermark image file e.g. wwwroot\images\watermark.png</param>
Expand All @@ -42,7 +42,7 @@ public static AnimatedImage AddImageWatermark(this AnimatedImage img, string wmI
}

/// <summary>
/// Draw image watermark.
/// Add a static image watermark over animated image.
/// <para>Notice regarding watermark opacity:</para>
/// <para>If watermark image needs to be resized, first resize the watermark image,
/// then save it to the disc, and read it again with Image.FromFile.</para>
Expand Down
7 changes: 3 additions & 4 deletions LazZiya.ImageResize/Animated/AnimatedImageTextWatermark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace LazZiya.ImageResize
{
/// <summary>
/// Add text watermark over the image.
/// Add a static text watermark over animated image.
/// </summary>
public static class AnimatedImageTextWatermark
{

/// <summary>
/// Add text watermark over the image.
/// Add a static text watermark over animated image.
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
Expand All @@ -21,7 +20,7 @@ public static AnimatedImage AddTextWatermark(this AnimatedImage img, string text
}

/// <summary>
/// Add text watermark over the image.
/// Add a static text watermark over animated image.
/// </summary>
/// <param name="img"></param>
/// <param name="text">text to draw over the image</param>
Expand Down
24 changes: 24 additions & 0 deletions LazZiya.ImageResize/Animated/AnimatedTextWatermarkOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace LazZiya.ImageResize.Animated
{
/// <summary>
/// Define options for animated text
/// </summary>
public class AnimatedTextWatermarkOptions
{
/// <summary>
/// Pre-defined text animations
/// </summary>
public TextAnimation TextAnimation { get; set; } = TextAnimation.Typing;
}

/// <summary>
/// Pre defined text animations
/// </summary>
public enum TextAnimation
{
/// <summary>
/// increment char-by-char
/// </summary>
Typing = 0
}
}
75 changes: 75 additions & 0 deletions LazZiya.ImageResize/Animated/ImageAnimatedImageWatermark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using System.Drawing;

namespace LazZiya.ImageResize.Animated
{

/// <summary>
/// Add an animated image watermark to static image
/// </summary>
public static class ImageAnimatedImageWatermark
{
/// <summary>
/// Draw animated image watermark over a static image
/// </summary>
/// <param name="img">The original image</param>
/// <param name="wmImgPath">Path to the watermark image file e.g. wwwroot\images\watermark.png</param>
public static AnimatedImage AddAnimatedImageWatermark(this Image img, string wmImgPath)
{
var wm = AnimatedImage.FromFile(wmImgPath);
return img.AddAnimatedImageWatermark(wm, new ImageWatermarkOptions());
}

/// <summary>
/// Draw animated image watermark over a static image
/// </summary>
/// <param name="img">The original image</param>
/// <param name="wmImage">Watermark image</param>
public static AnimatedImage AddAnimatedImageWatermark(this Image img, AnimatedImage wmImage)
{
return img.AddAnimatedImageWatermark(wmImage, new ImageWatermarkOptions());
}

/// <summary>
/// Draw animated image watermark over a static image
/// </summary>
/// <param name="img">The original image</param>
/// <param name="wmImgPath">Path to the watermark image file e.g. wwwroot\images\watermark.png</param>
/// <param name="ops">Image watermark options <see cref="ImageWatermarkOptions"/></param>
public static AnimatedImage AddAnimatedImageWatermark(this Image img, string wmImgPath, ImageWatermarkOptions ops)
{
var wm = AnimatedImage.FromFile(wmImgPath);
return img.AddAnimatedImageWatermark(wm, ops);
}

/// <summary>
/// Draw animated image watermark over a static image
/// <para>Notice regarding watermark opacity:</para>
/// <para>If watermark image needs to be resized, first resize the watermark image,
/// then save it to the disc, and read it again with Image.FromFile.</para>
/// </summary>
/// <param name="img">The original image</param>
/// <param name="wmImage">Watermak image</param>
/// <param name="ops">Image watermark options <see cref="ImageWatermarkOptions"/></param>
public static AnimatedImage AddAnimatedImageWatermark(this Image img, AnimatedImage wmImage, ImageWatermarkOptions ops)
{
var fList = new List<Image>();
var animImg = new AnimatedImage(new[] { img });

for (int i = 0; i < wmImage.FramesCount; i++)
{
var clone = img.Clone() as Image;
var imgI = clone.AddImageWatermark(wmImage.Frames[i], ops, false);
fList.Add(imgI);
}

img.Dispose();
wmImage.Dispose();

//animImg.Frames?.Clear();
animImg.Frames = fList;

return animImg;
}
}
}
71 changes: 71 additions & 0 deletions LazZiya.ImageResize/Animated/ImageAnimatedTextWatermark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections.Generic;
using System.Drawing;

namespace LazZiya.ImageResize.Animated
{
/// <summary>
/// Add animated text watermark over image
/// </summary>
public static class ImageAnimatedTextWatermark
{
/// <summary>
/// Add animated text watermark over a static image.
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
public static AnimatedImage AddAnimatedTextWatermark(this Image img, string text)
{
return AddAnimatedTextWatermark(img, text, new TextWatermarkOptions(), new AnimatedTextWatermarkOptions());
}

/// <summary>
/// Add animated text watermark over a static image.
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
/// <param name="animOps"></param>
public static AnimatedImage AddAnimatedTextWatermark(this Image img, string text, AnimatedTextWatermarkOptions animOps)
{
return AddAnimatedTextWatermark(img, text, new TextWatermarkOptions(), animOps);
}

/// <summary>
/// Add animated text watermark over a static image.
/// </summary>
/// <param name="img"></param>
/// <param name="text">text to draw over the image</param>
/// <param name="ops">Text watermark options <see cref="TextWatermarkOptions"/></param>
public static AnimatedImage AddAnimatedTextWatermark(this Image img, string text, TextWatermarkOptions ops)
{
return AddAnimatedTextWatermark(img, text, ops, new AnimatedTextWatermarkOptions());
}

/// <summary>
/// Add animated text watermark over a static image.
/// </summary>
/// <param name="img"></param>
/// <param name="text">text to draw over the image</param>
/// <param name="ops">Text watermark options <see cref="TextWatermarkOptions"/></param>
/// <param name="animOps">Animated text options <see cref="AnimatedTextWatermarkOptions"/></param>
public static AnimatedImage AddAnimatedTextWatermark(this Image img, string text, TextWatermarkOptions ops, AnimatedTextWatermarkOptions animOps)
{
var fList = new List<Image>();
var animImg = new AnimatedImage(new List<Image> { img });

for (int i = 0; i < text.Length; i++)
{
var subStr = text.Substring(0, i);
var clone = img.Clone() as Image;
var frame = clone.AddTextWatermark(subStr, ops);
fList.Add(frame);
}

img.Dispose();

animImg.RepeatCount = 0;
animImg.Frames = fList;

return animImg;
}
}
}
7 changes: 6 additions & 1 deletion LazZiya.ImageResize/LazZiya.ImageResize.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
<RepositoryUrl>https://github.com/LazZiya/ImageResize</RepositoryUrl>
<PackageProjectUrl>https://docs.ziyad.info/en/LazZiya.ImageResize/v4.0/index.md</PackageProjectUrl>
<VersionPrefix>4.0.0</VersionPrefix>
<VersionSuffix>preview1</VersionSuffix>
<VersionSuffix>preview2</VersionSuffix>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<FileVersion>4.0.0.0</FileVersion>
<PackageReleaseNotes>
Updates in preview 2:
- Added support to create animated text watermark
- Added support to create animated image watermark

Previous updates in preview 1:
- Added support for editing animated images (resize, text watewrmark, image water etc.)
- Update ref for System.Drawing.Common https://github.com/dotnet/core/issues/3244
- See all release notes https://github.com/LazZiya/ImageResize/releases
Expand Down
Loading

0 comments on commit 30e893d

Please sign in to comment.