Skip to content

Commit

Permalink
rearrange file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
LazZiya committed Dec 7, 2019
1 parent 6cedf7d commit 3628183
Show file tree
Hide file tree
Showing 18 changed files with 671 additions and 543 deletions.
62 changes: 0 additions & 62 deletions LazZiya.ImageResize/ImageExtensions.cs

This file was deleted.

30 changes: 16 additions & 14 deletions LazZiya.ImageResize/ImageResize.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using LazZiya.ImageResize.Exceptions;
using LazZiya.ImageResize.ResizeMethods;
using LazZiya.ImageResize.Tools;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace LazZiya.ImageResize
{
/// <summary>
/// Resize images
/// </summary>
public static class ImageResize
public abstract class ImageResize
{
/// <summary>
/// auto scale image by width or height till longest border (width/height) is equal to new width/height
/// final image aspect ratio is equal to original image aspect ratio
/// if the aspect ratio of new w/h != aspect ratio of original image then;
/// Auto scale image by width or height till longest border (width/height) is equal to new width/height.
/// Final image aspect ratio is equal to original image aspect ratio.
/// If the aspect ratio of new w/h != aspect ratio of original image then
/// one border will be in different size than the given value in order to keep original aspect ratio
/// </summary>
/// <param name="img"></param>
Expand All @@ -28,8 +30,8 @@ public static Image Scale(Image img, int newWidth, int newHeight)
}

/// <summary>
/// scale image by width and keep same aspect ratio of target image same as the original image
/// height will be adjusted automatically
/// Scale image by width and keep same aspect ratio of target image same as the original image.
/// Height will be adjusted automatically
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
Expand All @@ -42,8 +44,8 @@ public static Image ScaleByWidth(Image img, int newWidth)
}

/// <summary>
/// scale image by height and keep same aspect ratio of target image same as the original image
/// width will be adjusted automatically
/// Scale image by height and keep same aspect ratio of target image same as the original image.
/// Width will be adjusted automatically
/// </summary>
/// <param name="img"></param>
/// <param name="newHeight"></param>
Expand All @@ -56,9 +58,9 @@ public static Image ScaleByHeight(Image img, int newHeight)
}

/// <summary>
/// scale target image till shortest border are equal to target value,
/// then crop the additonal pixels from the longest border
/// final image aspect ratio is equal to the given new width/height
/// Scale target image till shortest border are equal to target value,
/// then crop the additonal pixels from the longest border.
/// Final image aspect ratio is equal to the given new width/height
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
Expand All @@ -73,8 +75,8 @@ public static Image ScaleAndCrop(Image img, int newWidth, int newHeight, TargetS
}

/// <summary>
/// directly crop original image without scaling it
/// final image aspect ratio is equal to given new width/height
/// Directly crop original image without scaling it.
/// Final image aspect ratio is equal to given new width/height
/// </summary>
/// <param name="img"></param>
/// <param name="newWidth"></param>
Expand All @@ -88,7 +90,7 @@ public static Image Crop(Image img, int newWidth, int newHeight, TargetSpot spot
}

/// <summary>
/// specify custom resize options
/// Specify custom resize options
/// </summary>
/// <param name="img">the image to resize</param>
/// <param name="source">The coordinates to read as source from the image,
Expand Down
77 changes: 77 additions & 0 deletions LazZiya.ImageResize/ImageWatermark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using LazZiya.ImageResize.Tools;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace LazZiya.ImageResize
{
/// <summary>
///
/// </summary>
public static class ImageWatermark
{

/// <summary>
///
/// </summary>
/// <param name="img"></param>
/// <param name="wmImgPath"></param>
public static Image AddImageWatermark(this Image img, string wmImgPath)
{
var wm = Image.FromFile(wmImgPath);
return img.AddImageWatermark(wm, new ImageWatermarkOptions());
}

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

/// <summary>
///
/// </summary>
/// <param name="img"></param>
/// <param name="wmImgPath"></param>
/// <param name="ops"></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
/// </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>
public static Image AddImageWatermark(this Image img, Image wmImage, ImageWatermarkOptions ops)
{
if (ops.Opacity > 0)
{
var graphics = Graphics.FromImage(img);

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

if (ops.Opacity < 100)
wmImage = ImageOpacity.ChangeImageOpacityMethod1(wmImage, ops.Opacity);

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

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();
}

return img;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace LazZiya.ImageResize.Watermark
namespace LazZiya.ImageResize
{
/// <summary>
/// Define options for adding text watermark over the image, like text color, opacity, text outline, etc.
Expand Down
33 changes: 33 additions & 0 deletions LazZiya.ImageResize/SaveImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using LazZiya.ImageResize.Tools;
using System.Drawing;
using System.Drawing.Imaging;

namespace LazZiya.ImageResize
{
public static class SaveImage
{
/// <summary>
/// Save the image to the specified path
/// </summary>
/// <param name="img">Image to save</param>
/// <param name="path">Full path including file name and extension to save the image to</param>
public static void SaveAs(this Image img, string path)
{
ImageCodecInfo myImageCodecInfo;
Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;

var dotIndex = path.LastIndexOf('.');
var ext = path.Substring(dotIndex, path.Length - dotIndex - 1);

myImageCodecInfo = EncoderInfo.GetEncoderInfo(ext);
myEncoder = Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;

img.Save(path, myImageCodecInfo, myEncoderParameters);
}
}
}
90 changes: 90 additions & 0 deletions LazZiya.ImageResize/TextWatermark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using LazZiya.ImageResize.Tools;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;

namespace LazZiya.ImageResize
{
public static class TextWatermark
{

/// <summary>
/// Add text watermark over the image.
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
public static Image AddTextWatermark(this Image img, string text)
{
return AddTextWatermark(img, text, new TextWatermarkOptions());
}

/// <summary>
/// Add text watermark over the image.
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
/// <param name="ops"></param>
public static Image AddTextWatermark(this Image img, string text, TextWatermarkOptions ops)
{
using (var graphics = Graphics.FromImage(img))
{
var bgPos = TextWatermarkPosition.SetBGPos(img.Width, img.Height, ops.FontSize, ops.Location, ops.Margin);

var sf = new StringFormat()
{
FormatFlags = StringFormatFlags.NoWrap
};

graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
//graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
//graphics.TextContrast = 12;
//graphics.CompositingMode = CompositingMode.SourceOver;
//graphics.InterpolationMode = InterpolationMode.High;

// Draw background if not fully transparent
if (ops.BGColor.A > 0)
{
var bgBrush = new SolidBrush(ops.BGColor);
graphics.FillRectangle(bgBrush, bgPos);
}

// Set font to use
var ff = new FontFamily(ops.FontName);
var font = new Font(ff, ops.FontSize, ops.FontStyle, GraphicsUnit.Pixel);

// Measure text size
var textMetrics = graphics.MeasureString(text, font, img.Width, sf);
var beforeText = TextWatermarkPosition.SetTextAlign(textMetrics, img.Width, ops.Location);
var drawPoint = new PointF(beforeText, bgPos.Y + (bgPos.Height / 4));

var outlineBrush = new SolidBrush(ops.OutlineColor);

using (var pen = new Pen(outlineBrush, ops.OutlineWidth))
{
using (var p = new GraphicsPath())
{
p.AddString(text, ff, (int)ops.FontStyle, ops.FontSize, drawPoint, sf);

// Draw text outline if not fully transparent
if (ops.OutlineColor.A > 0)
{
graphics.DrawPath(pen, p);
}

// Draw text if not fully transparent
if (ops.TextColor.A > 0)
{
var textBrush = new SolidBrush(ops.TextColor);
graphics.FillPath(textBrush, p);
}
}
}
}

return img;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Drawing;

namespace LazZiya.ImageResize.Watermark
namespace LazZiya.ImageResize
{
/// <summary>
/// Define options for adding text watermark over the image, like text color, opacity, text outline, etc.
Expand Down
Loading

0 comments on commit 3628183

Please sign in to comment.