■ 워터마크를 사용하는 방법을 보여준다.
▶ WatemarkHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
using System.Drawing; using System.Drawing.Imaging; namespace TestProject { /// <summary> /// 워터마크 헬퍼 /// </summary> public class WatermarkHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 워터마크 이미지 구하기 - GetWatermarkImage(sourceImageFilePath, watermarkFont, watermarkTextOpacity, watermarkTextBrush, watermarkText) /// <summary> /// 워터마크 이미지 구하기 /// </summary> /// <param name="sourceImageFilePath">소스 이미지 파일 경로</param> /// <param name="watermarkFont">워터마크 폰트</param> /// <param name="watermarkTextOpacity">워터마크 텍스트 투명도</param> /// <param name="watermarkTextBrush">워터마크 텍스트 브러시</param> /// <param name="watermarkText">워터마크 텍스트</param> /// <returns>워터마크 이미지</returns> public static Image GetWatermarkImage(string sourceImageFilePath, Font watermarkFont, float watermarkTextOpacity, Brush watermarkTextBrush, string watermarkText) { Image sourceImage = Image.FromFile(sourceImageFilePath); Graphics sourceImageGraphics = Graphics.FromImage(sourceImage); SizeF watermarkTextSize = sourceImageGraphics.MeasureString(watermarkText, watermarkFont); Bitmap targetImage = new Bitmap(sourceImage.Width, sourceImage.Height); Bitmap watermarkImage = new Bitmap((int)watermarkTextSize.Width, (int)watermarkTextSize.Height); watermarkImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution); Graphics watermarkImageGraphics = Graphics.FromImage(watermarkImage); watermarkImageGraphics.PageUnit = GraphicsUnit.Point; watermarkImageGraphics.Clear(Color.Empty); watermarkImageGraphics.DrawString(watermarkText, watermarkFont, watermarkTextBrush, new RectangleF(0, 0, watermarkTextSize.Width, watermarkTextSize.Height), StringFormat.GenericDefault); Graphics targetImageGraphics = Graphics.FromImage(targetImage); float[][] colorMatrixArray = { new float [] {1, 0, 0, 0 , 0}, new float [] {0, 1, 0, 0 , 0}, new float [] {0, 0, 1, 0 , 0}, new float [] {0, 0, 0, watermarkTextOpacity / 100f, 0}, new float [] {0, 0, 0, 0 , 1} }; ColorMatrix colorMatrix = new ColorMatrix(colorMatrixArray); ImageAttributes imageAttributes = new ImageAttributes(); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); targetImageGraphics.DrawImage(sourceImage, 0, 0, sourceImage.Width, sourceImage.Height); targetImageGraphics.DrawImage ( watermarkImage, new Rectangle ( sourceImage.Width - watermarkImage.Width - 5, sourceImage.Height - watermarkImage.Height - 5, watermarkImage.Width, watermarkImage.Height ), 0f, 0f, watermarkImage.Width, watermarkImage.Height, GraphicsUnit.Pixel, imageAttributes ); return targetImage; } #endregion } } |