이미지 360도 회전하기

프로젝트 2018. 2. 28. 13:16

C# 이미지 회전하는 원본 소스 : https://www.codeproject.com/Articles/58815/C-Image-PictureBox-Rotations)


C# 타이머 루프 소스 : http://code.i-harness.com/ko/q/aff378



[완성된 결과물]


 

 

수정된 소스파일 :  RotatePictureBox_2018-02-28.zip

 

 

원본소스(RotatePictureBox )에 타이머 Timer1 과 버튼 btnRotate 각각 1개씩 추가

 

[수정한 소스 :  Form1.cs ]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace RotatePictureBox
{
    public partial class Form1 : Form
    {
        private Bitmap image = null;
        private float angle = 0.0f;
        private int i=0;
        public Form1()
        {
            InitializeComponent();
            angleNumericUpDown.Value = (Decimal)angle;
        }

        //Load image
        private void LoadImageBtn_Click(object sender, EventArgs e)
        {
            lfd.InitialDirectory = Application.StartupPath;
            lfd.ShowDialog();
        }

        private void lfd_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                image = new Bitmap(lfd.FileName);

                pictureBox1.Image = (Bitmap)image.Clone();
                ImagePathTxtBox.Text = lfd.FileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            RotateImage(pictureBox1, image, angle);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    RotateImage(pictureBox1, image, angle++);
                    break;
                case Keys.Down:
                    RotateImage(pictureBox1, image, angle--);
                    break;
                case Keys.Right:
                    RotateImage(pictureBox1, image, angle++);
                    break;
                case Keys.Left:
                    RotateImage(pictureBox1, image, angle--);
                    break;
            }
        }

        private void angleNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            angle = (float)angleNumericUpDown.Value;
            RotateImage(pictureBox1, image, angle);
        }

        private void RotateImage(PictureBox pb, Image img, float angle)
        {
            if (img == null || pb.Image == null)
                return;

            Image oldImage = pb.Image;
            pb.Image = Utilities.RotateImage(img, angle);
            if (oldImage != null)
            {
                oldImage.Dispose();
            }
        }

        // 2018.02.28
        private void btnRotate_Click(object sender, EventArgs e)
        {
            i = 0;
            angleNumericUpDown.Value = i;
            CheckForIllegalCrossThreadCalls = false;
            Thread t1 = new Thread(LoopTest);
            t1.Start();
        }

        void LoopTest()
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            this.Cursor = Cursors.WaitCursor;
            while (true)
            {
                if (sw.ElapsedMilliseconds != 1000)
                {
                    i++;
                    angleNumericUpDown.Value = i;
                }
                else
                {
                    break;
                }
                if (i == 360)
                {
                    break;
                }
            }
            sw.Stop();
            this.Cursor = Cursors.Default;
            MessageBox.Show(i.ToString(), "Loop Output");
        }
    }
}

 

 

'프로젝트' 카테고리의 다른 글

VB6에서 .Net Control 사용하기  (0) 2020.07.16
엑셀 index match 함수 응용예제  (0) 2018.06.21
lazgraph  (0) 2017.12.05
vb6 인쇄시 페이지 당 row 수 계산  (0) 2017.11.24
todolist 한글패치 - 실패  (2) 2017.10.27
: