첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.02MB

▶ MainForm.cs

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

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

        #region 생성자 - MainForm()

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

            this.sourcePictureBox.SizeMode = PictureBoxSizeMode.Zoom;
            this.sourcePictureBox.Image    = Properties.Resources.sample;

            this.targetPictureBox.SizeMode = PictureBoxSizeMode.Zoom;

            this.sourcePictureBox.MouseDown += sourcePictureBox_MouseDown;
        }

        #endregion

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

        #region 소스 픽처 박스 마우스 DOWN 처리하기 - sourcePictureBox_MouseDown(sender, e)

        /// <summary>
        /// 소스 픽처 박스 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void sourcePictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            Point screenPoint = PointToScreen(e.Location);

            Color color = GetMousePointColor(screenPoint);

            Bitmap targetBitmap = GetTransparentBitmap(this.sourcePictureBox.Image as Bitmap, color);

            this.targetPictureBox.Image = targetBitmap;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Event

        #region 마우스 위치 색상 구하기 - GetMousePointColor(mousePoint)

        /// <summary>
        /// 마우스 위치 색상 구하기
        /// </summary>
        /// <param name="mousePoint">마우스 위치</param>
        /// <returns>마우스 위치 색상</returns>
        private Color GetMousePointColor(Point mousePoint)
        {
            Size     size     = new Size(1, 1);
            Bitmap   bitmap   = new Bitmap(1, 1);
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.CopyFromScreen(mousePoint.X, mousePoint.Y, 0, 0, size);

            return bitmap.GetPixel(0, 0);
        }

        #endregion
        #region 투명 비트맵 구하기 - GetTransparentBitmap(sourceBitmap, transparentColor)

        /// <summary>
        /// 투명 비트맵 구하기
        /// </summary>
        /// <param name="sourceBitmap">소스 비트맵</param>
        /// <param name="transparentColor">투명 색상</param>
        /// <returns>투명 비트맵</returns>
        private Bitmap GetTransparentBitmap(Bitmap sourceBitmap, Color transparentColor)
        {
            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.SetColorKey(transparentColor, transparentColor);

            int sourceWidth  = sourceBitmap.Width;
            int sourceHeight = sourceBitmap.Height;

            Bitmap targetBitmap = new Bitmap(sourceWidth, sourceHeight);

            Graphics targetGraphics = Graphics.FromImage(targetBitmap);

            targetGraphics.Clear(Color.Transparent);

            targetGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            targetGraphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;

            Rectangle targetRectangle = new Rectangle(0, 0, sourceWidth, sourceHeight);

            targetGraphics.DrawImage
            (
                sourceBitmap,
                targetRectangle,
                0,
                0,
                sourceWidth,
                sourceHeight,
                GraphicsUnit.Pixel,
                imageAttributes
            );

            return targetBitmap;
        }

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

댓글을 달아 주세요