我試圖將圖片檔案夾轉換為視頻格式,我可以播放和編輯其中的幀,或者只是在檔案夾中迭代并顯示它們。我使用 VS、C#、WPF 和 .NET 4.7.2.
。該檔案夾還有一個名為 "files.txt "的.txt檔案,其中包含了該檔案夾中所有的檔案,并逐行顯示。
示例(用行來分隔,而不是空格)-
" frame_000000.jpg frame_000001.jpg frame_000002.jpg frame_000003.jpg frame_000004.jpg frame_000005.jpg ... "
當我把這個檔案夾放到應用程式中時,它就開始運行了。
以下是我目前的情況。
using System;
using System.IO;
using System.Linq;
使用System.Windows.IO
using System.Windows.Media.Imaging;
namespace WpfApp2
{
public partial class MainWindow : Window >。
{
public static string filename { get; set; }
public static string imageName { get; set; }
public MainWindow()
{
InitializeComponent()。
}
public void UpdateImage() ?
{
ImportImage.Source = new BitmapImage(new Uri(imageName))。
}
public void FileDropStackPanel_Drop(object sender, DragEventArgs e)。
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop)。
filename = Path.GetFullPath(files[0] )。
GetFileName()。
}
}
public void GetFileName()。
{
listFilesInDirectory(filename)。
void listFilesInDirectory(string workingDirectory)。
{
string[] filePaths = Directory.GetFiles(workingDirectory)。
if (filePaths.Contains(filename @"files.txt")
{
string textFile = filename @"files.txt";
string[] lines = File.ReadAllLines(textFile)。
for (int x = 0; x < lines.Length; x )
{
imageName = filename @""/span> lines[x];
UpdateImage()。
}
}
}
}
}
}
首先,我不得不在代碼之外將所有的JPG檔案轉換為PNG。其次,它只顯示檔案夾中的最后一張圖片。
我不知道如何將JPG轉換成視頻格式或以25 FPS顯示它們。
uj5u.com熱心網友回復:
免責宣告:這里有一個解決你所問的問題的方法,是可行的。 我已經測驗了它。 然而,它并不理想,我也不推薦它,因為這段代碼取決于你的系統能夠執行異步背景關系切換的速度。
- 這段代碼適用于 JPG、PNG 和其他一些格式。我不清楚為什么你需要將所有的 JPG 檔案轉換為 PNG。
- 你的代碼之所以只顯示檔案夾中的最后一張圖片,是因為你在UI執行緒上同步運行所有代碼,所以在你的整個 "for "回圈完成之前,它無法更新UI。你需要通過使用async/await模式來分割UI執行緒。如果你對這種編碼方式不熟悉,可以去查一下。
代碼:
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace _69201085
{
// <summary>
// MainWindow.xaml的互動邏輯。
// </summary>/span>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent()。
}
public void UpdateImage(string imageName)
{
ImportImage.Source = new BitmapImage(new Uri(imageName))。
}
title">ShowFilesInFolder(string workingDirectory)
{
var textFile = Path.Combine(workingDirectory, files.txt")。
if (File.Exists(textFile))
{
//決議該檔案,然后以25FPS的速度遍歷所有列出的影像。
var lines = await File.ReadAllLinesAsync(textFile)。
foreach (var line in lines)
{
var imageName = Path.Combine(workingDirectory, line.Trim() )。
//在你嘗試改變影像之前檢查檔案是否存在。
if (File.Exists(imageName))
{
//確保該檔案是被批準的影像格式之一。
if (
Path.GetExtension(imageName).ToLower() == ".jpg"/span> ||
Path.GetExtension(imageName).ToLower() == ".png"/span> ||
Path.GetExtension(imageName).ToLower() == ".jpeg" <|
Path.GetExtension(imageName).ToLower() == ".jfif" <|
Path.GetExtension(imageName).ToLower() == ".gif" <|
Path.GetExtension(imageName).ToLower() == ".bmp" !
)
{
//更新影像。
UpdateImage(imageName)。
//如果你想要25 FPS,那么請等待40 ms。
await Task.Delay(40)。
}
}
}
}
}
private async void Window_Drop(object sender, DragEventArgs e)。
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop)。
string folder = Path.GetFullPath(files[0]) 。
if (! Directory.Exists(folder))
{
folder = Path.GetDirectoryName(files[0] )。)
}
//驗證該目錄/檔案夾是否存在。
if (Directory.Exists(folder))
{
await ShowFilesInFolder(folder)。
}
}
}
}
}
而xaml:
<Window x:Class="_69201085.MainWindow"/span>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"/span>
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"/span>
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"/span>
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"。
mc:Ignorable="d"。
Title="主視窗" Height="450" Width="800" Drop="Window_Drop" AllowDrop="True">
<Grid>
<StackPanel x:Name="FileDropStackPanel">
<Image x:Name="ImportImage" Stretch="Fill" />
</StackPanel>
</Grid>
</Window>
最后,代碼假設你的 "files.txt "中沒有列出目錄名,只有檔案名。像這樣:
file1.jpg
file2.PNG
file3.bmp
file4.jpg
...
uj5u.com熱心網友回復:
你可以使用ffmpeg和這個超級用戶帖子中描述的技術來把你的檔案幀變成一個電影
。ffmpeg -framerate 25 -i frame_%08d.jpg out.mp4
如果你是在Windows上,你可能要把%符號加倍
。轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/324498.html
標籤:
