WPF下载远程文件,并显示进度条和百分比

WPF下载远程文件,并显示进度条和百分比

1、xaml

<ProgressBar HorizontalAlignment="Left" Height="10" Margin="96,104,0,0" Name="pbDown" VerticalAlignment="Top" Width="100"/>
<Label Content="Label" Name="label1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="206,104,0,0"/>

 2、CS程序

using System;
using System.Windows;
using System.Windows.Controls;
using System.Net;
using System.IO;
using System.Threading;
using System.Drawing;

namespace WpfDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            if (HttpFileExist("http://183.62.138.31:57863/opt/resources/%E9%A3%8E%E6%99%AF/f1.jpg"))
            {
                DownloadHttpFile("http://183.62.138.31:57863/opt/resources/%E9%A3%8E%E6%99%AF/f1.jpg", @"d:\f1.jpg");
            }
        }
        public void DownloadHttpFile(String http_url, String save_url)
        {
            WebResponse response = null;
            //获取远程文件
            WebRequest request = WebRequest.Create(http_url);
            response = request.GetResponse();
            if (response == null) return;
            //读远程文件的大小
            pbDown.Maximum = response.ContentLength;
            //下载远程文件
            ThreadPool.QueueUserWorkItem((obj) =>
            {
                Stream netStream = response.GetResponseStream();
                Stream fileStream = new FileStream(save_url, FileMode.Create);
                byte[] read = new byte[1024];
                long progressBarValue = 0;
                int realReadLen = netStream.Read(read, 0, read.Length);
                while (realReadLen > 0)
                {
                    fileStream.Write(read, 0, realReadLen);
                    progressBarValue += realReadLen;
                    pbDown.Dispatcher.BeginInvoke(new ProgressBarSetter(SetProgressBar), progressBarValue);
                    realReadLen = netStream.Read(read, 0, read.Length);
                }
                netStream.Close();
                fileStream.Close();

            }, null);
        }        
        /// <summary>
        ///  判断远程文件是否存在
        /// </summary>
        /// <param name="fileUrl">文件URL</param>
        /// <returns>存在-true,不存在-false</returns>
        private bool HttpFileExist(string http_file_url)
        {
            WebResponse response = null;
            bool result = false;//下载结果
            try
            {
                response = WebRequest.Create(http_file_url).GetResponse();
                result = response == null ? false : true;
            }
            catch (Exception ex)
            {
                result = false;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
            return result;
        }
        public delegate void ProgressBarSetter(double value);
        public void SetProgressBar(double value)
        {
            //显示进度条
            pbDown.Value = value;
            //显示百分比
            label1.Content = (value / pbDown.Maximum) * 100 + "%";
        }
    }
}

 

要在WPF VLC播放器中显示进度条,可以使用WPF自带的ProgressBar控件来实现。以下是在WPF VLC播放器中显示进度条的步骤: 1. 在MainWindow.xaml文件中添加ProgressBar控件: ``` <ProgressBar Name="progressBar" Value="0" Minimum="0" Maximum="100" /> ``` 2. 在MainWindow.xaml.cs文件中添加以下代码: ``` using AxAXVLC; using System; using System.Windows; using System.Windows.Threading; namespace WpfVlcPlayer { public partial class MainWindow : Window { private DispatcherTimer timer; public MainWindow() { InitializeComponent(); axVLCPlugin1.playlist.add("your_media_file_path_here"); timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(100); timer.Tick += Timer_Tick; } private void Timer_Tick(object sender, EventArgs e) { if (axVLCPlugin1.playlist.isPlaying) { double position = axVLCPlugin1.input.position * 100.0; progressBar.Value = position; } } private void btnPlay_Click(object sender, RoutedEventArgs e) { axVLCPlugin1.playlist.play(); timer.Start(); } private void btnPause_Click(object sender, RoutedEventArgs e) { axVLCPlugin1.playlist.togglePause(); if (axVLCPlugin1.playlist.isPlaying) { timer.Start(); } else { timer.Stop(); } } private void btnStop_Click(object sender, RoutedEventArgs e) { axVLCPlugin1.playlist.stop(); timer.Stop(); progressBar.Value = 0; } } } ``` 3. 在上面的代码中,我们使用了一个DispatcherTimer来定期更新进度条的值。在MainWindow构造函数中,我们创建了一个时间间隔为100毫秒的计时器,并在每个计时器间隔中更新进度条的值。 4. 我们还需要使用axVLCPlugin1.input.position属性获取当前播放位置,并将其转换为进度条的值(0到100之间的百分比)。 这样,每当你点击“播放”按钮时,进度条就会自动更新。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值