첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요