첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

using TestProject.Properties;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 영역 타입 - AreaType

        /// <summary>
        /// 영역 타입
        /// </summary>
        private enum AreaType
        {
            /// <summary>
            /// 스트라이프
            /// </summary>
            Stripe,

            /// <summary>
            /// 대각선 스트라이프
            /// </summary>
            DiagonalStripe,

            /// <summary>
            /// 도트
            /// </summary>
            Dot,

            /// <summary>
            /// 텍스트
            /// </summary>
            Text,
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 브러시 문자열
        /// </summary>
        private string brushString =
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "                                                   +
            "In lacinia massa enim, non eleifend augue luctus sed. "                                                      +
            "Praesent tortor nisi, blandit vel semper vel, malesuada ultrices neque. "                                    +
            "Sed neque augue, laoreet sed quam a, sodales consequat libero. "                                             +
            "In euismod vulputate tortor, vel sodales ante ullamcorper commodo. "                                         + 
            "Pellentesque eget nunc at nisi finibus auctor sit amet sit amet dui. "                                       +
            "Sed eget enim quis velit aliquam finibus eget at nisl. "                                                     +
            "In hac habitasse platea dictumst. "                                                                          +
            "Interdum et malesuada fames ac ante ipsum primis in faucibus. "                                              +
            "Suspendisse libero nunc, mattis ac libero vitae, malesuada tincidunt ante. "                                 +
            "Curabitur enim metus, efficitur pharetra ex ac, molestie eleifend tellus. "                                  +
            "Duis sit amet magna augue. "                                                                                 +
            "Ut et tempor lacus. "                                                                                        +
            "Donec imperdiet magna nec quam congue mollis. "                                                              +
            "Pellentesque arcu ipsum, interdum non erat euismod, sodales pharetra erat."                                  +
            "Duis feugiat, urna quis fringilla eleifend, nibh ex condimentum velit, at pulvinar arcu ante sit amet sem. " +
            "Nulla at posuere augue. Nunc tellus diam, egestas vitae ante id, congue pellentesque lorem. "                +
            "Nunc commodo id orci nec maximus. "                                                                          +
            "Quisque blandit congue rutrum. "                                                                             +
            "Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. "                     +
            "Proin et cursus nisl, sollicitudin ultrices dolor. "                                                         +
            "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;"                     +
            "Sed hendrerit elementum metus, a gravida felis placerat vitae. "                                             +
            "Morbi viverra ante felis, id dictum nisi egestas eget. "                                                     +
            "Sed auctor, nulla sed blandit rhoncus, elit erat efficitur massa, id suscipit eros ex eu lacus. "            +
            "Donec ligula ante, vestibulum quis laoreet vitae, placerat ac lectus. "                                      +
            "Pellentesque faucibus vulputate dolor id faucibus. "                                                         +
            "Pellentesque quis erat in nisi blandit lobortis. "                                                           +
            "Cras at efficitur elit. "                                                                                    +
            "In commodo urna at turpis semper semper. "                                                                   +
            "Phasellus felis ligula, hendrerit ut enim et, vulputate volutpat ligula. Nullam aliquam, "                   +
            "lacus ac consectetur blandit, justo nisi rhoncus felis, nec gravida felis mauris a lorem. "                  +
            "Pellentesque blandit arcu justo, quis hendrerit turpis tristique vitae. "                                    +
            "Nam id urna eget ipsum tincidunt viverra. "                                                                  +
            "Etiam laoreet, risus quis vehicula lobortis, dui tortor cursus mi, viverra cursus dui leo et tortor. "       +
            "Suspendisse vehicula dignissim arcu. "                                                                       +
            "In vehicula neque enim, eget mollis elit mollis vel. "                                                       +
            "Morbi a nunc congue, ullamcorper est id, auctor leo.";

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            Load += Form_Load;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            Bitmap bitmap = Resources.fruit_small;

            Bitmap monochromeBitmap = GetMonochromeBitmap(bitmap);

            int margin = this.pictureBox1.Left;

            this.pictureBox1.Image = PlaceColorArea(monochromeBitmap, bitmap, AreaType.Stripe        , 10, 1);
            this.pictureBox2.Image = PlaceColorArea(monochromeBitmap, bitmap, AreaType.DiagonalStripe, 10, 1);
            this.pictureBox3.Image = PlaceColorArea(monochromeBitmap, bitmap, AreaType.Dot           , 10, 2);
            this.pictureBox4.Image = monochromeBitmap;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 모노크롬 비트맵 구하기 - GetMonochromeBitmap(image)

        /// <summary>
        /// 모노크롬 비트맵 구하기
        /// </summary>
        /// <param name="image">이미지</param>
        /// <returns>모노크롬 미트맵</returns>
        private Bitmap GetMonochromeBitmap(Image image)
        {
            ColorMatrix colorMatrix = new ColorMatrix
            (
                new float[][]
                {
                    new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
                    new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
                    new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
                    new float[] { 0     , 0     , 0     , 1, 0 },
                    new float[] { 0     , 0     , 0     , 0, 1 }
                }
            );

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.SetColorMatrix(colorMatrix);

            Point[] pointArray =
            {
                new Point(0          , 0           ),
                new Point(image.Width, 0           ),
                new Point(0          , image.Height)
            };

            Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);

            Bitmap bitmap = new Bitmap(image.Width, image.Height);

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.DrawImage
                (
                    image,
                    pointArray,
                    rectangle,
                    GraphicsUnit.Pixel,
                    imageAttributes
                );
            }

            return bitmap;
        }

        #endregion
        #region 색상 영역 설정하기 - PlaceColorArea(bitmap, brushBitmap, areaType, spacing, thickness)

        /// <summary>
        /// 색상 영역 설정하기
        /// </summary>
        /// <param name="bitmap">비트맵</param>
        /// <param name="brushBitmap">브러시 비트맵</param>
        /// <param name="areaType">영역 타입</param>
        /// <param name="spacing">간격</param>
        /// <param name="thickness">두께</param>
        /// <returns>비트맵</returns>
        private Bitmap PlaceColorArea(Bitmap bitmap, Bitmap brushBitmap, AreaType areaType, float spacing, float thickness)
        {
            Bitmap targetBitmap = GetMonochromeBitmap(bitmap);

            using(Graphics graphics = Graphics.FromImage(targetBitmap))
            {
                using(Brush brush = new TextureBrush(brushBitmap))
                {
                    using(Pen pen = new Pen(brush, thickness))
                    {
                        int width  = brushBitmap.Width;
                        int height = brushBitmap.Height;

                        switch(areaType)
                        {
                            case AreaType.Stripe :

                                for(float x = 0; x < width; x += spacing)
                                {
                                    graphics.DrawLine(pen, x, 0, x, height);
                                }

                                for(float y = 0; y < height; y += spacing)
                                {
                                    graphics.DrawLine(pen, 0, y, width, y);
                                }

                                break;

                            case AreaType.DiagonalStripe :

                                width   *= 2;
                                height  *= 2;
                                spacing *= (float)Math.Sqrt(2);

                                for(float i = 0; i < width; i += spacing)
                                {
                                    graphics.DrawLine(pen, i, 0, 0, i);
                                }

                                for(float i = -height; i < height; i += spacing)
                                {
                                    graphics.DrawLine(pen, 0, i, height, i + height);
                                }

                                break;

                            case AreaType.Dot :

                                for(float x = -spacing / 2f; x < width; x += spacing)
                                {
                                    for(float y = -spacing / 2f; y < height; y += spacing)
                                    {
                                        graphics.DrawEllipse(pen, x, y, thickness, thickness);
                                    }
                                }

                                break;

                            case AreaType.Text :

                                using(Font font = new Font("Arial", spacing))
                                {
                                    GraphicsUnit unit = GraphicsUnit.Pixel;

                                    graphics.DrawString(brushString, font, brush, brushBitmap.GetBounds(ref unit));
                                }

                                break;
                        }
                    }
                }
            }

            return targetBitmap;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요