■ 싸인파를 따라 반전된 색상으로 텍스트를 그리는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.pictureBox.Resize += pictureBox_Resize; this.pictureBox.Paint += pictureBox_Paint; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 픽처 박스 크기 변경시 처리하기 - pictureBox_Resize(sender, e) /// <summary> /// 픽처 박스 크기 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_Resize(object sender, EventArgs e) { this.pictureBox.Refresh(); } #endregion #region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e) /// <summary> /// 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_Paint(object sender, PaintEventArgs e) { using(Font font = new Font("나눔고딕코딩", 40, FontStyle.Bold)) { Color topBackgroundColor = Color.RoyalBlue; Color topForegroundColor = Color.White; Color bottomBackgroundColor = Color.White; Color bottomForegroundColor = Color.RoyalBlue; SolidBrush topBackgroundBrush = new SolidBrush(topBackgroundColor ); SolidBrush topForegroundBrush = new SolidBrush(topForegroundColor ); SolidBrush bottomBackgroundBrush = new SolidBrush(bottomBackgroundColor); SolidBrush bottomForegroundBrush = new SolidBrush(bottomForegroundColor); DrawSineSplitText ( e.Graphics, "싸인파 반전 텍스트", font, this.pictureBox.ClientRectangle, topBackgroundBrush, topForegroundBrush, bottomBackgroundBrush, bottomForegroundBrush, 2.5f, 0.25f ); topBackgroundBrush.Dispose(); topForegroundBrush.Dispose(); bottomBackgroundBrush.Dispose(); bottomForegroundBrush.Dispose(); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 싸인파 분리 텍스트 그리기 - DrawSineSplitText(graphics, text, font, rectangle, topBackgroundBrush, topForegroundBrush, bottomBackgroundBrush, bottomForegroundBrush, waveCount, scaleY) /// <summary> /// 싸인파 분리 텍스트 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="text">텍스트</param> /// <param name="font">폰트</param> /// <param name="rectangle">사각형</param> /// <param name="topBackgroundBrush">상단 배경 브러시</param> /// <param name="topForegroundBrush">상단 전경 브러시</param> /// <param name="bottomBackgroundBrush">하단 배경 브러시</param> /// <param name="bottomForegroundBrush">하단 전경 브러시</param> /// <param name="waveCount">웨이브 수</param> /// <param name="scaleY">Y 축적</param> private void DrawSineSplitText ( Graphics graphics, string text, Font font, Rectangle rectangle, Brush topBackgroundBrush, Brush topForegroundBrush, Brush bottomBackgroundBrush, Brush bottomForegroundBrush, float waveCount, float scaleY ) { Bitmap topBitmap = new Bitmap(rectangle.Width, rectangle.Height); Bitmap bottomBitmap = new Bitmap(rectangle.Width, rectangle.Height); using(StringFormat stringFormat = new StringFormat()) { stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; using(Graphics topGraphics = Graphics.FromImage(topBitmap)) { topGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; topGraphics.FillRectangle(topBackgroundBrush, rectangle); topGraphics.DrawString(text, font, topForegroundBrush, rectangle, stringFormat); } using(Graphics bottomGraphics = Graphics.FromImage(bottomBitmap)) { bottomGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; bottomGraphics.FillRectangle(bottomBackgroundBrush, rectangle); bottomGraphics.DrawString(text, font, bottomForegroundBrush, rectangle, stringFormat); } } using(TextureBrush brush = new TextureBrush(topBitmap)) { graphics.FillRectangle(brush, rectangle); } List<PointF> pointList = new List<PointF>(); float magnitude = (font.Size * 96f / 72f) / 2 * scaleY; float offsetY = rectangle.Height / 2f; pointList.Add(new PointF(0, rectangle.Height)); float scaleX = (float)(waveCount * 2 * Math.PI / rectangle.Width); for(int x = 0; x < rectangle.Width; x++) { float y = (float)(offsetY + magnitude * Math.Sin(x * scaleX)); pointList.Add(new PointF(x, y)); } pointList.Add(new PointF(rectangle.Width - 1, rectangle.Height)); using(TextureBrush brush = new TextureBrush(bottomBitmap)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.FillPolygon(brush, pointList.ToArray()); } topBitmap.Dispose(); bottomBitmap.Dispose(); } #endregion } } |