첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

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

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

        #region Field

        /// <summary>
        /// 비트맵
        /// </summary>
        private Bitmap bitmap = null;

        /// <summary>
        /// 시작 X
        /// </summary>
        private int startX;
        
        /// <summary>
        /// 시작 Y
        /// </summary>
        private int startY;
        
        /// <summary>
        /// 종료 X
        /// </summary>
        private int endX;
        
        /// <summary>
        /// 종료 Y
        /// </summary>
        private int endY;

        /// <summary>
        /// 영역 선택중 여부
        /// </summary>
        private bool isSelectingArea = false;

        /// <summary>
        /// 선택 비트맵
        /// </summary>
        private Bitmap selectedBitmap = null;

        /// <summary>
        /// 선택 그래픽스
        /// </summary>
        private Graphics selectedGraphics = null;

        #endregion

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

        #region 생성자 - MainForm()

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

            KeyPreview = true;

            #region 이벤트를 설정한다.

            Load                      += Form_Load;
            KeyPress                  += Form_KeyPress;
            this.pictureBox.MouseDown += pictureBox_MouseDown;
            this.pictureBox.MouseMove += pictureBox_MouseMove;
            this.pictureBox.MouseUp   += pictureBox_MouseUp;

            #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)
        {
            this.bitmap = new Bitmap(pictureBox.Image);
        }

        #endregion
        #region 폼 키 PRESS 처리하기 - Form_KeyPress(sender, e)

        /// <summary>
        /// 폼 키 PRESS 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == 27)
            {
                if(!this.isSelectingArea)
                {
                    return;
                }

                this.isSelectingArea = false;

                this.selectedBitmap = null;

                this.selectedGraphics = null;

                this.pictureBox.Image = this.bitmap;

                this.pictureBox.Refresh();
            }
        }

        #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.isSelectingArea = true;

            this.startX = e.X;
            this.startY = e.Y;

            this.selectedBitmap = new Bitmap(this.bitmap);

            this.selectedGraphics = Graphics.FromImage(this.selectedBitmap);

            this.pictureBox.Image = this.selectedBitmap;
        }

        #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.isSelectingArea)
            {
                return;
            }

            this.endX = e.X;
            this.endY = e.Y;

            this.selectedGraphics.DrawImage(this.bitmap, 0, 0);

            using(Pen selectPen = new Pen(Color.Blue, 2f))
            {
                selectPen.DashStyle = DashStyle.Dash;

                Rectangle rectangle = GetRectangle(this.startX, this.startY, this.endX, this.endY);

                this.selectedGraphics.DrawRectangle(selectPen, rectangle);
            }

            this.pictureBox.Refresh();
        }

        #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)
        {
            if(!this.isSelectingArea)
            {
                return;
            }

            this.isSelectingArea = false;

            this.selectedBitmap = null;

            this.selectedGraphics = null;

            this.pictureBox.Image = this.bitmap;

            this.pictureBox.Refresh();

            Rectangle rectangle = GetRectangle(this.startX, this.startY, this.endX, this.endY);

            if((rectangle.Width > 0) && (rectangle.Height > 0))
            {
                MessageBox.Show(rectangle.ToString());
            }
        }

        #endregion

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

        #region 사각형 구하기 - GetRectangle(startX, startY, endX, endY)

        /// <summary>
        /// 사각형 구하기
        /// </summary>
        /// <param name="startX">시작 X</param>
        /// <param name="startY">시작 Y</param>
        /// <param name="endX">종료 X</param>
        /// <param name="endY">종료 Y</param>
        /// <returns>사각형</returns>
        private Rectangle GetRectangle(int startX, int startY, int endX, int endY)
        {
            return new Rectangle
            (
                Math.Min(startX, endX),
                Math.Min(startY, endY),
                Math.Abs(startX - endX),
                Math.Abs(startY - endY)
            );
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요