我正在用C#制作一个声音样本播放器,并将样本本身的图片加载到PictureBox中。用户按下“播放”按钮,我现在希望在示例播放时在图片上滚动一条垂直线。
我想使用Winforms,但是对此(以及C#图形和动画)还比较陌生,所以想知道最好的方法。
一些声音样本图片使用相当大的分辨率,因此线动画技术需要有效以确保线平滑滚动。
一般的想法都会受到欢迎,示例代码片段也将为我指明正确的方向。
编辑:在Jimi的评论之后,我在下面添加了一些示例代码。为简洁起见,未显示项目的某些方面(例如播放声音样本),因为在此阶段,我仅关注垂直线滚动在样本图片上的可能优化,这需要尽可能快/顺畅
public partial class frmPlaySample : Form
{
// We have set up a form in designer mode with a Play sample button (btnPlay), a picturebox (pictureBox1) and a Timer (timer1)
public frmPlaySample()
{
InitializeComponent();
}
private void frmPlaySample_Load(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(@"C:\SamplePicture.jpg");
timer1.Interval = 17; // Timer Interval is set to 17 to approximate 60 frames per second.
}
int line = 0;
private void btnPlay_Click(object sender, EventArgs e)
{
line=0;
timer1.Enabled=true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen(Brushes.White), line,0,line,pictureBox1.Height);
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Invalidate(); // If we wanted to, we could invalidate a narrow rectangle, but that would make things only slightly faster.
line+=8; // This can be any amount. 8 is chosen arbitrarily.
if(line>= pictureBox1.Width) { timer1.Enabled=false; line=0; }
}
}