⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rotate.cs

📁 编辑点属性,地理信息系统开发学习使用, 学习
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace photo
{
    public partial class rotate : Form
    {
        string s;
        Form1 form;
        public rotate()
        {
            InitializeComponent();
        }
        public rotate(Form1 pform)
        {

            form = pform;
            
            InitializeComponent();
        }
        private  Bitmap picturerotate()
        {
            Bitmap bitmap = new Bitmap(form.PictureBox.Image);
            //原图像宽度和高度
            int lWidth = bitmap.Width;
            int lHeight = bitmap.Height;
            //原图四个角的坐标(以图像中心为坐标原点)
            float fSrcX1, fSrcY1, fSrcX2, fSrcY2, fSrcX3, fSrcY3, fSrcX4, fSrcY4;
            fSrcX1 = (float)(-(lWidth - 1) / 2);
            fSrcY1 = (float)((lHeight - 1) / 2);
            fSrcX2 = (float)((lWidth - 1) / 2);
            fSrcY2 = (float)((lHeight - 1) / 2);
            fSrcX3 = (float)(-(lWidth - 1) / 2);
            fSrcY3 = (float)(-(lHeight - 1) / 2);
            fSrcX4 = (float)((lWidth - 1) / 2);
            fSrcY4 = (float)(-(lHeight - 1) / 2);
          
            float fRotateAngle =(float) (Convert.ToDouble(s) * 3.14159265 / 180);
            //旋转后新图的四个角的坐标(以图像中心为坐标原点)
            float fDstX1, fDstY1, fDstX2, fDstY2, fDstX3, fDstY3, fDstX4, fDstY4;
            float fCosa, fSina;
            fCosa =(float) (Math.Cos(fRotateAngle));
            fSina =(float)( Math.Sin(fRotateAngle));

            fDstX1 = fCosa * fSrcX1 + fSina * fSrcY1;
            fDstY1 = -fSina * fSrcX1 + fCosa * fSrcY1;

            fDstX2 = fCosa * fSrcX2 + fSina * fSrcY2;
            fDstY2 = -fSina * fSrcX2 + fCosa * fSrcY2;

            fDstX3 = fCosa * fSrcX3 + fSina * fSrcY3;
            fDstY3 = -fSina * fSrcX3 + fCosa * fSrcY3; 

            fDstX4 = fCosa * fSrcX4 + fSina * fSrcY4;
            fDstY4 = -fSina * fSrcX4 + fCosa * fSrcY4;
            //新图的宽度和角度

            float lNewWidth, lNewHeight;

            float a = Math.Abs(fDstX4 - fDstX1);
            float b = Math.Abs(fDstX3 - fDstX2);
            float c = Math.Abs(fDstY4 - fDstY1);
            float d = Math.Abs(fDstY3 - fDstY2);

            lNewWidth = (float)(Math.Max(a, b) + 0.5);
            lNewHeight = (float)(Math.Max(c, d) + 0.5);

            //两个常数,之后不用再计算
            float f1, f2;
            f1 = (float)(-0.5 * (lNewWidth - 1) * fCosa - 0.5 * (lNewHeight - 1) * fSina + 0.5 * (lWidth - 1));
            f2 = (float)(0.5 * (lNewWidth - 1) * fSina - 0.5 * (lNewHeight - 1) * fCosa + 0.5 * (lHeight - 1));


            Bitmap newBitmap = new Bitmap((int)lNewWidth, (int)lNewHeight);
           
            for (int i = 0; i <(int) lNewHeight; i++)
            {
                for (int j = 0; j <(int ) lNewWidth; j++)
                {//计算该像素在原图中的坐标
                    float i0 =(float) (((float)j) * fSina + ((float)i) * fCosa + f1 + 0.5);
                    float j0 =(float) (((float)j) * fCosa + (-(float)i) * fSina + f2 + 0.5);

                    if ((j0 >= 0) && (j0 < lWidth) && (i0 > 0) && (i0 < lHeight))
                    { newBitmap.SetPixel(i, j, bitmap.GetPixel((int)i0, (int)j0)); }
                    else
                    { newBitmap.SetPixel (i,j,  Color.White); }
                }
            }
            return newBitmap;



        }

       

       
        private void button1_Click(object sender, EventArgs e)
        {s = this.textBox1.Text;
        if (s == "")
        { MessageBox.Show("请输入旋转角度"); }

        else
        {
            this.pictureBox1.Image = picturerotate();
        }

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -