■ 워터마크 사용하기

----------------------------------------------------------------------------------------------------


TestProject.zip


WatemarkHelper.cs

 

 

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

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요