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

TestProject.zip
0.01MB

▶ ColorSwapType.cs

namespace TestProject
{
    /// <summary>
    /// 색상 교환 타입
    /// </summary>
    public enum ColorSwapType
    {
        /// <summary>
        /// ShiftRight
        /// </summary>
        ShiftRight,

        /// <summary>
        /// ShiftLeft
        /// </summary>
        ShiftLeft,

        /// <summary>
        /// SwapBlueAndRed
        /// </summary>
        SwapBlueAndRed,

        /// <summary>
        /// SwapBlueAndGreen
        /// </summary>
        SwapBlueAndGreen,

        /// <summary>
        /// SwapRedAndGreen
        /// </summary>
        SwapRedAndGreen
    }
}

 

 

728x90

 

▶ ColorSwapFilter.cs

namespace TestProject
{
    /// <summary>
    /// 색상 교환 필터
    /// </summary>
    public class ColorSwapFilter
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 색상 교환 타입
        /// </summary>
        private ColorSwapType swapType = ColorSwapType.ShiftRight;

        /// <summary>
        /// 중간 색상 값 교환 여부
        /// </summary>
        private bool swapHalfColorValue = false;

        /// <summary>
        /// 교환시 색상 반전 여부
        /// </summary>
        private bool invertColorWhenSwapping = false;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 색상 교환 타입 - SwapType

        /// <summary>
        /// 색상 교환 타입
        /// </summary>
        public ColorSwapType SwapType
        {
            get
            {
                return this.swapType;
            }
            set
            {
                this.swapType = value;
            }
        }

        #endregion
        #region 중간 색상 값 교환 여부 - SwapHalfColorValue

        /// <summary>
        /// 중간 색상 값 교환 여부
        /// </summary>
        public bool SwapHalfColorValue
        {
            get
            {
                return this.swapHalfColorValue;
            }
            set
            {
                this.swapHalfColorValue = value;
            }
        }

        #endregion
        #region 교환시 색상 반전 여부 - InvertColorWhenSwapping

        /// <summary>
        /// 교환시 색상 반전 여부
        /// </summary>
        public bool InvertColorWhenSwapping
        {
            get
            {
                return this.invertColorWhenSwapping;
            }
            set
            {
                this.invertColorWhenSwapping = value;
            }
        }

        #endregion
    }
}

 

300x250

 

▶ BitmapHelper.cs

using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 비트맵 헬퍼
    /// </summary>
    public static class BitmapHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 색상 교환하기 - SwapColor(sourceBitmap, colorSwapFilter)

        /// <summary>
        /// 색상 교환하기
        /// </summary>
        /// <param name="sourceBitmap">소스 비트맵</param>
        /// <param name="colorSwapFilter">색상 교환 필터</param>
        /// <returns>비트맵</returns>
        public static Bitmap SwapColor(Bitmap sourceBitmap, ColorSwapFilter colorSwapFilter)
        {
            BitmapData sourceBitmapData = sourceBitmap.LockBits
            (
                new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height),
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppArgb
            );

            byte[] targetByteArray = new byte[sourceBitmapData.Stride * sourceBitmapData.Height];

            Marshal.Copy(sourceBitmapData.Scan0, targetByteArray, 0, targetByteArray.Length);

            sourceBitmap.UnlockBits(sourceBitmapData);

            byte sourceBlue   = 0;
            byte sourceGreen  = 0;
            byte sourceRed    = 0;
            byte targetBlue   = 0;
            byte targetGreen  = 0;
            byte targetRed    = 0;
            byte byte2        = 2;
            byte maximumValue = 255;

            for(int k = 0; k < targetByteArray.Length; k += 4)
            {
                sourceBlue  = targetByteArray[k    ];
                sourceGreen = targetByteArray[k + 1];
                sourceRed   = targetByteArray[k + 2];

                if(colorSwapFilter.InvertColorWhenSwapping)
                {
                    sourceBlue  = (byte)(maximumValue - sourceBlue );
                    sourceGreen = (byte)(maximumValue - sourceGreen);
                    sourceRed   = (byte)(maximumValue - sourceRed  );
                }

                if(colorSwapFilter.SwapHalfColorValue)
                {
                    sourceBlue  = (byte)(sourceBlue  / byte2);
                    sourceGreen = (byte)(sourceGreen / byte2);
                    sourceRed   = (byte)(sourceRed   / byte2);
                }

                switch(colorSwapFilter.SwapType)
                {
                    case ColorSwapType.ShiftRight :
                    {
                        targetBlue  = sourceGreen;
                        targetRed   = sourceBlue;
                        targetGreen = sourceRed;
                            
                        break;
                    }
                    case ColorSwapType.ShiftLeft :
                    {
                        targetBlue  = sourceRed;
                        targetRed   = sourceGreen;
                        targetGreen = sourceBlue;
                            
                        break;
                    }
                    case ColorSwapType.SwapBlueAndRed:
                    {
                        targetBlue = sourceRed;
                        targetRed  = sourceBlue;

                        break;
                    }
                    case ColorSwapType.SwapBlueAndGreen :
                    {
                        targetBlue  = sourceGreen;
                        targetGreen = sourceBlue;

                        break;
                    }
                    case ColorSwapType.SwapRedAndGreen :
                    {
                        targetRed   = sourceGreen;
                        targetGreen = sourceGreen;

                        break;
                    }
                }

                targetByteArray[k    ] = targetBlue;
                targetByteArray[k + 1] = targetGreen;
                targetByteArray[k + 2] = targetRed;
            }

            Bitmap targetBitmap = new Bitmap
            (
                sourceBitmap.Width,
                sourceBitmap.Height, 
                PixelFormat.Format32bppArgb
            );

            BitmapData targetBitmapData = targetBitmap.LockBits
            (
                new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height),
                ImageLockMode.WriteOnly,
                PixelFormat.Format32bppArgb
            );

            Marshal.Copy(targetByteArray, 0, targetBitmapData.Scan0, targetByteArray.Length);

            targetBitmap.UnlockBits(targetBitmapData);

            return targetBitmap;
        }

        #endregion
    }
}

 

▶ MainForm.cs

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

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

        #region 생성자 - MainForm()

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

            this.colorFilterTypeComboBox.Items.Add(ColorSwapType.ShiftLeft       );
            this.colorFilterTypeComboBox.Items.Add(ColorSwapType.ShiftRight      );
            this.colorFilterTypeComboBox.Items.Add(ColorSwapType.SwapBlueAndGreen);
            this.colorFilterTypeComboBox.Items.Add(ColorSwapType.SwapBlueAndRed  );
            this.colorFilterTypeComboBox.Items.Add(ColorSwapType.SwapRedAndGreen );

            this.colorFilterTypeComboBox.SelectedIndex = 0;

            this.colorFilterTypeComboBox.SelectedIndexChanged += colorFilterTypeComboBox_SelectedIndexChanged;
            this.invertColorCheckBox.CheckedChanged           += invertColorCheckBox_CheckedChanged;
            this.swapHalfColorCheckBox.CheckedChanged         += swapHalfColorCheckBox_CheckedChanged;
            this.loadImageButton.Click                        += loadImageButton_Click;
            this.saveImageButton.Click                        += saveImageButton_Click;
        }

        #endregion

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

        #region 색상 필터 타입 콤보 박스 선택 인덱스 변경시 처리하기 - colorFilterTypeComboBox_SelectedIndexChanged(sender, e)

        /// <summary>
        /// 색상 필터 타입 콤보 박스 선택 인덱스 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void colorFilterTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }

        #endregion
        #region 색상 반전 체크 박스 체크 변경시 처리하기 - invertColorCheckBox_CheckedChanged(sender, e)

        /// <summary>
        /// 색상 반전 체크 박스 체크 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void invertColorCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }

        #endregion
        #region 중간 색상 교환 체크 박스 체크 변경시 처리하기 - swapHalfColorCheckBox_CheckedChanged(sender, e)

        /// <summary>
        /// 중간 색상 교환 체크 박스 체크 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void swapHalfColorCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }

        #endregion
        #region 이미지 로드 버튼 클릭시 처리하기 - loadImageButton_Click(sender, e)

        /// <summary>
        /// 이미지 로드 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void loadImageButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Title  = "이미지 파일 선택";
            openFileDialog.Filter = "PNG 파일 (*.png)|*.png|비트맵 파일 (*.bmp)|*.bmp|JPEG 파일 (*.jpg)|*.jpg";

            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                StreamReader streamReader = new StreamReader(openFileDialog.FileName);

                Bitmap sourceBitmap = (Bitmap)Bitmap.FromStream(streamReader.BaseStream);

                streamReader.Close();

                this.sourcePictureBox.Image = sourceBitmap;

                ApplyFilter();
            }
        }

        #endregion
        #region 이미지 저장 버튼 클릭시 처리하기 - saveImageButton_Click(sender, e)

        /// <summary>
        /// 이미지 저장 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void saveImageButton_Click(object sender, EventArgs e)
        {
            if(this.targetPictureBox.Image != null)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();

                saveFileDialog.Title  = "이미지 파일 선택";
                saveFileDialog.Filter = "PNG 파일 (*.png)|*.png|비트맵 파일 (*.bmp)|*.bmp|JPEG 파일 (*.jpg)|*.jpg";

                if(saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string fileExtension = Path.GetExtension(saveFileDialog.FileName).ToUpper();

                    ImageFormat imageFormat = ImageFormat.Png;

                    if(fileExtension == "BMP")
                    {
                        imageFormat = ImageFormat.Bmp;
                    }
                    else if(fileExtension == "JPG")
                    {
                        imageFormat = ImageFormat.Jpeg;
                    }

                    StreamWriter streamWriter = new StreamWriter(saveFileDialog.FileName, false);

                    this.targetPictureBox.Image.Save(streamWriter.BaseStream, imageFormat);

                    streamWriter.Flush();

                    streamWriter.Close();
                }
            }
        }

        #endregion

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

        #region 필터 적용하기 - ApplyFilter()

        /// <summary>
        /// 필터 적용하기
        /// </summary>
        private void ApplyFilter()
        {
            if(this.sourcePictureBox.Image != null)
            {
                ColorSwapFilter colorSwapFilter = new ColorSwapFilter();

                colorSwapFilter.SwapType                = (ColorSwapType)this.colorFilterTypeComboBox.SelectedItem;
                colorSwapFilter.InvertColorWhenSwapping = this.invertColorCheckBox.Checked;
                colorSwapFilter.SwapHalfColorValue      = this.swapHalfColorCheckBox.Checked;

                Bitmap sourceBitmap = this.sourcePictureBox.Image as Bitmap;

                this.targetPictureBox.Image = BitmapHelper.SwapColor(sourceBitmap, colorSwapFilter);
            }
        }

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

댓글을 달아 주세요