■ SVG 이미지를 사용하는 방법을 보여준다.
▶ SVGParser.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 |
using System.Drawing; using Svg; namespace TestProject { /// <summary> /// SVG 파서 /// </summary> public class SVGParser { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 최대 크기 - MaximumSize /// <summary> /// 최대 크기 /// </summary> public static Size MaximumSize { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region SVG 문서 구하기 - GetSVGDocument(filePath) /// <summary> /// SVG 문서 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>SVG 문서</returns> public static SvgDocument GetSVGDocument(string filePath) { SvgDocument document = SvgDocument.Open(filePath); return AdjustSize(document); } #endregion #region 비트맵 구하기 - GetBitmap(filePath) /// <summary> /// 비트맵 구하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <returns>비트맵</returns> public static Bitmap GetBitmap(string filePath) { SvgDocument document = GetSVGDocument(filePath); Bitmap bitmap = document.Draw(); return bitmap; } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 크기 조정하기 - AdjustSize(document) /// <summary> /// 크기 조정하기 /// </summary> /// <param name="document">SVG 문서</param> /// <returns>SVG 문서</returns> private static SvgDocument AdjustSize(SvgDocument document) { if(document.Height > MaximumSize.Height) { document.Width = (int)((document.Width / (double)document.Height) * MaximumSize.Height); document.Height = MaximumSize.Height; } return document; } #endregion } } |
▶ 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
using System; using System.Drawing; using System.Windows.Forms; using Svg; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 파일 경로 /// </summary> private string filePath; /// <summary> /// SVG 문서 /// </summary> private SvgDocument document; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.browseButton.Click += browseButton_Click; this.sourceColorButton.Click += sourceColorButton_Click; this.targetColorButton.Click += targetColorButton_Click; this.pickColorCheckBox.CheckedChanged += pickColorCheckBox_CheckedChanged; this.replaceColorButton.Click += replaceColorButton_Click; this.scaleUpButton.Click += scaleUpButton_Click; this.scaleDownButton.Click += scaleDownButton_Click; this.pictureBox.MouseDown += pictureBox_MouseDown; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 파일 선택 버튼 클릭시 처리하기 - browseButton_Click(sender, e) /// <summary> /// 파일 선택 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void browseButton_Click(object sender, EventArgs e) { DialogResult result = openFileDialog.ShowDialog(); if(result == DialogResult.OK) { SVGParser.MaximumSize = new Size(this.pictureBox.Width, this.pictureBox.Height); this.filePath = this.openFileDialog.FileName; this.svgFilePathTextBox.Text = this.filePath; this.document = SVGParser.GetSVGDocument(this.filePath); this.widthTextBox.Text = this.document.Width.Value.ToString(); this.heightTextBox.Text = this.document.Height.Value.ToString(); this.pictureBox.Image = SVGParser.GetBitmap(this.filePath); } } #endregion #region 소스 색상 버튼 클릭시 처리하기 - sourceColorButton_Click(sender, e) /// <summary> /// 소스 색상 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void sourceColorButton_Click(object sender, EventArgs e) { DialogResult result = colorDialog.ShowDialog(); if(result == DialogResult.OK) { this.sourceColorButton.BackColor = this.colorDialog.Color; } } #endregion #region 색상 선택 체크 박스 체크 변경시 처리하기 - pickColorCheckBox_CheckedChanged(sender, e) /// <summary> /// 색상 선택 체크 박스 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pickColorCheckBox_CheckedChanged(object sender, EventArgs e) { if(this.pickColorCheckBox.Checked) { this.pickColorCheckBox.BackColor = Color.LightPink; this.pictureBox.Cursor = Cursors.Cross; } else { this.pickColorCheckBox.BackColor = Color.Gainsboro; this.pictureBox.Cursor = Cursors.Default; } } #endregion #region 타겟 색상 버튼 클릭시 처리하기 - targetColorButton_Click(sender, e) /// <summary> /// 타겟 색상 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void targetColorButton_Click(object sender, EventArgs e) { DialogResult result = colorDialog.ShowDialog(); if(result == DialogResult.OK) { this.targetColorButton.BackColor = this.colorDialog.Color; } } #endregion #region 색상 대체 버튼 클릭시 처리하기 - replaceColorButton_Click(sender, e) /// <summary> /// 색상 대체 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void replaceColorButton_Click(object sender, EventArgs e) { if(!ValidateData()) { return; } foreach(SvgElement element in this.document.Children) { ChangeColor(element, this.sourceColorButton.BackColor, this.targetColorButton.BackColor); } this.pictureBox.Image = this.document.Draw(); } #endregion #region 픽처 박스 마우스 DOWN 처리하기 - pictureBox_MouseDown(sender, e) /// <summary> /// 픽처 박스 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseDown(object sender, MouseEventArgs e) { if(this.pickColorCheckBox.Checked) { if(!ValidateData()) { return; } if(e.Button == MouseButtons.Left) { Bitmap bitmap = this.pictureBox.Image as Bitmap; this.sourceColorButton.BackColor = bitmap.GetPixel(e.X, e.Y); } } } #endregion #region 크기 감소 버튼 클릭시 처리하기 - scaleDownButton_Click(sender, e) /// <summary> /// 크기 감소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scaleDownButton_Click(object sender, EventArgs e) { if(!ValidateData()) { return; } int width = int.Parse(this.widthTextBox.Text ); int height = int.Parse(this.heightTextBox.Text); if(width - 10 > 0 && height- 10 > 0) { width -= 10; this.widthTextBox.Text = width.ToString(); height -= 10; this.heightTextBox.Text = height.ToString(); this.document.Width = width; this.document.Height = height; this.pictureBox.Image = this.document.Draw(); } } #endregion #region 크기 증가 버튼 클릭시 처리하기 - scaleUpButton_Click(sender, e) /// <summary> /// 크기 증가 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scaleUpButton_Click(object sender, EventArgs e) { if(!ValidateData()) { return; } int width = int.Parse(this.widthTextBox.Text ); int height = int.Parse(this.heightTextBox.Text); if(width + 10 < this.pictureBox.Width && height + 10 < this.pictureBox.Height) { width += 10; this.widthTextBox.Text = width.ToString(); height += 10; this.heightTextBox.Text = height.ToString(); this.document.Width = width; this.document.Height = height; this.pictureBox.Image = this.document.Draw(); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 데이터 검증하기 - ValidateData() /// <summary> /// 데이터 검증하기 /// </summary> /// <returns>데이터 검증 결과</returns> private bool ValidateData() { if(this.document == null || pictureBox.Image == null) { MessageBox.Show("SVG 이미지 파일을 선택해 주시기 바랍니다."); return false; } return true; } #endregion #region 색상 변경하기 - ChangeColor(element, sourceColor, targetColor) /// <summary> /// 색상 변경하기 /// </summary> /// <param name="element">SVG 요소</param> /// <param name="sourceColor">소스 색상</param> /// <param name="targetColor">타겟 색상</param> private void ChangeColor(SvgElement element, Color sourceColor, Color targetColor) { if(element is SvgPath) { if(((element as SvgPath).Fill as SvgColourServer).Colour.ToArgb() == sourceColor.ToArgb()) { (element as SvgPath).Fill = new SvgColourServer(targetColor); } } if(element.Children.Count > 0) { foreach(SvgElement child in element.Children) { ChangeColor(child, sourceColor, targetColor); } } } #endregion } } |