C# DrawImage 绘制图片实现音乐唱碟不停360度顺时针旋转或者逆时针
旋转
以下是使用C# DrawImage绘制图片实现音乐唱碟不停360度顺时针旋转或者逆时针旋转的示例代码:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MusicPlayer
{
public partial class Form1 : Form
{
private Image discImage;
private Timer timer;
private int rotationAngle;
public Form1()
{
InitializeComponent();
// 加载唱碟图片
discImage = Image.FromFile("disc.png");
// 创建计时器,每20毫秒更新一次唱碟旋转角度
timer = new Timer();
timer.Interval = 20;
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
// 更新唱碟旋转角度
rotationAngle += 5;
if (rotationAngle >= 360)
{
rotationAngle = 0;
}
// 重绘窗口
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 获取窗口大小
int width = ClientSize.Width;
int height = ClientSize.Height;
// 计算唱碟中心位置
int discCenterX = width / 2;
int discCenterY = height / 2;
// 计算唱碟半径
int discRadius = Math.Min(width, height) / 2 - 20;
// 创建唱碟旋转矩阵
Matrix matrix = new Matrix();
matrix.Translate(discCenterX, discCenterY);
matrix.Rotate(rotationAngle);
matrix.Translate(-discCenterX, -discCenterY);
// 应用唱碟旋转矩阵
e.Graphics.Transform = matrix;
// 绘制唱碟
e.Graphics.DrawImage(discImage, discCenterX - discRadius, discCenterY - discRadius, discRadius * 2, discRadius * 2);
}
}
}
该示例程序会在窗口中绘制一张唱碟图片,并且每隔20毫秒更新一次唱碟旋转角度,实现唱碟不停360度顺时针旋转。如果想要逆时针旋转,只需要将 rotationAngle 的增量改为负数即可
原文地址: https://www.cveoy.top/t/topic/fRDs 著作权归作者所有。请勿转载和采集!