■ Bitmap 클래스 : 투명 배경 비트맵 구하기

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


TestProject.zip


MainForm.cs

 

 

using System.Drawing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.pictureBox.Image = GetTransparentBitmap(this.pictureBox.Width - 10, this.pictureBox.Height - 10);

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 투명 비트맵 구하기 - GetTransparentBitmap(width, height)

 

        /// <summary>

        /// 투명 비트맵 구하기

        /// </summary>

        /// <param name="width">너비</param>

        /// <param name="height">높이</param>

        /// <returns></returns>

        private Bitmap GetTransparentBitmap(int width, int height)

        {

           Bitmap bitmap = new Bitmap(width, height);

 

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

           {

               graphics.Clear(Color.Transparent);

 

               using(Pen pen = new Pen(Color.RoyalBlue, 20))

               {

                    int ellipseWidth  = width  - (int)pen.Width;

                    int ellipseHeight = height - (int)pen.Width;

 

                    ellipseWidth  = ellipseWidth  < 0 ? 1 : ellipseWidth;

                    ellipseHeight = ellipseHeight < 0 ? 1 : ellipseHeight;

 

                    int x = (width  - ellipseWidth ) / 2;

                    int y = (height - ellipseHeight) / 2;

 

                    graphics.DrawEllipse(pen, x, y, ellipseWidth, ellipseHeight);

               }

            }

 

            return bitmap;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요