Created
March 27, 2018 13:23
-
-
Save cartermp/f40eb23427d62f689757388f0bb61190 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
private static void WriteWatermark(string watermarkContent, Stream originalImage, Stream newImage) | |
{ | |
originalImage.Position = 0; | |
using (Image inputImage = Image.FromStream(originalImage, true)) | |
using (Graphics graphic = Graphics.FromImage(inputImage)) | |
{ | |
Font font = new Font("Georgia", 12, FontStyle.Bold); | |
SizeF textSize = graphic.MeasureString(watermarkContent, font); | |
float xCenterOfImg = (inputImage.Width / 2); | |
float yPosFromBottom = (int)(inputImage.Height * 0.90) - (textSize.Height / 2); | |
graphic.SmoothingMode = SmoothingMode.HighQuality; | |
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; | |
graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; | |
StringFormat StrFormat = new StringFormat | |
{ | |
Alignment = StringAlignment.Center | |
}; | |
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0)); | |
graphic.DrawString(watermarkContent, font, semiTransBrush2, xCenterOfImg + 1, yPosFromBottom + 1, StrFormat); | |
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255)); | |
graphic.DrawString(watermarkContent, font, semiTransBrush, xCenterOfImg, yPosFromBottom, StrFormat); | |
graphic.Flush(); | |
inputImage.Save(newImage, ImageFormat.Jpeg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment