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

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
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.Image = Image.FromFile("sample.jpg");

            #region 이벤트를 설정한다.

            Load                        += Form_Load;
            this.redScrollBar.Scroll    += channelScrollBar_Scroll;
            this.greenScrollBar.Scroll  += channelScrollBar_Scroll;
            this.blueScrollBar.Scroll   += channelScrollBar_Scroll;
            this.brightScrollBar.Scroll += channelScrollBar_Scroll;

            #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.redScrollBar.Value    = 128;
            this.greenScrollBar.Value  = 128;
            this.blueScrollBar.Value   = 128;
            this.brightScrollBar.Value = 128;

            this.colorPictureBox.BackColor = Color.FromArgb
            (
                this.redScrollBar.Value,
                this.greenScrollBar.Value,
                this.blueScrollBar.Value
            );

            SetTargetImage();
        }

        #endregion
        #region 채널 스크롤바 스크롤시 처리하기 - channelScrollBar_Scroll(sender, e)

        /// <summary>
        /// 채널 스크롤바 스크롤시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void channelScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            this.colorPictureBox.BackColor = Color.FromArgb
            (
                this.redScrollBar.Value,
                this.greenScrollBar.Value,
                this.blueScrollBar.Value
            );

            SetTargetImage();
        }

        #endregion

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

        #region 색상 이미지 구하기 - GetColorImage(sourceImage, color)

        /// <summary>
        /// 색상 이미지 구하기
        /// </summary>
        /// <param name="sourceImage">소스 이미지</param>
        /// <param name="color">색상</param>
        /// <returns>비트맵</returns>
        private Bitmap GetColorImage(Image sourceImage, Color color)
        {
            float scale = this.brightScrollBar.Value / 128f;

            float red   = color.R / 255f * scale;
            float green = color.G / 255f * scale;
            float blue  = color.B / 255f * scale;

            ColorMatrix colorMatrix = new ColorMatrix
            (
                new float[][]
                {
                    new float[] {red, 0    , 0   , 0, 0 },
                    new float[] {0  , green, 0   , 0, 0 },
                    new float[] {0  , 0    , blue, 0, 0 },
                    new float[] {0  , 0    , 0   , 1, 0 },
                    new float[] {0  , 0    , 0   , 0, 1 }
                }
            );

            ImageAttributes imageAttributes = new ImageAttributes();

            imageAttributes.SetColorMatrix(colorMatrix);

            Point[] pointArray =
            {
                new Point(0, 0),
                new Point(sourceImage.Width - 1, 0),
                new Point(0, sourceImage.Height - 1),
            };

            Rectangle rectangle = new Rectangle(0, 0, sourceImage.Width, sourceImage.Height);

            Bitmap bitmap = new Bitmap(sourceImage.Width, sourceImage.Height);

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.DrawImage(sourceImage, pointArray, rectangle, GraphicsUnit.Pixel, imageAttributes);
            }

            return bitmap;
        }

        #endregion
        #region 타겟 이미지 설정하기 - SetTargetImage()

        /// <summary>
        /// 타겟 이미지 설정하기
        /// </summary>
        private void SetTargetImage()
        {
            this.targetPictureBox.Image = GetColorImage(this.sourcePictureBox.Image, this.colorPictureBox.BackColor);
        }

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

댓글을 달아 주세요