반응형
* C# 이미지 밝기 조절 (Image Brightness) 예제...
- 사용한 컨트롤 : Button 3개, PictureBox 1개
전체 소스 코드
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CSharp_ImageBrightness
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "JPG File(*.jpg) | *.jpg";
if (ofd.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(ofd.FileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
//20 씩 밝기 증가
pictureBox1.Image = BrightnessImage(pictureBox1.Image, 20);
}
private void button3_Click(object sender, EventArgs e)
{
//20 씩 밝기 감소
pictureBox1.Image = BrightnessImage(pictureBox1.Image, -20);
}
private Bitmap BrightnessImage(Image imSource, int iBrightness)
{
Bitmap btTmp = new Bitmap(imSource);
Color cTmp;
int iR, iG, iB;
for (int iY = 0; iY < btTmp.Height; iY++)
{
for (int iX = 0; iX < btTmp.Width; iX++)
{
cTmp = btTmp.GetPixel(iX, iY);
iR = Math.Max(0, Math.Min(255, cTmp.R + iBrightness));
iG = Math.Max(0, Math.Min(255, cTmp.G + iBrightness));
iB = Math.Max(0, Math.Min(255, cTmp.B + iBrightness));
cTmp = Color.FromArgb(iR, iG, iB);
btTmp.SetPixel(iX, iY, cTmp);
}
}
return btTmp;
}
}
}
* 예제 결과
- 이미지 파일 열기...
- 밝기 조절 없이 기본 화면
- 밝기 100 정도 된 화면
- 밝기 -100 정도 된 화면
반응형