C# winform帧动画

版权声明:以上纯属个人独自研究成果,仅供参考,转载请注明出处 https://blog.csdn.net/u012847695/article/details/79567750

字符动画个人觉得更具挑战性,可惜没那么多时间和精力来独自研究。

本文示例 利用系统线程实现将图片轮播配上音乐还原视屏原效果。

功能包括暂停播放,继续播放.

另外本示例以 Bad Apple为例。将视屏每帧转成图片,网上工具很多,可以网上搜索一下。

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

namespace WindowsFormsApplication2
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        [System.Runtime.InteropServices.DllImport("winmm.dll")]
        public static extern uint mciSendString(string lpstrCommand, string lpstrReturnString, uint uReturnLength, uint hWndCallback);

        private volatile bool isStop;
        private int i = 1;
        private Thread t = null;
        private string pathstr = System.Windows.Forms.Application.StartupPath + @"/BadApple_file";

        private void button1_Click(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            
            string bts = button1.Text;
            if (bts == "开始播放")
            {
                mciSendString(string.Format(@"open ""{0}/0.mp3"" alias temp_alias", pathstr), null, 0, 0);
                mciSendString("play temp_alias", null, 0, 0);
                Start();
                button1.Text = "暂停播放";
            }
            else
            {
                mciSendString("Pause temp_alias", null, 0, 0);
                Stop();
                button1.Text = "开始播放";
            }
        }

        public void Start()
        {
            isStop = false;
            t = new Thread(new ThreadStart(Threads));
            t.Start();
        }

        public void Stop()
        {
            isStop = true;

            if (t != null && t.ThreadState == ThreadState.Running)
                t.Abort();
            t = null;
        }

        private void Threads()
        {
            while (!isStop)
            {
                string path = pathstr + "/_" + i++ + ".jpg";
                Bitmap bitmap = new Bitmap(path);
                pictureBox1.Image = bitmap;
                label1.Text = path;
                if (i == 6575)
                {
                    mciSendString(@"close temp_alias", null, 0, 0);
                    button1.Text = "开始播放";
                    i = 1;
                    break;
                }
                Thread.Sleep(29);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Stop();
            mciSendString(@"close temp_alias", null, 0, 0);
        }

    }
}

源码下载

以上纯属个人独自研究成果,仅供参考,转载请注明出处

猜你喜欢

转载自blog.csdn.net/u012847695/article/details/79567750