Skip to content

Commit

Permalink
rename to animated image
Browse files Browse the repository at this point in the history
  • Loading branch information
LazZiya committed Feb 10, 2021
1 parent 172e5a4 commit 1deab1b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
namespace LazZiya.ImageResize.Animated
{
/// <summary>
/// Represents an animated gif image with multiple frames
/// Represents an animated image image with multiple frames
/// </summary>
public class AnimatedGif : IDisposable
public class AnimatedImage : IDisposable
{
/// <summary>
/// Gif image as list of frames
Expand Down Expand Up @@ -59,72 +59,86 @@ public class AnimatedGif : IDisposable
public ImageFormat RawFormat { get; }

/// <summary>
/// Create an animated gif from file
/// private constructor
/// </summary>
/// <param name="filePath"></param>
public AnimatedGif(string filePath)
: this(Image.FromFile(filePath))
private AnimatedImage(Image image)
{

if (!ImageAnimator.CanAnimate(image))
throw new BadImageFormatException("This is not an animated gif!");

var dim = new FrameDimension(image.FrameDimensionsList[0]);
FramesCount = image.GetFrameCount(dim);
Size = new Size(image.Width, image.Height);
Frames = new List<Image>();
ImageColorFormat = ImageColorFormats.GetColorFormat((Bitmap)image);
PixelFormat = image.PixelFormat;
HorizontalResolution = image.HorizontalResolution;
VerticalResolution = image.VerticalResolution;
RawFormat = image.RawFormat;

for (int i = 0; i < FramesCount; i++)
{
image.SelectActiveFrame(dim, i);
var frame = image.Clone() as Image;
Frames.Add(frame);
}

image.Dispose();
}

/// <summary>
/// Create an animated gif from a stream
/// Create animated image from file
/// </summary>
/// <param name="stream"></param>
public AnimatedGif(Stream stream)
:this(Image.FromStream(stream))
/// <param name="fileName"></param>
/// <returns></returns>
public static AnimatedImage FromFile(string fileName)
{

var img = Image.FromFile(fileName);
return new AnimatedImage(img);
}

/// <summary>
/// Create an animated gif from HBitmap
/// Create animated image from stream
/// </summary>
/// <param name="hbitmap"></param>
public AnimatedGif(IntPtr hbitmap)
: this(Image.FromHbitmap(hbitmap))
/// <param name="stream"></param>
/// <returns></returns>
public static AnimatedImage FromStream(Stream stream)
{
var img = Image.FromStream(stream);
return new AnimatedImage(img);
}

/// <summary>
/// Create animated image from image
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static AnimatedImage FromImage(Image image)
{
return new AnimatedImage(image);
}

/// <summary>
/// Create an animated gif from HBitmap
/// Create animated image from bitmap
/// </summary>
/// <param name="hbitmap"></param>
/// <param name="hplatte"></param>
public AnimatedGif(IntPtr hbitmap, IntPtr hplatte)
: this(Image.FromHbitmap(hbitmap, hplatte))
/// <returns></returns>
public static AnimatedImage FromHBitmap(IntPtr hbitmap)
{

var img = Image.FromHbitmap(hbitmap);
return new AnimatedImage(img);
}

/// <summary>
/// Create animated gif from Image file
/// Create animated image from bitmap
/// </summary>
/// <param name="image"></param>
public AnimatedGif(Image image)
/// <param name="hbitmap"></param>
/// <param name="hpalatte"></param>
/// <returns></returns>
public static AnimatedImage FromHBitmap(IntPtr hbitmap, IntPtr hpalatte)
{
if (!ImageAnimator.CanAnimate(image))
throw new BadImageFormatException("This is not an animated gif!");

var dim = new FrameDimension(image.FrameDimensionsList[0]);
FramesCount = image.GetFrameCount(dim);
Size = new Size(image.Width, image.Height);
Frames = new List<Image>();

ImageColorFormat = ImageColorFormats.GetColorFormat((Bitmap)image);
PixelFormat = image.PixelFormat;
HorizontalResolution = image.HorizontalResolution;
VerticalResolution = image.VerticalResolution;
RawFormat = image.RawFormat;

for (int i = 0; i < FramesCount; i++)
{
image.SelectActiveFrame(dim, i);
var frame = image.Clone() as Image;
Frames.Add(frame);
}
var img = Image.FromHbitmap(hbitmap, hpalatte);
return new AnimatedImage(img);
}

/// <summary>
Expand Down Expand Up @@ -157,7 +171,7 @@ public void Dispose()
/// <summary>
/// deconstruct
/// </summary>
~AnimatedGif()
~AnimatedImage()
{
this.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace LazZiya.ImageResize.Animated
/// <summary>
/// Add an image watermark to animated gif
/// </summary>
public static class AnimatedGifWaterMark
public static class AnimatedImageImageWatermark
{
/// <summary>
/// Draw image watermark
/// </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 AnimatedGif AddImageWatermark(this AnimatedGif img, string wmImgPath)
public static AnimatedImage AddImageWatermark(this AnimatedImage img, string wmImgPath)
{
var wm = Image.FromFile(wmImgPath);
return img.AddImageWatermark(wm, new ImageWatermarkOptions());
Expand All @@ -24,7 +24,7 @@ public static AnimatedGif AddImageWatermark(this AnimatedGif img, string wmImgPa
/// </summary>
/// <param name="img">The original image</param>
/// <param name="wmImage">Watermark image</param>
public static AnimatedGif AddImageWatermark(this AnimatedGif img, Image wmImage)
public static AnimatedImage AddImageWatermark(this AnimatedImage img, Image wmImage)
{
return img.AddImageWatermark(wmImage, new ImageWatermarkOptions());
}
Expand All @@ -35,7 +35,7 @@ public static AnimatedGif AddImageWatermark(this AnimatedGif img, Image wmImage)
/// <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 AnimatedGif AddImageWatermark(this AnimatedGif img, string wmImgPath, ImageWatermarkOptions ops)
public static AnimatedImage AddImageWatermark(this AnimatedImage img, string wmImgPath, ImageWatermarkOptions ops)
{
var wm = Image.FromFile(wmImgPath);
return img.AddImageWatermark(wm, ops);
Expand All @@ -50,7 +50,7 @@ public static AnimatedGif AddImageWatermark(this AnimatedGif img, string wmImgPa
/// <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 AnimatedGif AddImageWatermark(this AnimatedGif img, Image wmImage, ImageWatermarkOptions ops)
public static AnimatedImage AddImageWatermark(this AnimatedImage img, Image wmImage, ImageWatermarkOptions ops)
{
var fList = new List<Image>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using LazZiya.ImageResize.ColorFormats;
using LazZiya.ImageResize.ResizeMethods;
using LazZiya.ImageResize.Tools;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
Expand All @@ -12,7 +10,7 @@ namespace LazZiya.ImageResize.Animated
/// <summary>
/// Resize images
/// </summary>
public static class AnimatedGifResize
public static class AnimatedImageResize
{
/// <summary>
/// Auto scale image by width or height till longest border (width/height) is equal to new width/height.
Expand All @@ -24,7 +22,7 @@ public static class AnimatedGifResize
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <returns></returns>
public static AnimatedGif Scale(this AnimatedGif img, int newWidth, int newHeight)
public static AnimatedImage Scale(this AnimatedImage img, int newWidth, int newHeight)
{
var resize = new Scale(img.Size, new Size(newWidth, newHeight));

Expand All @@ -42,7 +40,7 @@ public static AnimatedGif Scale(this AnimatedGif img, int newWidth, int newHeigh
/// <param name="newHeight"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static AnimatedGif Scale(this AnimatedGif img, int newWidth, int newHeight, GraphicOptions ops)
public static AnimatedImage Scale(this AnimatedImage img, int newWidth, int newHeight, GraphicOptions ops)
{
var resize = new Scale(img.Size, new Size(newWidth, newHeight));

Expand All @@ -56,7 +54,7 @@ public static AnimatedGif Scale(this AnimatedGif img, int newWidth, int newHeigh
/// <param name="img"></param>
/// <param name="newWidth"></param>
/// <returns></returns>
public static AnimatedGif ScaleByWidth(this AnimatedGif img, int newWidth)
public static AnimatedImage ScaleByWidth(this AnimatedImage img, int newWidth)
{
var resize = new Scale(img.Size, new Size(newWidth, 0));

Expand All @@ -71,7 +69,7 @@ public static AnimatedGif ScaleByWidth(this AnimatedGif img, int newWidth)
/// <param name="newWidth"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static AnimatedGif GifScaleByWidth(this AnimatedGif img, int newWidth, GraphicOptions ops)
public static AnimatedImage GifScaleByWidth(this AnimatedImage img, int newWidth, GraphicOptions ops)
{
var resize = new Scale(img.Size, new Size(newWidth, 0));

Expand All @@ -85,7 +83,7 @@ public static AnimatedGif GifScaleByWidth(this AnimatedGif img, int newWidth, Gr
/// <param name="img"></param>
/// <param name="newHeight"></param>
/// <returns></returns>
public static AnimatedGif ScaleByHeight(this AnimatedGif img, int newHeight)
public static AnimatedImage ScaleByHeight(this AnimatedImage img, int newHeight)
{
var resize = new Scale(img.Size, new Size(0, newHeight));

Expand All @@ -100,7 +98,7 @@ public static AnimatedGif ScaleByHeight(this AnimatedGif img, int newHeight)
/// <param name="newHeight"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static AnimatedGif ScaleByHeight(this AnimatedGif img, int newHeight, GraphicOptions ops)
public static AnimatedImage ScaleByHeight(this AnimatedImage img, int newHeight, GraphicOptions ops)
{
var resize = new Scale(img.Size, new Size(0, newHeight));

Expand All @@ -117,7 +115,7 @@ public static AnimatedGif ScaleByHeight(this AnimatedGif img, int newHeight, Gra
/// <param name="newHeight"></param>
/// <param name="spot"></param>
/// <returns></returns>
public static AnimatedGif ScaleAndCrop(this AnimatedGif img, int newWidth, int newHeight, TargetSpot spot = TargetSpot.Center)
public static AnimatedImage ScaleAndCrop(this AnimatedImage img, int newWidth, int newHeight, TargetSpot spot = TargetSpot.Center)
{
var resize = new ScaleAndCrop(img.Size, new Size(newWidth, newHeight), spot);

Expand All @@ -135,7 +133,7 @@ public static AnimatedGif ScaleAndCrop(this AnimatedGif img, int newWidth, int n
/// <param name="spot"></param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static AnimatedGif ScaleAndCrop(this AnimatedGif img, int newWidth, int newHeight, GraphicOptions ops, TargetSpot spot = TargetSpot.Center)
public static AnimatedImage ScaleAndCrop(this AnimatedImage img, int newWidth, int newHeight, GraphicOptions ops, TargetSpot spot = TargetSpot.Center)
{
var resize = new ScaleAndCrop(img.Size, new Size(newWidth, newHeight), spot);

Expand All @@ -151,7 +149,7 @@ public static AnimatedGif ScaleAndCrop(this AnimatedGif img, int newWidth, int n
/// <param name="newHeight"></param>
/// <param name="spot">target spot to crop and save</param>
/// <returns></returns>
public static AnimatedGif Crop(this AnimatedGif img, int newWidth, int newHeight, TargetSpot spot = TargetSpot.Center)
public static AnimatedImage Crop(this AnimatedImage img, int newWidth, int newHeight, TargetSpot spot = TargetSpot.Center)
{
var resize = new Crop(img.Size, new Size(newWidth, newHeight), spot);
return Resize(img, resize.SourceRect, resize.TargetRect);
Expand All @@ -167,7 +165,7 @@ public static AnimatedGif Crop(this AnimatedGif img, int newWidth, int newHeight
/// <param name="spot">target spot to crop and save</param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static AnimatedGif Crop(this AnimatedGif img, int newWidth, int newHeight, GraphicOptions ops, TargetSpot spot = TargetSpot.Center)
public static AnimatedImage Crop(this AnimatedImage img, int newWidth, int newHeight, GraphicOptions ops, TargetSpot spot = TargetSpot.Center)
{
var resize = new Crop(img.Size, new Size(newWidth, newHeight), spot);
return Resize(img, resize.SourceRect, resize.TargetRect, ops);
Expand All @@ -181,7 +179,7 @@ public static AnimatedGif Crop(this AnimatedGif img, int newWidth, int newHeight
/// can be the whole image or part of it</param>
/// <param name="target">The coordinates of the target image size</param>
/// <returns></returns>
public static AnimatedGif Resize(this AnimatedGif img, Rectangle source, Rectangle target)
public static AnimatedImage Resize(this AnimatedImage img, Rectangle source, Rectangle target)
{
return img.Resize(source, target, new GraphicOptions());
}
Expand All @@ -195,7 +193,7 @@ public static AnimatedGif Resize(this AnimatedGif img, Rectangle source, Rectang
/// <param name="target">The coordinates of the target image size</param>
/// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
/// <returns></returns>
public static AnimatedGif Resize(this AnimatedGif img, Rectangle source, Rectangle target, GraphicOptions ops)
public static AnimatedImage Resize(this AnimatedImage img, Rectangle source, Rectangle target, GraphicOptions ops)
{
// check for CMYK pixel format to use Format32bppArgb
// or use the image pixel format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace LazZiya.ImageResize
/// <summary>
/// Add text watermark over the image.
/// </summary>
public static class AnimatedTextWatermark
public static class AnimatedImageTextWatermark
{

/// <summary>
/// Add text watermark over the image.
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
public static AnimatedGif AddTextWatermark(this AnimatedGif img, string text)
public static AnimatedImage AddTextWatermark(this AnimatedImage img, string text)
{
return AddTextWatermark(img, text, new TextWatermarkOptions());
}
Expand All @@ -26,7 +26,7 @@ public static AnimatedGif AddTextWatermark(this AnimatedGif img, string text)
/// <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 AnimatedGif AddTextWatermark(this AnimatedGif img, string text, TextWatermarkOptions ops)
public static AnimatedImage AddTextWatermark(this AnimatedImage img, string text, TextWatermarkOptions ops)
{
var fList = new List<Image>();

Expand Down
9 changes: 5 additions & 4 deletions LazZiya.ImageResize/LazZiya.ImageResize.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
<PackageTags>asp.net,core,.net,dotnet,image,resize,text,overlay</PackageTags>
<RepositoryUrl>https://github.com/LazZiya/ImageResize</RepositoryUrl>
<PackageProjectUrl>http://demo.ziyad.info/en/ImageResize</PackageProjectUrl>
<VersionPrefix>3.1.0</VersionPrefix>
<VersionSuffix>beta1</VersionSuffix>
<AssemblyVersion>3.1.0.0</AssemblyVersion>
<FileVersion>3.1.0.0</FileVersion>
<VersionPrefix>4.0.0</VersionPrefix>
<VersionSuffix>preview1</VersionSuffix>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<FileVersion>4.0.0.0</FileVersion>
<PackageReleaseNotes>
- Added support for animated gif (resize, text watewrmark, image water
- Update ref for System.Drawing.Common https://github.com/dotnet/core/issues/3244
- See all release notes https://github.com/LazZiya/ImageResize/releases
</PackageReleaseNotes>
Expand Down
Loading

0 comments on commit 1deab1b

Please sign in to comment.