728x90
반응형
728x170
▶ 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
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 소스 이미지
/// </summary>
private Image sourceImage = null;
/// <summary>
/// 타겟 이미지
/// </summary>
private Image targetImage = null;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
Load += Form_Load;
this.openMenuItem.Click += openMenuItem_Click;
this.saveAsMenuItem.Click += saveAsMenuItem_Click;
this.exitMenuItem.Click += exitMenuItem_Click;
this.resetMenuItem.Click += resetMenuItem_Click;
this.invertMenuItem.Click += invertMenuItem_Click;
this.removeRedMenuitem.Click += removeRedMenuitem_Click;
this.removeGreenMenuItem.Click += removeGreenMenuItem_Click;
this.removeBlueMenuItem.Click += removeBlueMenuItem_Click;
this.decreaseAlphaMenuItem.Click += decreaseAlphaMenuItem_Click;
this.increaseAlphaMenuItem.Click += increaseAlphaMenuItem_Click;
this.grayscaleMenuItem.Click += grayscaleMenuItem_Click;
this.averageMenuItem.Click += averageMenuItem_Click;
this.darkenMenuItem.Click += darkenMenuItem_Click;
this.brightenMenuItem.Click += brightenMenuItem_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 폼 로드시 처리하기 - Form_Load(sender, e)
/// <summary>
/// 폼 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Form_Load(object sender, EventArgs e)
{
SetMenuEnabled(this.menuSeparator5, false);
}
#endregion
#region 열기 메뉴 항목 클릭시 처리하기 - openMenuItem_Click(sender, e)
/// <summary>
/// 열기 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void openMenuItem_Click(object sender, EventArgs e)
{
if(this.openFileDialog.ShowDialog() == DialogResult.OK)
{
this.sourceImage = LoadBitmapUnlocked(openFileDialog.FileName);
this.targetImage = this.sourceImage;
ShowImage();
this.pictureBox.Visible = true;
ClientSize = new Size
(
this.pictureBox.Right + this.pictureBox.Left,
this.pictureBox.Bottom + this.pictureBox.Left
);
this.menuSeparator5.Enabled = true;
this.saveAsMenuItem.Enabled = true;
SetMenuEnabled(this.menuSeparator5, true);
}
}
#endregion
#region 다른 이름으로 저장 메뉴 항목 클릭시 처리하기 - saveAsMenuItem_Click(sender, e)
/// <summary>
/// 다른 이름으로 저장 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void saveAsMenuItem_Click(object sender, EventArgs e)
{
if(this.saveFileDialog.ShowDialog() == DialogResult.OK)
{
SaveImage(this.targetImage, this.saveFileDialog.FileName);
}
}
#endregion
#region 종료 메뉴 항목 클릭시 처리하기 - exitMenuItem_Click(sender, e)
/// <summary>
/// 종료 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void exitMenuItem_Click(object sender, EventArgs e)
{
Close();
}
#endregion
#region 리셋 메뉴 항목 클릭시 처리하기 - resetMenuItem_Click(sender, e)
/// <summary>
/// 리셋 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void resetMenuItem_Click(object sender, EventArgs e)
{
this.targetImage = this.sourceImage;
ShowImage();
}
#endregion
#region 반전 메뉴 항목 클릭시 처리하기 - invertMenuItem_Click(sender, e)
/// <summary>
/// 반전 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void invertMenuItem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { -1, 0, 0, 0, 0 },
new float[] { 0, -1, 0, 0, 0 },
new float[] { 0, 0, -1, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 1, 1, 1, 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
#region 적색 제거 메뉴 항목 클릭시 처리하기 - removeRedMenuitem_Click(sender, e)
/// <summary>
/// 적색 제거 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void removeRedMenuitem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { 0, 0, 0, 0, 0 },
new float[] { 0, 1, 0, 0, 0 },
new float[] { 0, 0, 1, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
#region 녹색 제거 메뉴 항목 클릭시 처리하기 - removeGreenMenuItem_Click(sender, e)
/// <summary>
/// 녹색 제거 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void removeGreenMenuItem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { 1, 0, 0, 0, 0 },
new float[] { 0, 0, 0, 0, 0 },
new float[] { 0, 0, 1, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
#region 청색 제거 메뉴 항목 클릭시 처리하기 - removeBlueMenuItem_Click(sender, e)
/// <summary>
/// 청색 제거 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void removeBlueMenuItem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { 1, 0, 0, 0, 0 },
new float[] { 0, 1, 0, 0, 0 },
new float[] { 0, 0, 0, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
#region 알파 감소 메뉴 항목 클릭시 처리하기 - decreaseAlphaMenuItem_Click(sender, e)
/// <summary>
/// 알파 감소 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void decreaseAlphaMenuItem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { 1, 0, 0, 0, 0 },
new float[] { 0, 1, 0, 0, 0 },
new float[] { 0, 0, 1, 0, 0 },
new float[] { 0, 0, 0, 0.8f, 0 },
new float[] { 0, 0, 0, 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
#region 알파 증가 메뉴 항목 클릭시 처리하기 - increaseAlphaMenuItem_Click(sender, e)
/// <summary>
/// 알파 증가 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void increaseAlphaMenuItem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { 1, 0, 0, 0, 0 },
new float[] { 0, 1, 0, 0, 0 },
new float[] { 0, 0, 1, 0, 0 },
new float[] { 0, 0, 0, 1.25f, 0 },
new float[] { 0, 0, 0, 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
#region 회색조 메뉴 항목 클릭시 처리하기 - grayscaleMenuItem_Click(sender, e)
/// <summary>
/// 회색조 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void grayscaleMenuItem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { 0.299f, 0.299f, 0.299f, 0, 0 },
new float[] { 0.587f, 0.587f, 0.587f, 0, 0 },
new float[] { 0.114f, 0.114f, 0.114f, 0, 0 },
new float[] { 0 , 0 , 0 , 1, 0 },
new float[] { 0 , 0 , 0 , 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
#region 평균 메뉴 항목 클릭시 처리하기 - averageMenuItem_Click(sender, e)
/// <summary>
/// 평균 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void averageMenuItem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { 0.333f, 0.333f, 0.333f, 0, 0 },
new float[] { 0.333f, 0.333f, 0.333f, 0, 0 },
new float[] { 0.333f, 0.333f, 0.333f, 0, 0 },
new float[] { 0 , 0 , 0 , 1, 0 },
new float[] { 0 , 0 , 0 , 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
#region 어둡게 메뉴 항목 클릭시 처리하기 - darkenMenuItem_Click(sender, e)
/// <summary>
/// 어둡게 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void darkenMenuItem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { 0.8f, 0, 0, 0, 0 },
new float[] { 0, 0.8f, 0, 0, 0 },
new float[] { 0, 0, 0.8f, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
#region 밝게 메뉴 항목 클릭시 처리하기 - brightenMenuItem_Click(sender, e)
/// <summary>
/// 밝게 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void brightenMenuItem_Click(object sender, EventArgs e)
{
ColorMatrix colorMatrix = new ColorMatrix
(
new float[][]
{
new float[] { 1.2f, 0, 0, 0, 0 },
new float[] { 0, 1.2f, 0, 0, 0 },
new float[] { 0, 0, 1.2f, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
}
);
this.targetImage = ApplyColorMatrix(this.targetImage, colorMatrix);
ShowImage();
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 메뉴 이용 가능 여부 설정하기 - SetMenuEnabled(menuItem, enabled)
/// <summary>
/// 메뉴 이용 가능 여부 설정하기
/// </summary>
/// <param name="menuItem">메뉴 항목</param>
/// <param name="enabled">이용 가능 여부</param>
private void SetMenuEnabled(ToolStripMenuItem menuItem, bool enabled)
{
foreach(ToolStripItem item in menuItem.DropDownItems)
{
item.Enabled = enabled;
}
}
#endregion
#region 잠금없이 비트맵 로드하기 - LoadBitmapUnlocked(filePath)
/// <summary>
/// 잠금없이 비트맵 로드하기
/// </summary>
/// <param name="filePath">파일 경로</param>
/// <returns>비트맵</returns>
private Bitmap LoadBitmapUnlocked(string filePath)
{
using(Bitmap bitmap = new Bitmap(filePath))
{
return new Bitmap(bitmap);
}
}
#endregion
#region 이미지 표시하기 - ShowImage()
/// <summary>
/// 이미지 표시하기
/// </summary>
private void ShowImage()
{
int width = this.targetImage.Width;
int height = this.targetImage.Height;
Bitmap bitmap = new Bitmap(width, height);
using(Graphics graphics = Graphics.FromImage(bitmap))
{
const int step = 20;
for(int x = 0; x < width; x += step)
{
for(int y = 0; y < height; y += step)
{
if(((x / step) + (y / step)) % 2 == 0)
{
graphics.FillRectangle(Brushes.Red, x, y, step, step);
}
else
{
graphics.FillRectangle(Brushes.Yellow, x, y, step, step);
}
}
}
graphics.DrawImage(this.targetImage, new Point(0, 0));
}
this.pictureBox.Image = bitmap;
}
#endregion
#region 이미지 저장하기 - SaveImage(image, filePath)
/// <summary>
/// 이미지 저장하기
/// </summary>
/// <param name="image">이미지</param>
/// <param name="filePath">파일 경로</param>
public void SaveImage(Image image, string filePath)
{
string extension = Path.GetExtension(filePath);
switch(extension.ToLower())
{
case ".bmp" : image.Save(filePath, ImageFormat.Bmp ); break;
case ".exif" : image.Save(filePath, ImageFormat.Exif); break;
case ".gif" : image.Save(filePath, ImageFormat.Gif ); break;
case ".jpg" :
case ".jpeg" : image.Save(filePath, ImageFormat.Jpeg); break;
case ".png" : image.Save(filePath, ImageFormat.Png ); break;
case ".tif" :
case ".tiff" : image.Save(filePath, ImageFormat.Tiff); break;
default : throw new NotSupportedException("Unknown file extension " + extension);
}
}
#endregion
#region 색상 매트릭스 적용하기 - ApplyColorMatrix(image, colorMatrix)
/// <summary>
/// 색상 매트릭스 적용하기
/// </summary>
/// <param name="image">이미지</param>
/// <param name="colorMatrix">색상 매트릭스</param>
/// <returns>이미지</returns>
private Image ApplyColorMatrix(Image image, ColorMatrix colorMatrix)
{
Bitmap bitmap = new Bitmap(image.Width, image.Height);
using(Graphics graphics = Graphics.FromImage(bitmap))
{
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix);
Point[] targetPointArray =
{
new Point(0 , 0 ),
new Point(image.Width, 0 ),
new Point(0 , image.Height)
};
Rectangle sourceRectangle = new Rectangle(0, 0, image.Width, image.Height);
graphics.DrawImage
(
image,
targetPointArray,
sourceRectangle,
GraphicsUnit.Pixel,
imageAttributes
);
}
return bitmap;
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 확대/축소된 이미지를 특정 크기로 자르기 (0) | 2020.08.07 |
---|---|
[C#/WINFORM] Graphics 클래스 : FillPolygon 메소드 사용하기 (0) | 2020.08.07 |
[C#/WINFORM] 다른 선 아래로 지나가는 것을 보여주기 위해 간격이 있는 선 그리기 (0) | 2020.08.06 |
[C#/WINFORM] 미로 길 찾기 (애니메이션) (0) | 2020.08.05 |
[C#/WINFORM] 미로 길 찾기 (0) | 2020.08.05 |
[C#/WINFORM] ImageAttributes 클래스 : 이미지 수정하기 (0) | 2020.08.04 |
[C#/WINFORM] 라운드 다각형 그리기 (0) | 2020.08.03 |
[C#/WINFORM] 기준선을 사용해 이미지 회전하기 (0) | 2020.08.03 |
[C#/WINFORM] 마우스 아래 그려진 문자 찾기 (0) | 2020.08.03 |
[C#/WINFORM] 다각형 내에서 이미지 그리기 (0) | 2020.08.03 |
[C#/WINFORM] RGB 색상에서 HSV 색상 구하기 (0) | 2020.08.02 |
댓글을 달아 주세요