첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ Matrix.cs

namespace TestProject
{
    /// <summary>
    /// 매트릭스
    /// </summary>
    public static class Matrix
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region MEAN 3X3 - Mean3X3

        /// <summary>
        /// MEAN 3X3
        /// </summary>
        public static double[,] Mean3X3
        {
            get
            {
                return new double[,]
                {
                    { 1, 1, 1, }, 
                    { 1, 1, 1, }, 
                    { 1, 1, 1, }
                };
            }
        }

        #endregion
        #region MEAN 5X5 - Mean5X5

        /// <summary>
        /// MEAN 5X5
        /// </summary>
        public static double[,] Mean5X5
        {
            get
            {
                return new double[,]
                {
                    { 1, 1, 1, 1, 1 }, 
                    { 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1 }
                };
            }
        }

        #endregion
        #region MEAN 7X7 - Mean7X7

        /// <summary>
        /// MEAN 7X7
        /// </summary>
        public static double[,] Mean7X7
        {
            get
            {
                return new double[,]
                {
                    { 1, 1, 1, 1, 1, 1, 1 }, 
                    { 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1 },
                };
            }
        }

        #endregion
        #region MEAN 9X9 - Mean9X9

        /// <summary>
        /// MEAN 9X9
        /// </summary>
        public static double[,] Mean9X9
        {
            get
            {
                return new double[,]
                {
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1 }
                };
            }
        }

        #endregion
        #region GAUSSIAN BLUR 3X3 - GaussianBlur3X3

        /// <summary>
        /// GAUSSIAN BLUR 3X3
        /// </summary>
        public static double[,] GaussianBlur3X3
        {
            get
            {
                return new double[,]
                {
                    { 1, 2, 1, }, 
                    { 2, 4, 2, }, 
                    { 1, 2, 1, }
                };
            }
        }

        #endregion
        #region GAUSSIAN BLUR 5X5 - GaussianBlur5X5

        /// <summary>
        /// GAUSSIAN BLUR 5X5
        /// </summary>
        public static double[,] GaussianBlur5X5
        {
            get
            {
                return new double[,]
                {
                    { 2, 04, 05, 04, 2 }, 
                    { 4, 09, 12, 09, 4 }, 
                    { 5, 12, 15, 12, 5 },
                    { 4, 09, 12, 09, 4 },
                    { 2, 04, 05, 04, 2 }
                };
            }
        }

        #endregion
        #region MOTION BLUR 5X5 - MotionBlur5X5

        /// <summary>
        /// MOTION BLUR 5X5
        /// </summary>
        public static double[,] MotionBlur5X5
        {
            get
            {
                return new double[,]
                {
                    { 1, 0, 0, 0, 1 }, 
                    { 0, 1, 0, 1, 0 }, 
                    { 0, 0, 1, 0, 0 },
                    { 0, 1, 0, 1, 0 },
                    { 1, 0, 0, 0, 1 }
                };
            }
        }

        #endregion
        #region MOTION BLUR 5X5 AT 45 DEGREES - MotionBlur5X5At45Degrees

        /// <summary>
        /// MOTION BLUR 5X5 AT 45 DEGREES
        /// </summary>
        public static double[,] MotionBlur5X5At45Degrees
        {
            get
            {
                return new double[,]
                {
                    { 0, 0, 0, 0, 1 }, 
                    { 0, 0, 0, 1, 0 }, 
                    { 0, 0, 1, 0, 0 },
                    { 0, 1, 0, 0, 0 },
                    { 1, 0, 0, 0, 0 }
                };
            }
        }

        #endregion
        #region MOTION BLUR 5X5 AT 135 DEGREES - MotionBlur5X5At135Degrees

        /// <summary>
        /// MOTION BLUR 5X5 AT 135 DEGREES
        /// </summary>
        public static double[,] MotionBlur5X5At135Degrees
        {
            get
            {
                return new double[,]
                {
                    { 1, 0, 0, 0, 0 }, 
                    { 0, 1, 0, 0, 0 }, 
                    { 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 1, 0 },
                    { 0, 0, 0, 0, 1 }
                };
            }
        }

        #endregion
        #region MOTION BLUR 7X7 - MotionBlur7X7

        /// <summary>
        /// MOTION BLUR 7X7
        /// </summary>
        public static double[,] MotionBlur7X7
        {
            get
            {
                return new double[,]
                {
                    { 1, 0, 0, 0, 0, 0, 1 }, 
                    { 0, 1, 0, 0, 0, 1, 0 }, 
                    { 0, 0, 1, 0, 1, 0, 0 },
                    { 0, 0, 0, 1, 0, 0, 0 },
                    { 0, 0, 1, 0, 1, 0, 0 },
                    { 0, 1, 0, 0, 0, 1, 0 },
                    { 1, 0, 0, 0, 0, 0, 1 }
                };
            }
        }

        #endregion
        #region MOTION BLUR 7X7 AT 45 DEGREES - MotionBlur7X7At45Degrees

        /// <summary>
        /// MOTION BLUR 7X7 AT 45 DEGREES
        /// </summary>
        public static double[,] MotionBlur7X7At45Degrees
        {
            get
            {
                return new double[,]
                {
                    { 0, 0, 0, 0, 0, 0, 1 }, 
                    { 0, 0, 0, 0, 0, 1, 0 }, 
                    { 0, 0, 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 1, 0, 0, 0 },
                    { 0, 0, 1, 0, 0, 0, 0 },
                    { 0, 1, 0, 0, 0, 0, 0 },
                    { 1, 0, 0, 0, 0, 0, 0 }
                };
            }
        }

        #endregion
        #region MOTION BLUR 7X7 AT 135 DEGREES - MotionBlur7X7At135Degrees

        /// <summary>
        /// MOTION BLUR 7X7 AT 135 DEGREES
        /// </summary>
        public static double[,] MotionBlur7X7At135Degrees
        {
            get
            {
                return new double[,]
                {
                    { 1, 0, 0, 0, 0, 0, 0 }, 
                    { 0, 1, 0, 0, 0, 0, 0 }, 
                    { 0, 0, 1, 0, 0, 0, 0 },
                    { 0, 0, 0, 1, 0, 0, 0 },
                    { 0, 0, 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 0, 0, 1, 0 },
                    { 0, 0, 0, 0, 0, 0, 1 }
                };
            }
        }

        #endregion
        #region MOTION BLUR 9X9 - MotionBlur9X9

        /// <summary>
        /// MOTION BLUR 9X9
        /// </summary>
        public static double[,] MotionBlur9X9
        {
            get
            {
                return new double[,]
                {
                    { 1, 0, 0, 0, 0, 0, 0, 0, 1 },
                    { 0, 1, 0, 0, 0, 0, 0, 1, 0 },
                    { 0, 0, 1, 0, 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 1, 0, 1, 0, 0, 0 },
                    { 0, 0, 0, 0, 1, 0, 0, 0, 0 },
                    { 0, 0, 0, 1, 0, 1, 0, 0, 0 },
                    { 0, 0, 1, 0, 0, 0, 1, 0, 0 },
                    { 0, 1, 0, 0, 0, 0, 0, 1, 0 },
                    { 1, 0, 0, 0, 0, 0, 0, 0, 1 }
                };
            }
        }

        #endregion
        #region MOTION BLUR 9X9 AT 45 DEGREES - MotionBlur9X9At45Degrees

        /// <summary>
        /// MOTION BLUR 9X9 AT 45 DEGREES
        /// </summary>
        public static double[,] MotionBlur9X9At45Degrees
        {
            get
            {
                return new double[,]
                {
                    { 0, 0, 0, 0, 0, 0, 0, 0, 1 },
                    { 0, 0, 0, 0, 0, 0, 0, 1, 0 },
                    { 0, 0, 0, 0, 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 0, 0, 1, 0, 0, 0 },
                    { 0, 0, 0, 0, 1, 0, 0, 0, 0 },
                    { 0, 0, 0, 1, 0, 0, 0, 0, 0 },
                    { 0, 0, 1, 0, 0, 0, 0, 0, 0 },
                    { 0, 1, 0, 0, 0, 0, 0, 0, 0 },
                    { 1, 0, 0, 0, 0, 0, 0, 0, 0 }
                };
            }
        }

        #endregion
        #region MOTION BLUR 9X9 AT 135 DEGREES - MotionBlur9X9At135Degrees

        /// <summary>
        /// MOTION BLUR 9X9 AT 135 DEGREES
        /// </summary>
        public static double[,] MotionBlur9X9At135Degrees
        {
            get
            {
                return new double[,]
                {
                    { 1, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 1, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 1, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 1, 0, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 1, 0, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 1, 0, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 1, 0, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 1, 0 },
                    { 0, 0, 0, 0, 0, 0, 0, 0, 1 }
                };
            }
        }

        #endregion
    }
}

 

728x90

 

▶ BlurType.cs

namespace TestProject
{
    /// <summary>
    /// 불선명 타입
    /// </summary>
    public enum BlurType
    {
        /// <summary>
        /// 평균 3X3
        /// </summary>
        Mean3X3,

        /// <summary>
        /// 평균 5X5
        /// </summary>
        Mean5X5,

        /// <summary>
        /// 평균 7X7
        /// </summary>
        Mean7X7,

        /// <summary>
        /// 평균 9X9
        /// </summary>
        Mean9X9,

        /// <summary>
        /// 가우시안 BLUR 3X3
        /// </summary>
        GaussianBlur3X3,

        /// <summary>
        /// 가우시안 BLUR 5X5
        /// </summary>
        GaussianBlur5X5,

        /// <summary>
        /// 모션 BLUR 5X5
        /// </summary>
        MotionBlur5X5,

        /// <summary>
        /// 모션 BLUR 5X5 45도
        /// </summary>
        MotionBlur5X5At45Degrees,

        /// <summary>
        /// 모션 BLUR 5X5 135도
        /// </summary>
        MotionBlur5X5At135Degrees,

        /// <summary>
        /// 모션 BLUR 7X7
        /// </summary>
        MotionBlur7X7,

        /// <summary>
        /// 모션 BLUR 7X7 45도
        /// </summary>
        MotionBlur7X7At45Degrees,

        /// <summary>
        /// 모션 BLUR 7X7 135도
        /// </summary>
        MotionBlur7X7At135Degrees,

        /// <summary>
        /// 모션 BLUR 9X9
        /// </summary>
        MotionBlur9X9,

        /// <summary>
        /// 모션 BLUR 9X9 45도
        /// </summary>
        MotionBlur9X9At45Degrees,

        /// <summary>
        /// 모션 BLUR 9X9 135도
        /// </summary>
        MotionBlur9X9At135Degrees,

        /// <summary>
        /// 중앙값 3X3
        /// </summary>
        Median3X3,

        /// <summary>
        /// 중앙값 5X5
        /// </summary>
        Median5X5,

        /// <summary>
        /// 중앙값 7X7
        /// </summary>
        Median7X7,

        /// <summary>
        /// 중앙값 9X9
        /// </summary>
        Median9X9,

        /// <summary>
        /// 중앙값 11X11
        /// </summary>
        Median11X11
    }
}

 

300x250

 

▶ BitmapHelper.cs

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

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

        #region 비트맵 로드하기 - LoadBitmap(filePath)

        /// <summary>
        /// 비트맵 로드하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>비트맵</returns>
        public static Bitmap LoadBitmap(string filePath)
        {
            using(Bitmap bitmap = new Bitmap(filePath))
            {
                return new Bitmap(bitmap);
            }
        }

        #endregion
        #region 불선명 필터 적용하기 - ApplyBlurFilter(sourceBitmap, blurType)

        /// <summary>
        /// 불선명 필터 적용하기
        /// </summary>
        /// <param name="sourceBitmap">소스 비트맵</param>
        /// <param name="blurType">불선명 타입</param>
        /// <returns>비트맵</returns>
        public static Bitmap ApplyBlurFilter(Bitmap sourceBitmap, BlurType blurType)
        {
            Bitmap targetBitmap = null;

            switch(blurType)
            {
                case BlurType.Mean3X3 :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.Mean3X3, 1.0 / 9.0, 0);

                    break;

                case BlurType.Mean5X5 :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.Mean5X5, 1.0 / 25.0, 0);

                    break;

                case BlurType.Mean7X7 :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.Mean7X7, 1.0 / 49.0, 0);

                    break;

                case BlurType.Mean9X9 :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.Mean9X9, 1.0 / 81.0, 0);

                    break;

                case BlurType.GaussianBlur3X3 :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.GaussianBlur3X3, 1.0 / 16.0, 0);

                    break;

                case BlurType.GaussianBlur5X5 :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.GaussianBlur5X5, 1.0 / 159.0, 0);

                    break;

                case BlurType.MotionBlur5X5 :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.MotionBlur5X5, 1.0 / 10.0, 0);

                    break;

                case BlurType.MotionBlur5X5At45Degrees :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.MotionBlur5X5At45Degrees, 1.0 / 5.0, 0);

                    break;

                case BlurType.MotionBlur5X5At135Degrees :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.MotionBlur5X5At135Degrees, 1.0 / 5.0, 0);

                    break;

                case BlurType.MotionBlur7X7 :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.MotionBlur7X7, 1.0 / 14.0, 0);

                    break;

                case BlurType.MotionBlur7X7At45Degrees :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.MotionBlur7X7At45Degrees, 1.0 / 7.0, 0);

                    break;

                case BlurType.MotionBlur7X7At135Degrees :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.MotionBlur7X7At135Degrees, 1.0 / 7.0, 0);

                    break;

                case BlurType.MotionBlur9X9 :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.MotionBlur9X9, 1.0 / 18.0, 0);

                    break;

                case BlurType.MotionBlur9X9At45Degrees :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.MotionBlur9X9At45Degrees, 1.0 / 9.0, 0);

                    break;

                case BlurType.MotionBlur9X9At135Degrees :

                    targetBitmap = ApplyConvolutionFilter(sourceBitmap, Matrix.MotionBlur9X9At135Degrees, 1.0 / 9.0, 0);

                    break;

                case BlurType.Median3X3 :

                    targetBitmap = ApplyMedianFilter(sourceBitmap, 3);

                    break;

                case BlurType.Median5X5 :

                    targetBitmap = ApplyMedianFilter(sourceBitmap, 5);

                    break;

                case BlurType.Median7X7 :

                    targetBitmap = ApplyMedianFilter(sourceBitmap, 7);

                    break;

                case BlurType.Median9X9 :

                    targetBitmap = ApplyMedianFilter(sourceBitmap, 9);

                    break;

                case BlurType.Median11X11 :

                    targetBitmap = ApplyMedianFilter(sourceBitmap, 11);

                    break;
            }

            return targetBitmap;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Private
        
        #region 회선 필터 적용하기 - ApplyConvolutionFilter(sourceBitmap, filterArray, factor, bias)

        /// <summary>
        /// 회선 필터 적용하기
        /// </summary>
        /// <param name="sourceBitmap">소스 비트맵</param>
        /// <param name="filterArray">필터 배열</param>
        /// <param name="factor">인자</param>
        /// <param name="bias">바이어스</param>
        /// <returns>비트맵</returns>
        private static Bitmap ApplyConvolutionFilter(Bitmap sourceBitmap, double[,] filterArray, double factor = 1, int bias = 0)
        {
            BitmapData sourceBitmapData = sourceBitmap.LockBits
            (
                new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height),
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppArgb
            );

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

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

            sourceBitmap.UnlockBits(sourceBitmapData);

            double blue  = 0.0;
            double green = 0.0;
            double red   = 0.0;

            int filterWidth     = filterArray.GetLength(1);
            int filterHeight    = filterArray.GetLength(0);
            int filterOffset    = (filterWidth - 1) / 2;
            int temporaryOffset = 0;
            int byteOffset      = 0;

            for(int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++)
            {
                for(int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++)
                {
                    blue  = 0;
                    green = 0;
                    red   = 0;

                    byteOffset = offsetY * sourceBitmapData.Stride + offsetX * 4;

                    for(int filterY = -filterOffset; filterY <= filterOffset; filterY++)
                    {
                        for(int filterX = -filterOffset; filterX <= filterOffset; filterX++)
                        {
                            temporaryOffset = byteOffset + (filterX * 4) + (filterY * sourceBitmapData.Stride);

                            blue += (double)(sourceByteArray[temporaryOffset]) * 
                                    filterArray[filterY + filterOffset, filterX + filterOffset];

                            green += (double)(sourceByteArray[temporaryOffset + 1]) *
                                        filterArray[filterY + filterOffset, filterX + filterOffset];

                            red += (double)(sourceByteArray[temporaryOffset + 2]) *
                                    filterArray[filterY + filterOffset, filterX + filterOffset];
                        }
                    }

                    blue  = factor * blue + bias;
                    green = factor * green + bias;
                    red   = factor * red + bias;

                    blue  = (blue  > 255 ? 255 : (blue  < 0 ? 0 : blue ));
                    green = (green > 255 ? 255 : (green < 0 ? 0 : green));
                    red   = (red   > 255 ? 255 : (red   < 0 ? 0 : red  ));

                    targetByteArray[byteOffset    ] = (byte)(blue);
                    targetByteArray[byteOffset + 1] = (byte)(green);
                    targetByteArray[byteOffset + 2] = (byte)(red);
                    targetByteArray[byteOffset + 3] = 255;
                }
            }

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

            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
        #region 중앙값 필터 적용하기 - ApplyMedianFilter(sourceBitmap, matrixSize)

        /// <summary>
        /// 중앙값 필터 적용하기
        /// </summary>
        /// <param name="sourceBitmap">소스 비트맵</param>
        /// <param name="matrixSize">매트릭스 크기</param>
        /// <returns>비트맵</returns>
        private static Bitmap ApplyMedianFilter(Bitmap sourceBitmap, int matrixSize)
        {
            BitmapData sourceBitmapData = sourceBitmap.LockBits
            (
                new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height),
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppArgb
            );

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

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

            sourceBitmap.UnlockBits(sourceBitmapData);

            int filterOffset = (matrixSize - 1) / 2;

            int temporaryOffset = 0;

            int byteOffset = 0;

            List<int> neighbourPixelList = new List<int>();

            byte[] middlePixel;

            for(int offsetY = filterOffset; offsetY < sourceBitmap.Height - filterOffset; offsetY++)
            {
                for(int offsetX = filterOffset; offsetX < sourceBitmap.Width - filterOffset; offsetX++)
                {
                    byteOffset = offsetY * sourceBitmapData.Stride + offsetX * 4;

                    neighbourPixelList.Clear();

                    for(int filterY = -filterOffset; filterY <= filterOffset; filterY++)
                    {
                        for(int filterX = -filterOffset; filterX <= filterOffset; filterX++)
                        {
                            temporaryOffset = byteOffset + (filterX * 4) + (filterY * sourceBitmapData.Stride);

                            neighbourPixelList.Add(BitConverter.ToInt32(sourceByteArray, temporaryOffset));
                        }
                    }

                    neighbourPixelList.Sort();

                    middlePixel = BitConverter.GetBytes(neighbourPixelList[filterOffset]);

                    targetByteArray[byteOffset    ] = middlePixel[0];
                    targetByteArray[byteOffset + 1] = middlePixel[1];
                    targetByteArray[byteOffset + 2] = middlePixel[2];
                    targetByteArray[byteOffset + 3] = middlePixel[3];
                }
            }

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

            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.Drawing;
using System.Windows.Forms;

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

        #region 생성자 - MainForm()

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

            Bitmap sourceBitmap = BitmapHelper.LoadBitmap("IMAGE\\sample.jpg");
            Bitmap targetBitmap = BitmapHelper.ApplyBlurFilter(sourceBitmap, BlurType.MotionBlur9X9At135Degrees);

            this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
            this.pictureBox.Image    = targetBitmap;
        }

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

댓글을 달아 주세요