■ 비트맵을 확대하는 방법을 보여준다.
▶ 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 |
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> /// 월드 너비 /// </summary> private const int WORLD_WIDTH = 100; /// <summary> /// 월드 높이 /// </summary> private const int WORLD_HEIGHT = 100; /// <summary> /// 비트맵 /// </summary> private Bitmap bitmap = null; /// <summary> /// 비트맵 스케일 /// </summary> private float bitmapScale = 1.0f; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 이벤트를 설정한다. this.Load += Form_Load; this.scaleComboBox.SelectedIndexChanged += scaleComboBox_SelectedIndexChanged; #endregion } #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) { this.scaleComboBox.SelectedIndex = 2; } #endregion #region 스케일 콤보 박스 선택 인덱스 변경시 처리하기 - scaleComboBox_SelectedIndexChanged(sender, e) /// <summary> /// 스케일 콤보 박스 선택 인덱스 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scaleComboBox_SelectedIndexChanged(object sender, EventArgs e) { switch(this.scaleComboBox.SelectedIndex) { case 0 : SetScale(0.25f); break; case 1 : SetScale(0.5f ); break; case 2 : SetScale(1f ); break; case 3 : SetScale(2f ); break; case 4 : SetScale(4f ); break; case 5 : SetScale(8f ); break; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 스케일 설정하기 - SetScale(bitmapScale) /// <summary> /// 스케일 설정하기 /// </summary> /// <param name="bitmapScale">비트맵 스케일</param> private void SetScale(float bitmapScale) { this.bitmapScale = bitmapScale; this.bitmap = new Bitmap ( (int)(this.bitmapScale * WORLD_WIDTH ), (int)(this.bitmapScale * WORLD_HEIGHT) ); using(Graphics graphics = Graphics.FromImage(this.bitmap)) { graphics.Clear(Color.White); graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.ScaleTransform(this.bitmapScale, this.bitmapScale); DrawImage(graphics); } this.canvasPictureBox.Image = this.bitmap; } #endregion #region 이미지 그리기 - DrawImage(graphics) /// <summary> /// 이미지 그리기 /// </summary> /// <param name="graphics">그래픽스</param> private void DrawImage(Graphics graphics) { Rectangle rectangle; rectangle = new Rectangle(10, 10, 80, 80); graphics.FillEllipse(Brushes.LightGreen, rectangle); graphics.DrawEllipse(Pens.Green, rectangle); rectangle = new Rectangle(40, 40, 20, 30); graphics.FillEllipse(Brushes.LightBlue, rectangle); graphics.DrawEllipse(Pens.Blue, rectangle); rectangle = new Rectangle(25, 30, 50, 50); graphics.DrawArc(Pens.Red, rectangle, 20, 140); rectangle = new Rectangle(25, 25, 15, 20); graphics.FillEllipse(Brushes.White, rectangle); graphics.DrawEllipse(Pens.Black, rectangle); rectangle = new Rectangle(30, 30, 10, 10); graphics.FillEllipse(Brushes.Black, rectangle); rectangle = new Rectangle(60, 25, 15, 20); graphics.FillEllipse(Brushes.White, rectangle); graphics.DrawEllipse(Pens.Black, rectangle); rectangle = new Rectangle(65, 30, 10, 10); graphics.FillEllipse(Brushes.Black, rectangle); } #endregion } } |