使图片不断旋转的方法

在做项目时,我们有时会想要将一张圆形图片在界面上不断旋转,例如制作音乐播放器时想要有光碟缓慢旋转的效果,那么如何使一张圆形图片进行不断旋转呢?话不多说,看下面

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

namespace Test3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Bitmap bmp;
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();//打开文件对话框选择一张圆形图片
            ofd.ShowDialog();
            Image image = Image.FromFile(ofd.FileName);
            bmp = new Bitmap(image);//bmp即为我们的原型图片
            picPicture.BackgroundImage = bmp;//将图片作为picturebox背景,方便展示
        }

        //使图片旋转的方法
        public static Bitmap picRotate(Bitmap bmp,int angle)
        {
            Bitmap reBmp = new Bitmap(bmp.Width,bmp.Height);
            Graphics g = Graphics.FromImage(reBmp);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//选择模式
            g.TranslateTransform((float)bmp.Width/2,(float)bmp.Height/2); //参数为旋转中心
            g.RotateTransform(angle);//设置旋转角度
            g.TranslateTransform(-(float)bmp.Width/2,-(float)bmp.Height/2);
            g.DrawImage(bmp,new Point(0,0));
            return reBmp;//返回旋转后的图片
        }

        private void btnRotate_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        int angle = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            picPicture.BackgroundImage = picRotate(bmp,angle);//将旋转后的图片设置为picturebox背景
            angle += 5;//每次增加5度
            if(angle>=360)
            {
                angle = 0;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/h2503652646/article/details/83903268
今日推荐