我想在PictureBox
中播放一个小视频。我的桌面上有一个文件夹,包含151个扩展名为.png的框架。这是我已经尝试过的:
System.IO.DirectoryInfo di = new DirectoryInfo("c:\\Users\\" + Environment.UserName + "\\Desktop\\fireplace");
foreach (FileInfo file in di.GetFiles())
{
pictureBox1.Image = file;
}
这不起作用,因为有一个错误说:
无法隐式将
System.IO.FileInfo
类型转换为System.Drawing.Image
我不知道如何FileInfo
成为Image
。
(顺便说一下,代码中的壁炉文件夹是包含框架的文件夹。)
答案 0 :(得分:1)
我的建议是将所有图片加载到一个List<Bitmap>
中,然后使用Timer
更改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++;
}
现在你所要做的就是阅读文件,然后设置你的计时器:
LoadImages(new DirectoryInfo("c:\\Users\\" + Environment.UserName + "\\Desktop\\fireplace"));
Timer t = new Timer();
t.Interval = 1000 / 25; // 25 FPS
t.Tick += WhenTimerTicks;
t.Start();
答案 1 :(得分:0)
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++;
}