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

TestProject.zip
다운로드

▶ MainForm.cs

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

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

        #region Field

        /// <summary>
        /// 원본 비트맵
        /// </summary>
        private Bitmap originalBitmap = null;

        /// <summary>
        /// 그리기 여부
        /// </summary>
        private bool isDrawing = false;

        /// <summary>
        /// 시작 포인트
        /// </summary>
        private Point startPoint;
        
        /// <summary>
        /// 종료 포인트
        /// </summary>
        private Point endPoint;

        #endregion

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

        #region 생성자 - MainForm()

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

            this.openMenuItem.Click   += openMenuItem_Click;
            this.saveAsMenuItem.Click += saveAsMenuItem_Click;
            this.exitMenuItem.Click   += exitMenuItem_Click;
            this.pictureBox.MouseDown += pictureBox_MouseDown;
            this.pictureBox.MouseMove += pictureBox_MouseMove;
            this.pictureBox.MouseUp   += pictureBox_MouseUp;
        }

        #endregion

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

        #region Open 메뉴 항목 클릭시 처리하기 - openMenuItem_Click(sender, e)

        /// <summary>
        /// Open 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void openMenuItem_Click(object sender, EventArgs e)
        {
            if(this.openFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.originalBitmap = new Bitmap(this.openFileDialog.FileName);

                this.pictureBox.Image = new Bitmap(this.openFileDialog.FileName);

                this.saveAsMenuItem.Enabled = true;

                this.pictureBox.Visible = true;
            }
        }

        #endregion
        #region Save 메뉴 항목 클릭시 처리하기 - saveAsMenuItem_Click(sender, e)

        /// <summary>
        /// Save 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void saveAsMenuItem_Click(object sender, EventArgs e)
        {
            if(this.saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                using(Bitmap bitmap = CutOutOval(this.startPoint, this.endPoint))
                {
                    SaveImage(bitmap, this.saveFileDialog.FileName);

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

                    using(Graphics graphics = Graphics.FromImage(sampleBitmap))
                    {
                        for(int y = 0; y < bitmap.Height; y += 16)
                        {
                            int temporaryY = y / 16;

                            for(int x = 0; x < bitmap.Width; x += 16)
                            {
                                int temporaryX = x / 16;

                                if((temporaryX + temporaryY) % 2 == 0)
                                {
                                    graphics.FillRectangle(Brushes.Red, x, y, 16, 16);
                                }
                                else
                                {
                                    graphics.FillRectangle(Brushes.Black, x, y, 16, 16);
                                }
                            }
                        }

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

                        using(TextureBrush brush = new TextureBrush(bitmap, rectangle))
                        {
                            graphics.FillRectangle(brush, rectangle);
                        }

                        this.pictureBox.Image = sampleBitmap;
                    }
                }
            }
        }

        #endregion
        #region Exit 메뉴 항목 클릭시 처리하기 - exitMenuItem_Click(sender, e)

        /// <summary>
        /// Exit 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
        #region 픽처 박스 마우스 DOWN 처리하기 - pictureBox_MouseDown(sender, e)

        /// <summary>
        /// 픽처 박스 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            this.isDrawing = true;

            this.startPoint = e.Location;
            this.endPoint   = e.Location;
        }

        #endregion
        #region 픽처 박스 마우스 이동시 처리하기 - pictureBox_MouseMove(sender, e)

        /// <summary>
        /// 픽처 박스 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if(!this.isDrawing)
            {
                return;
            }

            this.endPoint = e.Location;

            DrawImage();
        }

        #endregion
        #region 픽처 박스 마우스 UP 처리하기 - pictureBox_MouseUp(sender, e)

        /// <summary>
        /// 픽처 박스 마우스 UP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            this.isDrawing = false;
        }

        #endregion

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

        #region 원 잘라내기 - CutOutOval(startPoint, endPoint)

        /// <summary>
        /// 원 잘라내기
        /// </summary>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        /// <returns>비트맵</returns>
        private Bitmap CutOutOval(Point startPoint, Point endPoint)
        {
            Rectangle fullRectangle = new Rectangle
            (
                0,
                0,
                this.originalBitmap.Width,
                this.originalBitmap.Height
            );

            Region region = new Region(fullRectangle);

            Rectangle ellipseRectangle = GetRectangle(startPoint, endPoint);

            GraphicsPath path = new GraphicsPath();

            path.AddEllipse(ellipseRectangle);

            region.Exclude(path);

            Bitmap bitmap = new Bitmap(originalBitmap);

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(Color.Transparent);

                graphics.SetClip(region, CombineMode.Replace);

                graphics.SmoothingMode = SmoothingMode.AntiAlias;

                using(TextureBrush brush = new TextureBrush(originalBitmap, fullRectangle))
                {
                    graphics.FillRectangle(brush, fullRectangle);
                }
            }

            return bitmap;
        }

        #endregion
        #region 이미지 저장하기 - SaveImage(image, filePath)

        /// <summary>
        /// 이미지 저장하기
        /// </summary>
        /// <param name="image">이미지</param>
        /// <param name="filePath">파일 경로</param>
        private void SaveImage(Image image, string filePath)
        {
            string extension = Path.GetExtension(filePath);

            switch(extension.ToLower())
            {
                case ".bmp"  : image.Save(filePath, ImageFormat.Bmp ); break;
                case ".exif" : image.Save(filePath, ImageFormat.Exif); break;
                case ".gif"  : image.Save(filePath, ImageFormat.Gif ); break;
                case ".jpg"  :
                case ".jpeg" : image.Save(filePath, ImageFormat.Jpeg); break;
                case ".png"  : image.Save(filePath, ImageFormat.Png ); break;
                case ".tif"  :
                case ".tiff" : image.Save(filePath, ImageFormat.Tiff); break;
                default      : throw new NotSupportedException("Unknown file extension " + extension);
            }
        }

        #endregion
        #region 사각형 구하기 - GetRectangle(startPoint, endPoint)

        /// <summary>
        /// 사각형 구하기
        /// </summary>
        /// <param name="startPoint">시작 포인트</param>
        /// <param name="endPoint">종료 포인트</param>
        /// <returns>사각형</returns>
        private Rectangle GetRectangle(Point startPoint, Point endPoint)
        {
            int x      = Math.Min(startPoint.X, endPoint.X);
            int y      = Math.Min(startPoint.Y, endPoint.Y);
            int width  = Math.Abs(endPoint.X - startPoint.X);
            int height = Math.Abs(endPoint.Y - startPoint.Y);

            return new Rectangle(x, y, width, height);
        }

        #endregion
        #region 이미지 그리기 - DrawImage()

        /// <summary>
        /// 이미지 그리기
        /// </summary>
        private void DrawImage()
        {
            Bitmap bitmap = new Bitmap(this.originalBitmap);

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;

                using(Pen pen = new Pen(Color.Magenta, 3))
                {
                    graphics.DrawEllipse(pen, GetRectangle(this.startPoint, this.endPoint));
                }
            }

            this.pictureBox.Image = bitmap;
        }

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

댓글을 달아 주세요