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

TestProject.zip
다운로드

▶ MainForm.cs

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

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

        #region 생성자 - MainForm()

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

            #region 이벤트를 설정한다.

            Load += Form_Load;

            #endregion
        }

        #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)
        {
            Metafile metaFile = GetMetafile(100, 100, "test.emf");

            DrawSampleImage(metaFile);

            Bitmap bitmap = GetBitmap(metaFile);

            this.canvasPictureBox1.Image = bitmap;
            this.canvasPictureBox2.Image = bitmap;
            this.canvasPictureBox3.Image = metaFile;
        }

        #endregion

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

        #region 메타 파일 구하기 - GetMetafile(width, height, filePath)

        /// <summary>
        /// 메타 파일 구하기
        /// </summary>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="filePath">파일 경로</param>
        /// <returns>메타 파일</returns>
        private Metafile GetMetafile(float width, float height, string filePath)
        {
            using(Bitmap bitmap = new Bitmap(16, 16))
            {
                using(Graphics graphics = Graphics.FromImage(bitmap))
                {
                    RectangleF rectangle = new RectangleF(0, 0, width, height);

                    Metafile metaFile;

                    if((filePath != null) && (filePath.Length > 0))
                    {
                        metaFile = new Metafile(filePath, graphics.GetHdc(), rectangle, MetafileFrameUnit.Pixel);
                    }
                    else
                    {
                        metaFile = new Metafile(graphics.GetHdc(), rectangle, MetafileFrameUnit.Pixel);
                    }

                    graphics.ReleaseHdc();

                    return metaFile;
                }
            }
        }

        #endregion
        #region 샘플 이미지 그리기 - DrawSampleImage(targetMetaFile)

        /// <summary>
        /// 샘플 이미지 그리기
        /// </summary>
        /// <param name="targetMetaFile">타겟 메타 파일</param>
        private void DrawSampleImage(Metafile targetMetaFile)
        {
            using(Graphics graphics = Graphics.FromImage(targetMetaFile))
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;

                using(Pen pen = new Pen(Color.Red, 5))
                {
                    graphics.DrawEllipse(pen, 5, 5, 90, 90);
                }

                using(Brush brush = new SolidBrush(Color.FromArgb(255, 128, 255, 128)))
                {
                    graphics.FillEllipse(brush, 5, 25, 90, 50);
                }

                using(Brush brush = new SolidBrush(Color.FromArgb(128, 128, 128, 255)))
                {
                    graphics.FillEllipse(brush, 25, 5, 50, 90);
                }

                Point[] pointArray =
                {
                    new Point(50, 5 ),
                    new Point(94, 50),
                    new Point(50, 94),
                    new Point(5 , 50),
                };

                graphics.DrawPolygon(Pens.Blue, pointArray);
            }
        }

        #endregion
        #region 비트맵 구하기 - GetBitmap(sourceMetaFile)

        /// <summary>
        /// 비트맵 구하기
        /// </summary>
        /// <param name="sourceMetaFile">소스 메타 파일</param>
        /// <returns>비트맵</returns>
        private Bitmap GetBitmap(Metafile sourceMetaFile)
        {
            Bitmap bitmap = new Bitmap(sourceMetaFile.Width, sourceMetaFile.Height);

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                GraphicsUnit graphicsUnit = GraphicsUnit.Pixel;
                RectangleF   rectangle    = sourceMetaFile.GetBounds(ref graphicsUnit);

                PointF[] targetPointArray =
                {
                    new PointF(0              , 0               ),
                    new PointF(rectangle.Width, 0               ),
                    new PointF(0              , rectangle.Height),
                };

                graphics.DrawImage(sourceMetaFile, targetPointArray, rectangle, GraphicsUnit.Pixel);
            }

            return bitmap;
        }

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

댓글을 달아 주세요