Skip to content

Commit

Permalink
new project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
LazZiya committed Feb 24, 2019
1 parent 5f8f8d8 commit f69d749
Show file tree
Hide file tree
Showing 19 changed files with 495 additions and 548 deletions.
80 changes: 0 additions & 80 deletions LazZiya.ImageResize/Enums.cs

This file was deleted.

12 changes: 12 additions & 0 deletions LazZiya.ImageResize/Exceptions/FailureReasonType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace LazZiya.ImageResize.Exceptions
{
public enum FailureReasonType
{
None,
EncoderNotFound,
ExtensionNotSupported,
UnknownImageFormatGuid,
ZeroSizeNotAllowed,
GraphicsException
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace LazZiya.ImageResize
namespace LazZiya.ImageResize.Exceptions
{
public class ImageResizeException : Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace LazZiya.ImageResize
namespace LazZiya.ImageResize.Exceptions
{
public class ImageResizeResult
{
Expand Down
174 changes: 3 additions & 171 deletions LazZiya.ImageResize/ImageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,156 +1,12 @@
using System.Drawing;
using System.Drawing.Drawing2D;
using LazZiya.ImageResize.Exceptions;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Globalization;

namespace LazZiya.ImageResize
{
public static class ImageExtensions
{
/// <summary>
/// add watermark text over image
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
/// <param name="color">hex8 Color e.g. #77FFFFFF</param>
/// <param name="bgColor">hex8 Color e.g. #00000000</param>
/// <param name="fontFamily"></param>
/// <param name="size"></param>
/// <param name="align"></param>
/// <param name="vAlign"></param>
/// <param name="style"></param>
public static void TextWatermark(this Image img,
string text,
string color = "#77FFFFFF", string bgColor = "#00000000",
string fontFamily = "Arial", int size = 24,
TargetSpot spot = TargetSpot.BottomLeft, FontStyle style = FontStyle.Regular, int margin = 10)
{
var graphics = Graphics.FromImage(img);

graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.TextContrast = 12;
graphics.SmoothingMode = SmoothingMode.None;
graphics.CompositingMode = CompositingMode.SourceOver;

var _bgAlpha = int.Parse(bgColor.Substring(1, 2), NumberStyles.HexNumber);
var _bgColor = bgColor.Substring(3, 6);
var bgBrush = new SolidBrush(
Color.FromArgb(_bgAlpha, ColorTranslator.FromHtml($"#{_bgColor}")));

var rectPos = SetBGPos(img.Width, img.Height, size, spot, margin);
graphics.FillRectangle(bgBrush, rectPos);

var textFont = new Font(fontFamily, size, style, GraphicsUnit.Pixel);
var _alpha = int.Parse(color.Substring(1, 2), NumberStyles.HexNumber);
var _color = color.Substring(3, 6);
var textBrush = new SolidBrush(
Color.FromArgb(_alpha, ColorTranslator.FromHtml($"#{_color}")));

var textMetrics = graphics.MeasureString(text, textFont);
var beforeText = SetTextAlign(textMetrics, img.Width, spot);

var drawPoint = new PointF(beforeText, rectPos.Y + (rectPos.Height / 4));
graphics.DrawString(text, textFont, textBrush, drawPoint);

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, int margin = 10, int opacity = 35)
{
if (opacity > 0)
{
var graphics = Graphics.FromImage(img);

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

var wmImage = Image.FromFile(wmFileName);

if (opacity < 100)
wmImage = ImageOpacity.ChangeImageOpacity(wmImage, opacity);

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

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

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

graphics.Dispose();
}
}

private static PointF ImageWatermarkPos(int imgWidth, int imgHeight, int wmWidth, int wmHeight, TargetSpot spot, int margin)
{
PointF point;

switch (spot)
{
case TargetSpot.BottomLeft: point = new PointF(margin, imgHeight - (wmHeight + margin)); break;
case TargetSpot.BottomMiddle: point = new PointF((imgWidth / 2) - (wmWidth / 2), imgHeight - (wmHeight + margin)); break;
case TargetSpot.BottomRight: point = new PointF(imgWidth - (wmWidth + margin), imgHeight - (wmHeight + margin)); break;
case TargetSpot.MiddleLeft: point = new PointF(margin, (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 + margin), (imgHeight / 2) - (wmHeight / 2)); break;
case TargetSpot.TopLeft: point = new PointF(margin, margin); break;
case TargetSpot.TopMiddle: point = new PointF((imgWidth / 2) - (wmWidth / 2), margin); break;

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

return point;
}

/// <summary>
/// watermark text pos
/// </summary>
/// <param name="imgWidth"></param>
/// <param name="imgHeight"></param>
/// <param name="fontSize"></param>
/// <param name="pos"></param>
/// <returns></returns>
private static Rectangle SetBGPos(int imgWidth, int imgHeight, int fontSize, TargetSpot spot, int margin)
{
Rectangle rect;

var bgHeight = fontSize * 2;

switch (spot)
{
case TargetSpot.TopLeft:
case TargetSpot.TopMiddle:
case TargetSpot.TopRight:
rect = new Rectangle(0, margin, imgWidth, bgHeight);
break;

case TargetSpot.MiddleLeft:
case TargetSpot.MiddleRight:
case TargetSpot.Center:
rect = new Rectangle(0, imgHeight / 2 - bgHeight / 2, imgWidth, bgHeight);
break;

case TargetSpot.BottomLeft:
case TargetSpot.BottomMiddle:
case TargetSpot.BottomRight:
default:
rect = new Rectangle(0, imgHeight - bgHeight - margin, imgWidth, bgHeight);
break;
}

return rect;
}

private static ImageCodecInfo GetEncoderInfo(string ext)
public static ImageCodecInfo GetEncoderInfo(string ext)
{
int j;

Expand All @@ -172,30 +28,6 @@ private static ImageCodecInfo GetEncoderInfo(string ext)
});
}

private static int SetTextAlign(SizeF textMetrics, int imgWidth, TargetSpot spot)
{
int space;
switch (spot)
{
case TargetSpot.BottomMiddle:
case TargetSpot.TopMiddle:
case TargetSpot.Center:
space = (int)(imgWidth - textMetrics.Width) / 2; break;

case TargetSpot.BottomRight:
case TargetSpot.MiddleRight:
case TargetSpot.TopRight:
space = (int)(imgWidth - textMetrics.Width) - 5; break;

case TargetSpot.BottomLeft:
case TargetSpot.MiddleLeft:
case TargetSpot.TopLeft:
default: space = 5; break;
}

return space;
}

public static void SaveAs(this Image img, string path)
{
ImageCodecInfo myImageCodecInfo;
Expand Down
64 changes: 64 additions & 0 deletions LazZiya.ImageResize/ImageFormats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using LazZiya.ImageResize.Exceptions;
using System;
using System.Drawing.Imaging;

namespace LazZiya.ImageResize
{
public class ImageFormats
{
/// <summary>
/// return image format by comparing the ImageFormat.Guid param
/// </summary>
/// <param name="guid"></param>
/// <returns>System.Drawing.Imaging.ImageFormat</returns>
public static ImageFormat GetImageFormat(Guid guid)
{
return
guid == ImageFormat.Bmp.Guid ? ImageFormat.Bmp :
guid == ImageFormat.Emf.Guid ? ImageFormat.Emf :
guid == ImageFormat.Exif.Guid ? ImageFormat.Exif :
guid == ImageFormat.Gif.Guid ? ImageFormat.Gif :
guid == ImageFormat.Icon.Guid ? ImageFormat.Icon :
guid == ImageFormat.Jpeg.Guid ? ImageFormat.Jpeg :
guid == ImageFormat.MemoryBmp.Guid ? ImageFormat.MemoryBmp :
guid == ImageFormat.Png.Guid ? ImageFormat.Png :
guid == ImageFormat.Tiff.Guid ? ImageFormat.Tiff :
guid == ImageFormat.Wmf.Guid ? ImageFormat.Wmf :

throw new ImageResizeException(new ImageResizeResult()
{
Reason = FailureReasonType.UnknownImageFormatGuid,
Success = false,
Value = guid.ToString()
});
}

/// <summary>
/// return image format by reading file extension
/// </summary>
/// <param name="fileName"></param>
/// <returns>System.Drawing.Imaging.ImageFormat</returns>
public static ImageFormat GetImageFormat(string fileName)
{
var dotIndex = fileName.LastIndexOf('.');
var ext = fileName.Substring(dotIndex, fileName.Length - dotIndex).ToLower();

return
ext == ".bmp" ? ImageFormat.Bmp :
ext == ".emf" ? ImageFormat.Emf :
ext == ".exif" ? ImageFormat.Exif :
ext == ".gif" ? ImageFormat.Gif :
ext == ".icon" ? ImageFormat.Icon :
ext == ".jpg" ? ImageFormat.Jpeg :
ext == ".png" ? ImageFormat.Png :
ext == ".tiff" ? ImageFormat.Tiff :
ext == ".wmf" ? ImageFormat.Wmf :
throw new ImageResizeException(new ImageResizeResult()
{
Reason = FailureReasonType.ExtensionNotSupported,
Success = false,
Value = ext
});
}
}
}
Loading

0 comments on commit f69d749

Please sign in to comment.