첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 비트맵 확대하기

------------------------------------------------------------------------------------------------------------------------


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 const int WORLD_WIDTH = 100;

 

        /// <summary>

        /// 월드 높이

        /// </summary>

        private const int WORLD_HEIGHT = 100;

 

        /// <summary>

        /// 비트맵

        /// </summary>

        private Bitmap bitmap = null;

 

        /// <summary>

        /// 비트맵 스케일

        /// </summary>

        private float bitmapScale = 1.0f;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.Load                               += Form_Load;

            this.scaleComboBox.SelectedIndexChanged += scaleComboBox_SelectedIndexChanged;

 

            #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.scaleComboBox.SelectedIndex = 2;

        }

 

        #endregion

        #region 스케일 콤보 박스 선택 인덱스 변경시 처리하기 - scaleComboBox_SelectedIndexChanged(sender, e)

 

        /// <summary>

        /// 스케일 콤보 박스 선택 인덱스 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void scaleComboBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            switch(this.scaleComboBox.SelectedIndex)

            {

                case 0 : SetScale(0.25f); break;

                case 1 : SetScale(0.5f ); break;

                case 2 : SetScale(1f   ); break;

                case 3 : SetScale(2f   ); break;

                case 4 : SetScale(4f   ); break;

                case 5 : SetScale(8f   ); break;

            }

        }

 

        #endregion

 

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

 

        #region 스케일 설정하기 - SetScale(bitmapScale)

 

        /// <summary>

        /// 스케일 설정하기

        /// </summary>

        /// <param name="bitmapScale">비트맵 스케일</param>

        private void SetScale(float bitmapScale)

        {

            this.bitmapScale = bitmapScale;

 

            this.bitmap = new Bitmap

            (

                (int)(this.bitmapScale * WORLD_WIDTH ),

                (int)(this.bitmapScale * WORLD_HEIGHT)

            );

 

            using(Graphics graphics = Graphics.FromImage(this.bitmap))

            {

                graphics.Clear(Color.White);

 

                graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

                graphics.ScaleTransform(this.bitmapScale, this.bitmapScale);

 

                DrawImage(graphics);

            }

 

            this.canvasPictureBox.Image = this.bitmap;

        }

 

        #endregion

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

 

        /// <summary>

        /// 이미지 그리기

        /// </summary>

        /// <param name="graphics">그래픽스</param>

        private void DrawImage(Graphics graphics)

        {

            Rectangle rectangle;

 

            rectangle = new Rectangle(10, 10, 80, 80);

 

            graphics.FillEllipse(Brushes.LightGreen, rectangle);

 

            graphics.DrawEllipse(Pens.Green, rectangle);

 

            rectangle = new Rectangle(40, 40, 20, 30);

 

            graphics.FillEllipse(Brushes.LightBlue, rectangle);

 

            graphics.DrawEllipse(Pens.Blue, rectangle);

 

            rectangle = new Rectangle(25, 30, 50, 50);

 

            graphics.DrawArc(Pens.Red, rectangle, 20, 140);

 

            rectangle = new Rectangle(25, 25, 15, 20);

 

            graphics.FillEllipse(Brushes.White, rectangle);

 

            graphics.DrawEllipse(Pens.Black, rectangle);

 

            rectangle = new Rectangle(30, 30, 10, 10);

 

            graphics.FillEllipse(Brushes.Black, rectangle);

 

            rectangle = new Rectangle(60, 25, 15, 20);

 

            graphics.FillEllipse(Brushes.White, rectangle);

 

            graphics.DrawEllipse(Pens.Black, rectangle);

 

            rectangle = new Rectangle(65, 30, 10, 10);

 

            graphics.FillEllipse(Brushes.Black, rectangle);

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요