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

        #endregion

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

        #region 생성자 - MainForm()

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

            this.openMenuItem.Click      += openMenuitem_Click;
            this.saveAsMenuItem.Click    += saveAsMenuItem_Click;
            this.radiusXScrollBar.Scroll += radiusXScrollBar_Scroll;
            this.radiusYScrollBar.Scroll += radiusYScrollBar_Scroll;
        }

        #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.pictureBox.Visible = true;
                this.pictureBox.Image   = this.sourceBitmap;

                this.saveAsMenuItem.Enabled = true;

                ShowBitmap();
            }
        }

        #endregion
        #region 다른 이름으로 저장 메뉴 항목 클릭시 처리하기 - saveAsMenuItem_Click(sender, e)

        /// <summary>
        /// 다른 이름으로 저장 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void saveAsMenuItem_Click(object sender, EventArgs e)
        {
            if(this.saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                SaveImage(this.pictureBox.Image, this.saveFileDialog.FileName);
            }
        }

        #endregion
        #region 반경 X 스크롤바 스크롤시 처리하기 - radiusXScrollBar_Scroll(sender, e)

        /// <summary>
        /// 반경 X 스크롤바 스크롤시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void radiusXScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            this.radiusXValueLabel.Text = this.radiusXScrollBar.Value.ToString();

            ShowBitmap();
        }

        #endregion
        #region 반경 Y 스크롤바 스크롤시 처리하기 - radiusYScrollBar_Scroll(sender, e)

        /// <summary>
        /// 반경 Y 스크롤바 스크롤시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void radiusYScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            this.radiusYValueLabel.Text = this.radiusYScrollBar.Value.ToString();

            ShowBitmap();
        }

        #endregion

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

        #region 둥근 사각형 그래픽 패스 구하기 - GetRoundedRectangleGraphicsPath(rectangle, radiusX, radiusY, roundUpperLeft, roundUpperRight, roundLowerLeft, roundLowerRight)

        /// <summary>
        /// 둥근 사각형 그래픽 패스 구하기
        /// </summary>
        /// <summary>
        /// 둥근 사각형 그래픽 패스 구하기
        /// </summary>
        /// <param name="rectangle">사각형</param>
        /// <param name="radiusX">반경 X</param>
        /// <param name="radiusY">반경 Y</param>
        /// <param name="roundUpperLeft">좌상단 라운드 처리 여부</param>
        /// <param name="roundUpperRight">우상단 라운드 처리 여부</param>
        /// <param name="roundLowerLeft">좌하단 라운드 처리 여부</param>
        /// <param name="roundLowerRight">우하단 라운드 처리 여부</param>
        /// <returns>그래픽스 패스</returns>
        private GraphicsPath GetRoundedRectangleGraphicsPath
        (
            RectangleF rectangle,
            float      radiusX,
            float      radiusY,
            bool       roundUpperLeft,
            bool       roundUpperRight,
            bool       roundLowerLeft,
            bool       roundLowerRight
        )
        {
            PointF       point1;
            PointF       point2;
            GraphicsPath path = new GraphicsPath();

            if(roundUpperLeft)
            {
                RectangleF cornerRectangle = new RectangleF
                (
                    rectangle.X,
                    rectangle.Y,
                    2 * radiusX,
                    2 * radiusY
                );

                path.AddArc(cornerRectangle, 180, 90);

                point1 = new PointF(rectangle.X + radiusX, rectangle.Y);
            }
            else
            {
                point1 = new PointF(rectangle.X, rectangle.Y);
            }

            if(roundUpperRight)
            {
                point2 = new PointF(rectangle.Right - radiusX, rectangle.Y);
            }
            else
            {
                point2 = new PointF(rectangle.Right, rectangle.Y);
            }
            path.AddLine(point1, point2);

            if(roundUpperRight)
            {
                RectangleF cornerRectangle = new RectangleF
                (
                    rectangle.Right - 2 * radiusX,
                    rectangle.Y,
                    2 * radiusX,
                    2 * radiusY
                );

                path.AddArc(cornerRectangle, 270, 90);

                point1 = new PointF(rectangle.Right, rectangle.Y + radiusY);
            }
            else
            {
                point1 = new PointF(rectangle.Right, rectangle.Y);
            }

            if(roundLowerRight)
            {
                point2 = new PointF(rectangle.Right, rectangle.Bottom - radiusY);
            }
            else
            {
                point2 = new PointF(rectangle.Right, rectangle.Bottom);
            }

            path.AddLine(point1, point2);

            if(roundLowerRight)
            {
                RectangleF cornerRectangle = new RectangleF
                (
                    rectangle.Right  - 2 * radiusX,
                    rectangle.Bottom - 2 * radiusY,
                    2 * radiusX,
                    2 * radiusY
                );

                path.AddArc(cornerRectangle, 0, 90);

                point1 = new PointF(rectangle.Right - radiusX, rectangle.Bottom);
            }
            else
            {
                point1 = new PointF(rectangle.Right, rectangle.Bottom);
            }

            if(roundLowerLeft)
            {
                point2 = new PointF(rectangle.X + radiusX, rectangle.Bottom);
            }
            else
            {
                point2 = new PointF(rectangle.X, rectangle.Bottom);
            }

            path.AddLine(point1, point2);

            if(roundLowerLeft)
            {
                RectangleF cornerRectangle = new RectangleF
                (
                    rectangle.X,
                    rectangle.Bottom - 2 * radiusY,
                    2 * radiusX,
                    2 * radiusY
                );

                path.AddArc(cornerRectangle, 90, 90);

                point1 = new PointF(rectangle.X, rectangle.Bottom - radiusY);
            }
            else
            {
                point1 = new PointF(rectangle.X, rectangle.Bottom);
            }

            if(roundUpperLeft)
            {
                point2 = new PointF(rectangle.X, rectangle.Y + radiusY);
            }
            else
            {
                point2 = new PointF(rectangle.X, rectangle.Y);
            }

            path.AddLine(point1, point2);

            path.CloseFigure();

            return path;
        }

        #endregion
        #region 비트맵 표시하기 - ShowBitmap()

        /// <summary>
        /// 비트맵 표시하기
        /// </summary>
        private void ShowBitmap()
        {
            if((this.radiusXScrollBar.Value == 0) || (this.radiusYScrollBar.Value == 0))
            {
                this.pictureBox.Image = this.sourceBitmap;

                return;
            }

            int width  = this.sourceBitmap.Width;
            int height = this.sourceBitmap.Height;

            Bitmap bitmap = new Bitmap(width, height);

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

                graphics.SmoothingMode     = SmoothingMode.AntiAlias;
                graphics.InterpolationMode = InterpolationMode.High;

                GraphicsPath path = GetRoundedRectangleGraphicsPath
                (
                    new Rectangle(0, 0, width, height),
                    this.radiusXScrollBar.Value,
                    this.radiusYScrollBar.Value,
                    true,
                    true,
                    true,
                    true
                );

                using(TextureBrush brush = new TextureBrush(this.sourceBitmap))
                {
                    graphics.FillPath(brush, path);
                }
            }

            this.pictureBox.Image = bitmap;
        }

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

        /// <summary>
        /// 이미지 저장하기
        /// </summary>
        /// <param name="image">이미지</param>
        /// <param name="filePath">파일 경로</param>
        public 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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요