728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 비트맵 1
/// </summary>
private Bitmap bitmap1;
/// <summary>
/// 비트맵 2
/// </summary>
private Bitmap bitmap2;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
Load += Form_Load;
this.pictureBox1.Paint += pictureBox1_Paint;
this.pictureBox2.Paint += pictureBox2_Paint;
}
#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)
{
int width = this.pictureBox1.ClientSize.Width;
int height = this.pictureBox1.ClientSize.Height;
this.bitmap1 = new Bitmap(width, height);
using(Graphics graphics = Graphics.FromImage(this.bitmap1))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.Clear(Color.White);
DrawSmiley(graphics, this.pictureBox1.ClientRectangle, 10);
this.bitmap1.MakeTransparent(Color.White);
}
this.bitmap2 = new Bitmap(width, height);
using(Graphics graphics = Graphics.FromImage(this.bitmap2))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.Clear(Color.Transparent);
DrawSmiley(graphics, this.pictureBox1.ClientRectangle, 10);
}
}
#endregion
#region 픽처 박스 1 페인트시 처리하기 - pictureBox1_Paint(sender, e)
/// <summary>
/// 픽처 박스 1 페인트시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.Blue);
e.Graphics.DrawImage(this.bitmap1, 0, 0);
}
#endregion
#region 픽처 박스 2 페인트시 처리하기 - pictureBox2_Paint(sender, e)
/// <summary>
/// 픽처 박스 2페인트시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.Blue);
e.Graphics.DrawImage(this.bitmap2, 0, 0);
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 웃는 얼굴 그리기 - DrawSmiley(graphics, rectangle, width)
/// <summary>
/// 웃는 얼굴 그리기
/// </summary>
/// <param name="graphics">그래픽스</param>
/// <param name="rectangle">사각형</param>
/// <param name="width">너비</param>
private void DrawSmiley(Graphics graphics, Rectangle rectangle, int width)
{
rectangle.Inflate(-4, -4);
using(Pen pen = new Pen(Color.Black, width))
{
// 얼굴
Rectangle faceRectangle = new Rectangle
(
rectangle.Left + width / 2,
rectangle.Top + width / 2,
rectangle.Width - width,
rectangle.Height - width
);
graphics.FillEllipse(Brushes.Yellow, faceRectangle);
graphics.DrawEllipse(pen, faceRectangle);
// 입
pen.Width /= 2;
faceRectangle.Inflate(-30, -30);
graphics.DrawArc(pen, faceRectangle, 10, 160);
// 왼쪽 눈
int eyeWidth = (int)(rectangle.Width * 0.2 );
int eyeHeight = (int)(rectangle.Height * 0.25);
Rectangle eyeRectangle = new Rectangle
(
(int)(rectangle.Left + rectangle.Width * 0.25),
(int)(rectangle.Top + rectangle.Height * 0.20),
eyeWidth,
eyeHeight
);
graphics.FillEllipse(Brushes.LightBlue, eyeRectangle);
graphics.DrawEllipse(pen, eyeRectangle);
Rectangle pupilRectangle = new Rectangle
(
eyeRectangle.Left + eyeWidth / 2,
eyeRectangle.Top + eyeHeight / 4,
eyeWidth / 2,
eyeHeight / 2
);
graphics.FillEllipse(Brushes.Black, pupilRectangle);
graphics.DrawEllipse(pen, pupilRectangle);
// 오른쪽 눈
eyeRectangle = new Rectangle
(
(int)(rectangle.Right - rectangle.Width * 0.25) - eyeWidth,
(int)(rectangle.Top + rectangle.Height * 0.20),
eyeWidth,
eyeHeight
);
graphics.FillEllipse(Brushes.LightBlue, eyeRectangle);
graphics.DrawEllipse(pen, eyeRectangle);
pupilRectangle = new Rectangle
(
eyeRectangle.Left + eyeWidth / 2,
eyeRectangle.Top + eyeHeight / 4,
eyeWidth / 2,
eyeHeight / 2
);
graphics.FillEllipse(Brushes.Black, pupilRectangle);
graphics.DrawEllipse(pen, pupilRectangle);
// 코
Rectangle noseRectangle = new Rectangle
(
(int)(rectangle.Left + rectangle.Width / 2) - eyeWidth / 2,
(int)(rectangle.Top + rectangle.Height * 0.45),
eyeWidth,
eyeWidth
);
graphics.FillEllipse(Brushes.LightGreen, noseRectangle);
graphics.DrawEllipse(pen, noseRectangle);
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 직선 방향의 가운데 정렬 텍스트 그리기 (0) | 2020.07.19 |
---|---|
[C#/WINFORM] Bitmap 클래스 : 비트맵 크기 변경하기 (0) | 2020.07.16 |
[C#/WINFORM] ColorMatrix 클래스 : 이미지 불투명도 조정하기 (0) | 2020.07.13 |
[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 |
[C#/WINFORM] 사인(Sine)과 코사인(Cosine)을 사용해 원과 타원 그리기 (0) | 2020.07.09 |
[C#/WINFORM] Bitmap 클래스 : 투명한 구멍을 갖는 비트맵 만들기 (0) | 2020.07.05 |
[C#/WINFORM] PictureBox 클래스 : 사용자 그리기 및 확대/축소하기 (0) | 2020.07.05 |
댓글을 달아 주세요