Skip to content

Commit

Permalink
tbd: image quality
Browse files Browse the repository at this point in the history
  • Loading branch information
LazZiya committed Dec 8, 2019
1 parent 3628183 commit 1b6c63a
Show file tree
Hide file tree
Showing 15 changed files with 847 additions and 137 deletions.
8 changes: 4 additions & 4 deletions LazZiya.sln → ImageResize.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Global
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Debug|x64.ActiveCfg = Debug|x64
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Debug|x64.Build.0 = Debug|x64
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Debug|x64.ActiveCfg = Debug|Any CPU
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Debug|x64.Build.0 = Debug|Any CPU
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Release|Any CPU.Build.0 = Release|Any CPU
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Release|x64.ActiveCfg = Release|x64
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Release|x64.Build.0 = Release|x64
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Release|x64.ActiveCfg = Release|Any CPU
{23C49B9A-D554-4F5B-8201-5F5C69FF41C0}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
21 changes: 21 additions & 0 deletions LazZiya.ImageResize/Exceptions/ImageResizeException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ namespace LazZiya.ImageResize.Exceptions
/// </summary>
public class ImageResizeException : Exception
{
/// <summary>
/// Raise an image resize exeption
/// </summary>
public ImageResizeException()
{
}

/// <summary>
/// Raise an image resize exeption
/// </summary>
public ImageResizeException(string message) : base(message)
{
}

/// <summary>
/// Raise an image resize exeption
/// </summary>
public ImageResizeException(string message, Exception innerException) : base(message, innerException)
{
}

/// <summary>
/// Image resize result
/// </summary>
Expand Down
24 changes: 14 additions & 10 deletions LazZiya.ImageResize/ImageResize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace LazZiya.ImageResize
/// <summary>
/// Resize images
/// </summary>
public abstract class ImageResize
public static class ImageResize
{
/// <summary>
/// Auto scale image by width or height till longest border (width/height) is equal to new width/height.
Expand Down Expand Up @@ -97,23 +97,28 @@ public static Image Crop(Image img, int newWidth, int newHeight, TargetSpot spot
/// 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 Image Resize(Image img, Rectangle source, Rectangle target)
public static Image Resize(this Image img, Rectangle source, Rectangle target)
{
Bitmap outputImage = new Bitmap(target.Width, target.Height, img.PixelFormat);

outputImage.SetResolution(img.HorizontalResolution, img.VerticalResolution);

try
{
Graphics graphics = Graphics.FromImage(outputImage);
using (var graphics = Graphics.FromImage(outputImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

graphics.DrawImage(
img,
target,
source,
GraphicsUnit.Pixel);
graphics.DrawImage(
img,
target,
source,
GraphicsUnit.Pixel);

graphics.Dispose();
}
}
catch (Exception e)
{
Expand All @@ -124,7 +129,6 @@ public static Image Resize(Image img, Rectangle source, Rectangle target)
Value = e.Message
});
}

return outputImage;
}
}
Expand Down
54 changes: 26 additions & 28 deletions LazZiya.ImageResize/ImageWatermark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,70 @@
namespace LazZiya.ImageResize
{
/// <summary>
///
/// Draw image watermark
/// </summary>
public static class ImageWatermark
{

/// <summary>
///
/// Draw image watermark
/// </summary>
/// <param name="img"></param>
/// <param name="wmImgPath"></param>
/// <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 Image AddImageWatermark(this Image img, string wmImgPath)
{
var wm = Image.FromFile(wmImgPath);
return img.AddImageWatermark(wm, new ImageWatermarkOptions());
}

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

/// <summary>
///
/// Draw image watermark
/// </summary>
/// <param name="img"></param>
/// <param name="wmImgPath"></param>
/// <param name="ops"></param>
/// <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 Image AddImageWatermark(this Image img, string wmImgPath, ImageWatermarkOptions ops)
{
var wm = Image.FromFile(wmImgPath);
return img.AddImageWatermark(wm, ops);
}

/// <summary>
/// Add image watermark
/// Draw image watermark
/// </summary>
/// <param name="img">The main image</param>
/// <param name="wmImage">full path to the image that will be used as watermark</param>
/// <param name="ops">Image watermark options</param>
/// <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 Image AddImageWatermark(this Image img, Image wmImage, ImageWatermarkOptions ops)
{
if (ops.Opacity > 0)
{
var graphics = Graphics.FromImage(img);
using (var graphics = Graphics.FromImage(img))
{
graphics.SmoothingMode = SmoothingMode.None;
graphics.CompositingMode = CompositingMode.SourceOver;

graphics.SmoothingMode = SmoothingMode.None;
graphics.CompositingMode = CompositingMode.SourceOver;
if (ops.Opacity < 100)
wmImage = ImageOpacity.ChangeImageOpacityMethod1(wmImage, ops.Opacity);

if (ops.Opacity < 100)
wmImage = ImageOpacity.ChangeImageOpacityMethod1(wmImage, ops.Opacity);
var wmW = wmImage.Width;
var wmH = wmImage.Height;

var wmW = wmImage.Width;
var wmH = wmImage.Height;
var drawingPoint = ImageWatermarkPosition.ImageWatermarkPos(img.Width, img.Height, wmW, wmH, ops.Location, ops.Margin);

var drawingPoint = ImageWatermarkPosition.ImageWatermarkPos(img.Width, img.Height, wmW, wmH, ops.Location, ops.Margin);

graphics.DrawImage(wmImage, drawingPoint.X, drawingPoint.Y, wmW, wmH);

graphics.Dispose();
graphics.DrawImage(wmImage, drawingPoint.X, drawingPoint.Y, wmW, wmH);
}
}

return img;
}
}
Expand Down
15 changes: 9 additions & 6 deletions LazZiya.ImageResize/ImageWatermarkOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
namespace LazZiya.ImageResize
{
/// <summary>
/// Define options for adding text watermark over the image, like text color, opacity, text outline, etc.
/// Define options for adding image watermark over the image, like margin, opacity, and location.
/// </summary>
public class ImageWatermarkOptions
{
/// <summary>
/// Margin in pixels. Depends on watermark location.
/// Margin in pixels. Depends on watermark location. default value 10
/// </summary>
public int Margin { get; set; } = 10;

/// <summary>
/// The location to draw the image watermark. Choose from pre-defined 9 main locations (3 cols, 3 rows)
/// The location to draw the image watermark. Choose from pre-defined 9 main locations (3 cols, 3 rows).
/// Default value TargetSpot.TopRight.
/// See <see cref="TargetSpot"/>
/// </summary>
public TargetSpot Location { get; set; } = TargetSpot.BottomLeft;
public TargetSpot Location { get; set; } = TargetSpot.TopRight;

/// <summary>
/// Set opacity value of the image watermark
/// Set opacity value of the image watermark (0 - 100).
/// Default value 100 full color.
/// </summary>
[Range(0, 100)]
public int Opacity { get; set; }
public int Opacity { get; set; } = 100;
}
}
31 changes: 18 additions & 13 deletions LazZiya.ImageResize/LazZiya.ImageResize.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,43 @@
<Authors>Ziya Mollamahmut</Authors>
<Company>Ziyad.info</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Simple tool for image resize, and add text over image for ASP.NET Core</Description>
<Description>Easily resize images, add text watermark with outline, add image watermark with transparency.</Description>
<Copyright>Ziyad.info</Copyright>
<PackageTags>asp.net,core,.net,dotnet,image,resize,text,overlay</PackageTags>
<RepositoryUrl>https://github.com/LazZiya/ImageResize</RepositoryUrl>
<PackageProjectUrl>http://ziyad.info/en/articles/29-Image_Resize_for_dotNetCore</PackageProjectUrl>
<Platforms>AnyCPU;x64</Platforms>
<Version>3.0.0-preview1</Version>
<Version>3.0.0-preview2</Version>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<FileVersion>3.0.0.0</FileVersion>
<PackageReleaseNotes>image watermark with opacity settings</PackageReleaseNotes>
<PackageReleaseNotes>
- .NetStandard 2.0 support
- New: TextOutline for text watermark
- New: AddTextWatermark methods
- New: AddImageWatermark methods
- Obsolete: TextWatermark, ImageWatermark extensions are obsolete and will be removed in a feature release
- Overall performance improvments
</PackageReleaseNotes>
<PackageIconUrl>https://raw.githubusercontent.com/LazZiya/ImageResize/master/LazZiya.ImageResize/files/icon.png</PackageIconUrl>
<PackageIcon>icon.png</PackageIcon>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>files\LazZiya.ImageResize.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>LazZiya.ImageResize.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
</ItemGroup>

<ItemGroup>
<None Update="files\icon.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="files\icon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="files\license.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="files\icon.png" Pack="true" Visible="false" PackagePath="\" />
<None Include="files\icon.ico" Pack="true" Visible="false" PackagePath="\" />
<None Include="files\license.txt" Pack="true" PackagePath="\" />
</ItemGroup>

</Project>
Loading

0 comments on commit 1b6c63a

Please sign in to comment.