简体   繁体   中英

c# - trying to play a video in a picturebox

I want to play a small video in a PictureBox . I have a folder on my desktop containing 151 frames with a .png extension. This is what I have already tried:

System.IO.DirectoryInfo di = new DirectoryInfo("c:\\Users\\" + Environment.UserName + "\\Desktop\\fireplace");

foreach (FileInfo file in di.GetFiles())
{
    pictureBox1.Image = file;
}

This doesn't work because there's an error that says:

Cannot implicitly convert type System.IO.FileInfo to System.Drawing.Image

I don't know how to make FileInfo to an Image . (btw. the fireplace folder in the code is the folder that contains the frames.)

My suggestion would be to load all of the images into one List<Bitmap> and then use Timer to change the pictures inside PictureBox :

List<Bitmap> _images = new List<Bitmap>();
int _currentImageIndex = 0;

int CurrentImageIndex
{
    get { return _currentImageIndex; }
    set {
        _currentImageIndex = value;
        if (InvokeRequired)
        {
            Invoke(new MethodInvoker( () => { _pictureBox.Image = _images[_currentImageIndex]; } );
        }
        else 
        {
            _pictureBox.Image = _images[_currentImageIndex];
        }
    }
}

Bitmap LoadImage(Stream stream)
{
    return new Bitmap(stream, false);
}

public void LoadImages(DirectoryInfo dInfo)
{
    foreach(FileInfo fInfo in dInfo.GetFiles())
    {
        if(InvokeRequired)
        {
            Invoke(new MethodInvoker( () => { _images.Add(LoadImage (fInfo.Open(FileMode.Open))); });
        }
        else
        {
            _images.Add(LoadImage (fInfo.Open()));
        }
    }
}

void WhenTimerTicks(object sender, EventArgs e)
{
    if(CurrentImageIndex < _images.Count)
        CurrentImageIndex++;
}

Now all you have to do is to read the files and after that set up your timer :

LoadImages(new DirectoryInfo("c:\\Users\\" + Environment.UserName + "\\Desktop\\fireplace"));

Timer t = new Timer();
t.Interval = 1000 / 25; // 25 FPS
t.Tick += WhenTimerTicks;
t.Start();

m.rogalski:

List<Bitmap> _images = new List<Bitmap>();
    int _currentImageIndex = 0;

    int CurrentImageIndex
    {
        get { return _currentImageIndex; }
        set
        {
            _currentImageIndex = value;
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => { pictureBox1.Image = _images[_currentImageIndex]; });
            }
            else
            {
                pictureBox1.Image = _images[_currentImageIndex];
            }
        }
    }

    Bitmap LoadImage(Stream stream)
    {
        return new Bitmap(stream, false);
    }

    public void LoadImages(DirectoryInfo dInfo)
    {
        foreach (FileInfo fInfo in dInfo.GetFiles())
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(() => { _images.Add(fInfo.Open(FileMode.Open)); })); //Argument 1: cannot convert from 'System.IO.FileStream' to 'System.Drawing.Bitmap'
            }
            else
            {
                _images.Add(fInfo.Open(FileMode.Open)); //Argument 1: cannot convert from 'System.IO.FileStream' to 'System.Drawing.Bitmap'
            }
        }
    }

    void WhenTimerTicks(object sender, EventArgs e)
    {
        if (CurrentImageIndex < _images.Count)
            CurrentImageIndex++;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM