첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
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 sourceBitmap = null;

        /// <summary>
        /// 타겟 사각형
        /// </summary>
        private Rectangle targetRectangle = new Rectangle(0, 0, 200, 250);

        /// <summary>
        /// 드래그 여부
        /// </summary>
        private bool isDragging = false;

        /// <summary>
        /// 마우스 포인트
        /// </summary>
        private Point mousePoint;

        #endregion

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

        #region 생성자 - MainForm()

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

            #region 이벤트를 설정한다.

            this.openMenuItem.Click         += openMenuItem_Click;
            this.saveMenuItem.Click         += saveMenuItem_Click;
            this.exitMenuItem.Click         += exitMenuItem_Click;
            this.widthTextBox.TextChanged   += sizeTextBox_TextChanged;
            this.heightTextBox.TextChanged  += sizeTextBox_TextChanged;
            this.canvasPictureBox.MouseDown += canvasPictureBox_MouseDown;
            this.canvasPictureBox.MouseMove += canvasPictureBox_MouseMove;
            this.canvasPictureBox.MouseUp   += canvasPictureBox_MouseUp;

            #endregion
        }

        #endregion

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

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

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

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

                this.saveMenuItem.Enabled = true;

                this.canvasPictureBox.Visible = true;

                DrawImage();
            }
        }

        #endregion
        #region 파일 저장하기 메뉴 항목 클릭시 처리하기 - saveMenuItem_Click(sender, e)

        /// <summary>
        /// 파일 저장하기 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void saveMenuItem_Click(object sender, EventArgs e)
        {
            if(this.saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                using(Bitmap bitmap = GetOvalBitmap(this.targetRectangle))
                {
                    SaveImage(bitmap, this.saveFileDialog.FileName);
                }
            }
        }

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

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

        #endregion
        #region 크기 텍스트 박스 텍스트 변경시 처리하기 - sizeTextBox_TextChanged(sender, e)

        /// <summary>
        /// 크기 텍스트 박스 텍스트 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void sizeTextBox_TextChanged(object sender, EventArgs e)
        {
            int width;
            int height;

            if(int.TryParse(widthTextBox.Text, out width))
            {
                this.targetRectangle.Width = width;
            }

            if(int.TryParse(heightTextBox.Text, out height))
            {
                this.targetRectangle.Height = height;
            }

            DrawImage();
        }

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

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

            this.mousePoint = e.Location;
        }

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

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

            this.targetRectangle.X += e.Location.X - this.mousePoint.X;
            this.targetRectangle.Y += e.Location.Y - this.mousePoint.Y;

            this.mousePoint = e.Location;

            DrawImage();
        }

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

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

        #endregion

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

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

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

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

                graphics.DrawEllipse(Pens.Magenta, this.targetRectangle);
            }

            this.canvasPictureBox.Image = bitmap;
        }

        #endregion
        #region 원 비트맵 구하기 - GetOvalBitmap(rectangle)

        /// <summary>
        /// 원 비트맵 구하기
        /// </summary>
        /// <param name="rectangle">사각형</param>
        /// <returns>원 비트맵</returns>
        private Bitmap GetOvalBitmap(Rectangle rectangle)
        {
            Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height);

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

                graphics.Clear(Color.Transparent);

                using(TextureBrush brush = new TextureBrush(this.sourceBitmap, rectangle))
                {
                    rectangle.X = 0;
                    rectangle.Y = 0;

                    graphics.FillEllipse(brush, rectangle);
                }
            }

            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 fileExtension = Path.GetExtension(filePath);

            switch(fileExtension.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(@"알 수 없는 파일 확장자 입니다 : {fileExtension}");
            }
        }

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

댓글을 달아 주세요