[C#] 자연스럽게 Pen으로 그리기
아래의 URL의 질문에 대한 답변입니다.
http://www.hoons.kr/board.aspx?Name=qacshap&Mode=2&BoardIdx=24425&Key=&Value=
그림판을 하나 만들어 볼까 해서
일단은 펜으로 낙서 하는 정도만 코딩을 했는데요
문제가 생겨서 질문 드립니다.
보시면 하늘색이 제가 마우스로 그린거구요
빨간색으로 네모 쳐진게 문제 인데요.
보시면 군데 군데 공백이 있는데요
녹색으로 네모 친 건 직선에서는 문제가 없는데
곡선을 그릴 때 문제가 생기네요
if (this.pictureBox1.Capture&& e.Button == MouseButtons.Left)
{
Pen pen = new Pen(Brushes.DeepSkyBlue, 5);
g = Graphics.FromImage(this.pictureBox1.Image);
g.DrawLine(pen, point1,point2);
point1.X = point2.X;
point1.Y = point2.Y;
point2.X = e.X;
point2.Y = e.Y;
g.Dispose();
this.pictureBox1.Invalidate();
}
코드는 이렇구요
g <-- 이넘은 Graphic 객체 입니다.
저 빨간색으로 네모친 부분을 좀더 스무스(?) 하게 처리할 수 있는 방법이 있을까요?
그럼 오늘도 즐거운 코딩되시길..
--------------------------------------------------------------------------------------------------------------------------------------------------
public Form1() { InitializeComponent(); this.pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove); } Point point1; Point point2; void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { point1 = point2; point2 = e.Location; Pen pen = new Pen(Brushes.DeepSkyBlue, 5); pen.EndCap = System.Drawing.Drawing2D.LineCap.Round; Graphics g = Graphics.FromImage(this.pictureBox1.Image); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; g.DrawLine(pen, point1, point2); g.Dispose(); this.pictureBox1.Invalidate(); } else { point2 = e.Location; } }
1. 먼저 Pen 객체에 끝이 기본적으로 사각형 모양으로 바꾸어야 곡선을 표현 할 때 자연스럽습니다.
2. Graphics 객체의 SmoothingMode 가 있는데, 이 속성을 AntiAlias로 바꾸면 렌더링 시간이 오래 걸리지만 가장 자연스럽게 표현됩니다.
참고로 SmoothingMode는 아래와 같이 정의되어 있습니다.
// Summary: // Specifies whether smoothing (antialiasing) is applied to lines and curves // and the edges of filled areas. public enum SmoothingMode { // Summary: // Specifies an invalid mode. Invalid = -1, // // Summary: // Specifies no antialiasing. Default = 0, // // Summary: // Specifies high speed, low quality rendering. HighSpeed = 1, // // Summary: // Specifies high quality, low speed rendering. HighQuality = 2, // // Summary: // Specifies no antialiasing. None = 3, // // Summary: // Specifies antialiased rendering. AntiAlias = 4, }
3. 또한 PixelOffsetMode 까지 설정할 필요는 없을 것 같은데 참고로 가장 좋은 품질을 주는 HighQuality로 세팅하면 됩니다.
// Summary: // Specifies how pixels are offset during rendering. public enum PixelOffsetMode { // Summary: // Specifies an invalid mode. Invalid = -1, // // Summary: // Specifies the default mode. Default = 0, // // Summary: // Specifies high speed, low quality rendering. HighSpeed = 1, // // Summary: // Specifies high quality, low speed rendering. HighQuality = 2, // // Summary: // Specifies no pixel offset. None = 3, // // Summary: // Specifies that pixels are offset by -.5 units, both horizontally and vertically, // for high speed antialiasing. Half = 4, }
옆에 보이는 것 처럼 원래 보다 자연스럽죠?
"프로그래밍 / TIP& Study" 분류의 다른 글
| [퀴즈] SQLite collate 문제 (0) | 2010/07/16 |
| [TIP] XP, Vista에서 CD/DVD롬이 보이지 않을 경우 (2) | 2009/08/05 |
| [C#] WinForm 에서 Docking 순서 변경하기 (0) | 2009/07/29 |
| [WPF] InkCanvas 사용하기 (2) | 2009/06/29 |
| [C#] Control Library 만들 때 TIP (0) | 2009/06/26 |
| [WPF] Canvas의 Width, Height Binding (0) | 2009/06/23 |
| [C#] DateTime으로 7일 후는 어떻게? (0) | 2009/06/23 |
| [C#] string의 byte 길이 구하기 (0) | 2009/06/22 |



댓글을 달아 주세요