-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
671 additions
and
543 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
8 changes: 2 additions & 6 deletions
8
...Resize/Watermark/ImageWatermarkOptions.cs → LazZiya.ImageResize/ImageWatermarkOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
7 changes: 2 additions & 5 deletions
7
...eResize/Watermark/TextWatermarkOptions.cs → LazZiya.ImageResize/TextWatermarkOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.