첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

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
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 소스 비트맵
        /// </summary>
        private Bitmap sourceBitmap = null;

        /// <summary>
        /// 타겟 비트맵
        /// </summary>
        private Bitmap targetBitmap = null;

        /// <summary>
        /// 불투명도
        /// </summary>
        private float opacity = 0.75f;

        #endregion

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

        #region 생성자 - MainForm()

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

            this.openMenuItem.Click += openMenuItem_Click;
            this.saveMenuItem.Click += saveMenuItem_Click;
            this.scrollBar.Scroll   += scrollBar_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);

                ShowImage();

                this.pictureBox.Visible = true;

                this.saveMenuItem.Enabled = true;
            }
        }

        #endregion
        #region 저장하기 메뉴 항목 클릭시 처리하기 - saveMenuItem_Click(sender, e)

        /// <summary>
        /// 저장하기 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void saveMenuItem_Click(object sender, EventArgs e)
        {
            if(this.saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.targetBitmap.Save(this.saveFileDialog.FileName, ImageFormat.Png);
            }
        }

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

        /// <summary>
        /// 스크롤바 스크롤시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void scrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            this.opacity = this.scrollBar.Value / 100f;

            this.opacityValueLabel.Text = this.opacity.ToString("0.00");

            ShowImage();
        }

        #endregion

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

        #region 체커 보드 비트맵 구하기 - GetCheckerboardBitmap(width, height, size)

        /// <summary>
        /// 체커 보드 비트맵 구하기
        /// </summary>
        /// <param name="width">너비</param>
        /// <param name="height">높이</param>
        /// <param name="size">크기</param>
        /// <returns>체커 보드 비트맵</returns>
        private Bitmap GetCheckerboardBitmap(int width, int height, int size)
        {
            int rowCount    = height / size + 1;
            int columnCount = width  / size + 1;

            Bitmap bitmap = new Bitmap(width, height);

            using(Graphics graphics = Graphics.FromImage(bitmap))
            {
                for(int row = 0; row < rowCount; row++)
                {
                    for(int column = 0; column < columnCount; column++)
                    {
                        Rectangle rectangle = new Rectangle
                        (
                            column * size,
                            row    * size,
                            size,
                            size
                        );

                        if((row + column) % 2 == 0)
                        {
                            graphics.FillRectangle(Brushes.Blue, rectangle);
                        }
                        else
                        {
                            graphics.FillRectangle(Brushes.Yellow, rectangle);
                        }
                    }
                }
            }

            return bitmap;
        }

        #endregion
        #region 비트맵 구하기 - GetBitmap(sourceBitmap, opacity)

        /// <summary>
        /// 비트맵 구하기
        /// </summary>
        /// <param name="sourceBitmap">소스 비트맵</param>
        /// <param name="opacity">불투명도</param>
        /// <returns>비트맵</returns>
        private Bitmap GetBitmap(Bitmap sourceBitmap, float opacity)
        {
            Bitmap targetBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);

            using(Graphics graphics = Graphics.FromImage(targetBitmap))
            {
                ColorMatrix colorMatrix = new ColorMatrix();

                colorMatrix.Matrix33 = opacity;

                ImageAttributes imageAttributes = new ImageAttributes();

                imageAttributes.SetColorMatrix
                (
                    colorMatrix,
                    ColorMatrixFlag.Default,
                    ColorAdjustType.Bitmap
                );

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

                graphics.DrawImage
                (
                    sourceBitmap,
                    rectangle,
                    0,
                    0,
                    sourceBitmap.Width,
                    sourceBitmap.Height,
                    GraphicsUnit.Pixel,
                    imageAttributes
                );
            }

            return targetBitmap;
        }

        #endregion
        #region 이미지 표시하기 - ShowImage()

        /// <summary>
        /// 이미지 표시하기
        /// </summary>
        private void ShowImage()
        {
            if(this.sourceBitmap == null)
            {
                return;
            }

            Bitmap temporaryBitmap = GetCheckerboardBitmap(sourceBitmap.Width, sourceBitmap.Height, 64);

            this.targetBitmap = GetBitmap(this.sourceBitmap, opacity);

            using(Graphics graphics = Graphics.FromImage(temporaryBitmap))
            {
                graphics.DrawImage(this.targetBitmap, 0, 0);
            }

            this.pictureBox.Image = temporaryBitmap;
        }

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

댓글을 달아 주세요