퇴근5분전

 

 

훈스 질답게시판에 이미지 리사이징 관련 글이 올라와서

 

30분 정도 짜보았음.  

 

 

 

 

 

콤보1, 버튼2, 체크박스1

 

판넬1,

 

픽쳐박스1 ( 이미지 리사이징 레이어 )

 

 

 

 

소스

 

public partial class Form1 : Form
    {
        float oldSizing = 100f;
        float[] factors = new float[] { 20, 50, 100, 120, 240 };

        public Form1()
        {
            InitializeComponent();
            comboBox1.DataSource = factors;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 2;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Resizing(comboBox1.SelectedIndex);
        }

        private void Resizing(int idx)
        {
            float CurrentSizing = factors[idx];
            pictureBox1.Scale(new SizeF(CurrentSizing / oldSizing, CurrentSizing / oldSizing));
            oldSizing = CurrentSizing;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //+
            ZoomIn();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //-
            ZoomOut();
        }

        void ZoomIn()
        {
            if (checkBox1.Checked)
            {
                Point Befor = GetCenter(pictureBox1);

                int idx = comboBox1.SelectedIndex;
                if (factors.Length > comboBox1.SelectedIndex + 1)
                    comboBox1.SelectedIndex++;

                Point after = GetCenter(pictureBox1);
                pictureBox1.Left = pictureBox1.Left - (after.X - Befor.X);
                pictureBox1.Top = pictureBox1.Top - (after.Y - Befor.Y);
            }
            else
            {
                int idx = comboBox1.SelectedIndex;
                if (factors.Length > comboBox1.SelectedIndex + 1)
                    comboBox1.SelectedIndex++;
            }
        }

        void ZoomOut()
        {
            if (checkBox1.Checked)
            {
                Point Befor = GetCenter(pictureBox1);

                int idx = comboBox1.SelectedIndex;
                if (0 <= comboBox1.SelectedIndex - 1)
                    comboBox1.SelectedIndex--;

                Point after = GetCenter(pictureBox1);
                pictureBox1.Left = pictureBox1.Left + (Befor.X - after.X);
                pictureBox1.Top = pictureBox1.Top + (Befor.Y - after.Y);
            }
            else
            {
                int idx = comboBox1.SelectedIndex;
                if (0 <= comboBox1.SelectedIndex - 1)
                    comboBox1.SelectedIndex--;
            }
        }
       
        Point GetCenter(Control c)
        {
            Point p = c.Location;
            p.X += (int)((float)c.Width / 2f);
            p.Y += (int)((float)c.Height / 2f);
            return p;
        }
    }

 

'# 4) .Net ( Vs 2010 ) > C#' 카테고리의 다른 글

WinForm] 단축키  (0) 2012.10.11
GDI+] 이미지 회전!  (0) 2012.08.13
.Net] 버튼 Pressed Event!!  (0) 2012.05.09
.NET ] 멀티 랭귀지 지원 ...  (0) 2012.03.06
ArrayList.Sort 하기...  (4) 2011.07.29