728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 소스 비트맵
/// </summary>
private Bitmap sourceBitmap = null;
/// <summary>
/// 타겟 비트맵
/// </summary>
private Bitmap targetBitmap = null;
/// <summary>
/// 불투명도
/// </summary>
private float opacity = 0.75f;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
this.openMenuItem.Click += openMenuItem_Click;
this.saveMenuItem.Click += saveMenuItem_Click;
this.scrollBar.Scroll += scrollBar_Scroll;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#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.sourceBitmap = new Bitmap(this.openFileDialog.FileName);
ShowImage();
this.pictureBox.Visible = true;
this.saveMenuItem.Enabled = true;
}
}
#endregion
#region 저장하기 메뉴 항목 클릭시 처리하기 - saveMenuItem_Click(sender, e)
/// <summary>
/// 저장하기 메뉴 항목 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void saveMenuItem_Click(object sender, EventArgs e)
{
if(this.saveFileDialog.ShowDialog() == DialogResult.OK)
{
this.targetBitmap.Save(this.saveFileDialog.FileName, ImageFormat.Png);
}
}
#endregion
#region 스크롤바 스크롤시 처리하기 - scrollBar_Scroll(sender, e)
/// <summary>
/// 스크롤바 스크롤시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void scrollBar_Scroll(object sender, ScrollEventArgs e)
{
this.opacity = this.scrollBar.Value / 100f;
this.opacityValueLabel.Text = this.opacity.ToString("0.00");
ShowImage();
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 체커 보드 비트맵 구하기 - GetCheckerboardBitmap(width, height, size)
/// <summary>
/// 체커 보드 비트맵 구하기
/// </summary>
/// <param name="width">너비</param>
/// <param name="height">높이</param>
/// <param name="size">크기</param>
/// <returns>체커 보드 비트맵</returns>
private Bitmap GetCheckerboardBitmap(int width, int height, int size)
{
int rowCount = height / size + 1;
int columnCount = width / size + 1;
Bitmap bitmap = new Bitmap(width, height);
using(Graphics graphics = Graphics.FromImage(bitmap))
{
for(int row = 0; row < rowCount; row++)
{
for(int column = 0; column < columnCount; column++)
{
Rectangle rectangle = new Rectangle
(
column * size,
row * size,
size,
size
);
if((row + column) % 2 == 0)
{
graphics.FillRectangle(Brushes.Blue, rectangle);
}
else
{
graphics.FillRectangle(Brushes.Yellow, rectangle);
}
}
}
}
return bitmap;
}
#endregion
#region 비트맵 구하기 - GetBitmap(sourceBitmap, opacity)
/// <summary>
/// 비트맵 구하기
/// </summary>
/// <param name="sourceBitmap">소스 비트맵</param>
/// <param name="opacity">불투명도</param>
/// <returns>비트맵</returns>
private Bitmap GetBitmap(Bitmap sourceBitmap, float opacity)
{
Bitmap targetBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);
using(Graphics graphics = Graphics.FromImage(targetBitmap))
{
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.Matrix33 = opacity;
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix
(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap
);
Rectangle rectangle = new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height);
graphics.DrawImage
(
sourceBitmap,
rectangle,
0,
0,
sourceBitmap.Width,
sourceBitmap.Height,
GraphicsUnit.Pixel,
imageAttributes
);
}
return targetBitmap;
}
#endregion
#region 이미지 표시하기 - ShowImage()
/// <summary>
/// 이미지 표시하기
/// </summary>
private void ShowImage()
{
if(this.sourceBitmap == null)
{
return;
}
Bitmap temporaryBitmap = GetCheckerboardBitmap(sourceBitmap.Width, sourceBitmap.Height, 64);
this.targetBitmap = GetBitmap(this.sourceBitmap, opacity);
using(Graphics graphics = Graphics.FromImage(temporaryBitmap))
{
graphics.DrawImage(this.targetBitmap, 0, 0);
}
this.pictureBox.Image = temporaryBitmap;
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 개선된 투명 배경 이미지 구하기 (0) | 2020.07.20 |
---|---|
[C#/WINFORM] 둥근 모서리 이미지 구하기 (0) | 2020.07.20 |
[C#/WINFORM] 워터마크 추가하기 (0) | 2020.07.19 |
[C#/WINFORM] 직선 방향의 가운데 정렬 텍스트 그리기 (0) | 2020.07.19 |
[C#/WINFORM] Bitmap 클래스 : 비트맵 크기 변경하기 (0) | 2020.07.16 |
[C#/WINFORM] 호(arc) 그리기/이동하기/수정하기 (0) | 2020.07.12 |
[C#/WINFORM] 호(arc) 위의 마우스 위치 여부 구하기 (0) | 2020.07.12 |
[C#/WINFORM] 안티-알리아싱으로 그리는 경우 투명도 사용하기 (0) | 2020.07.11 |
[C#/WINFORM] 이미지 나선 그리기 (0) | 2020.07.10 |
[C#/WINFORM] Graphics 클래스 : DrawCurve 메소드를 사용해 곡선 그리기 (0) | 2020.07.09 |
댓글을 달아 주세요