■ Bitmap 클래스를 사용해 비트맵 크기를 변경하는 방법을 보여준다.
▶ 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 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 원본 비트맵 /// </summary> private Bitmap originalBitmap = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.openMenuItem.Click += openMenuItem_Click; this.saveMenuItem.Click += saveMenuItem_Click; this.scaleTextBox.TextChanged += scaleTextBox_TextChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region Open 메뉴 항목 클릭시 처리하기 - openMenuItem_Click(sender, e) /// <summary> /// Open 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openMenuItem_Click(object sender, EventArgs e) { if(this.openFileDialog.ShowDialog() == DialogResult.OK) { this.originalBitmap = LoadBitmapUnlocked(this.openFileDialog.FileName); ShowImage(); } } #endregion #region Save 메뉴 항목 클릭시 처리하기 - saveMenuItem_Click(sender, e) /// <summary> /// Save 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveMenuItem_Click(object sender, EventArgs e) { if(this.saveFileDialog.ShowDialog() == DialogResult.OK) { if(!this.saveFileDialog.FileName.ToLower().EndsWith(".png")) { DialogResult result = MessageBox.Show ( "Transparency will be lost unless you save with the PNG format. Do you want to save Anyway?", "Save Anyway?", MessageBoxButtons.YesNo ); if(result == DialogResult.No) { return; } } SaveImage(this.pictureBox.Image, this.saveFileDialog.FileName); } } #endregion #region Scale 텍스트 박스 텍스트 변경시 처리하기 - scaleTextBox_TextChanged(sender, e) /// <summary> /// Scale 텍스트 박스 텍스트 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scaleTextBox_TextChanged(object sender, EventArgs e) { ShowImage(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 잠금없이 비트맵 로드하기 - LoadBitmapUnlocked(filePame) /// <summary> /// 잠금없이 비트맵 로드하기 /// </summary> /// <param name="filePame">파일 경로</param> /// <returns>비트맵</returns> private Bitmap LoadBitmapUnlocked(string filePame) { using(Bitmap bitmap = new Bitmap(filePame)) { return new Bitmap(bitmap); } } #endregion #region 이미지 표시하기 - ShowImage() /// <summary> /// 이미지 표시하기 /// </summary> private void ShowImage() { this.sizeLabel.Text = "Size : "; this.pictureBox.Visible = false; if(this.originalBitmap == null) { return; } float scale; if(!float.TryParse(this.scaleTextBox.Text, out scale)) { return; } if(scale < 0.0001) { return; } int width = (int)(this.originalBitmap.Width * scale); int height = (int)(this.originalBitmap.Height * scale); this.sizeLabel.Text = string.Format("Size : {0} x {1}", width, height); Bitmap bitmap = new Bitmap(width, height); using(Graphics graphics = Graphics.FromImage(bitmap)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; Rectangle targetRectangle = new Rectangle(0, 0, width, height); Rectangle sourceRectangle = new Rectangle(0, 0, this.originalBitmap.Width, this.originalBitmap.Height); graphics.DrawImage(this.originalBitmap, targetRectangle, sourceRectangle, GraphicsUnit.Pixel); } this.pictureBox.Image = bitmap; this.pictureBox.Visible = true; } #endregion #region 이미지 저장하기 - SaveImage(image, filePath) /// <summary> /// 이미지 저장하기 /// </summary> /// <param name="image">이미지</param> /// <param name="filePath">파일 경로</param> public void SaveImage(Image image, string filePath) { string fileExtension = Path.GetExtension(filePath); switch(fileExtension.ToLower()) { case ".bmp" : image.Save(filePath, ImageFormat.Bmp ); break; case ".exif" : image.Save(filePath, ImageFormat.Exif); break; case ".gif" : image.Save(filePath, ImageFormat.Gif ); break; case ".jpg" : case ".jpeg" : image.Save(filePath, ImageFormat.Jpeg); break; case ".png" : image.Save(filePath, ImageFormat.Png ); break; case ".tif" : case ".tiff" : image.Save(filePath, ImageFormat.Tiff); break; default : throw new NotSupportedException("Unknown file extension " + fileExtension); } } #endregion } } |