728x90
728x170
■ Bitmap 클래스을 사용해 세피아 톤 비트맵을 구하는 방법을 보여준다.
▶ 예제 코드 (C#)
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
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] LED 텍스트 표시하기 (0) | 2020.08.09 |
---|---|
[C#/WINFORM] Bitmap 클래스 : 이미지 나선 효과 사용하기 (0) | 2020.08.08 |
[C#/WINFORM] Bitmap 클래스 : 잠금없이 비트맵 파일 로드하기 (0) | 2020.08.08 |
[C#/WINFORM] Image 클래스 : 이미지를 파일로 저장하기 (0) | 2020.08.08 |
[C#/WINFORM] Bitmap 클래스 : 회색조/투명/네거티브/세피아 톤 필터 사용하기 (0) | 2020.08.08 |
[C#/WINFORM] Bitmap 클래스 : 네거티브 비트맵 구하기 (0) | 2020.08.08 |
[C#/WINFORM] Bitmap 클래스 : 회색조 비트맵 구하기 (0) | 2020.08.08 |
[C#/WINFORM] Bitmap 클래스 : 투명 비트맵 구하기 (0) | 2020.08.08 |
[C#/WINFORM] Bitmap 클래스 : 32비트 ARGB 포맷 비트맵 구하기 (0) | 2020.08.08 |
[C#/WINFORM] Bitmap 클래스 : 중앙값 필터(Median Filter) 만들기 (0) | 2020.08.08 |