첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

#region 세피아 톤 비트맵 구하기 - GetSepiaToneBitmap(sourceImage)

/// <summary>
/// 세피아 톤 비트맵 구하기
/// </summary>
/// <param name="sourceImage">소스 이미지</param>
/// <returns>세피아 톤 비트맵</returns>
public Bitmap GetSepiaToneBitmap(Image sourceImage)
{
    Bitmap targetBitmap = GetBitmap(sourceImage);

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

    IntPtr sourceHandle = targetBitmapData.Scan0;

    byte[] targetByteArray = new byte[targetBitmapData.Stride * targetBitmap.Height];

    Marshal.Copy(sourceHandle, targetByteArray, 0, targetByteArray.Length);

    byte maximumValue = 255;

    float red   = 0;
    float green = 0;
    float blue  = 0;

    for(int i = 0; i < targetByteArray.Length; i += 4)
    {
        red   = targetByteArray[i] * 0.189f + targetByteArray[i + 1] * 0.769f + targetByteArray[i + 2] * 0.393f;
        green = targetByteArray[i] * 0.168f + targetByteArray[i + 1] * 0.686f + targetByteArray[i + 2] * 0.349f;
        blue  = targetByteArray[i] * 0.131f + targetByteArray[i + 1] * 0.534f + targetByteArray[i + 2] * 0.272f;

        targetByteArray[i + 2] = (red   > maximumValue ? maximumValue : (byte)red  );
        targetByteArray[i + 1] = (green > maximumValue ? maximumValue : (byte)green);
        targetByteArray[i    ] = (blue  > maximumValue ? maximumValue : (byte)blue );
    }

    Marshal.Copy(targetByteArray, 0, sourceHandle, targetByteArray.Length);

    targetBitmap.UnlockBits(targetBitmapData);

    targetBitmapData = null;
    targetByteArray  = null;

    return targetBitmap;
}

#endregion
#region 비트맵 구하기 - GetBitmap(sourceImage)

/// <summary>
/// 비트맵 구하기
/// </summary>
/// <param name="sourceImage">소스 이미지</param>
/// <returns>비트맵</returns>
private Bitmap GetBitmap(Image sourceImage)
{
    Bitmap targetBitmap = new Bitmap
    (
        sourceImage.Width,
        sourceImage.Height,
        PixelFormat.Format32bppArgb
    );

    using(Graphics graphics = Graphics.FromImage(targetBitmap))
    {
        graphics.DrawImage
        (
            sourceImage,
            new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height),
            new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height),
            GraphicsUnit.Pixel
        );

        graphics.Flush();
    }

    return targetBitmap;
}

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

댓글을 달아 주세요