【问题标题】:C# WPF image load like progressbarC# WPF 图像加载类似进度条
【发布时间】:2015-12-23 23:43:05
【问题描述】:

我有一个进度条和一个图像。 当进度条值为 50 时,图像加载 50%。 我尝试将图像添加为进度条前景,但它有绿色阴影。好丑。

我该怎么做?

【问题讨论】:

  • 嗯,颜色完全是个人喜好问题,我敢肯定有人会喜欢你的 groovy green-shaded-picture-progress bar
  • @GaboO 我假设您想使用正在加载的图像与进度条背景中使用的图像相同。

标签: c# wpf image performance progress-bar


【解决方案1】:

我有类似的情况。 如果您希望滚动条只是一个矩形, 最简单的制作方法:

1- 将图像添加到您的窗口。

2- 在其上放置一个网格,使网格隐藏图像。

3- 以编程方式更改网格的宽度或高度。

如果您需要示例代码,请告诉我。

【讨论】:

  • 谢谢,但我如何计算百分比? (使用progressbar.maxvalue约为100000)
  • 进度的最大值是 100。您应该以百分比计算实际工作进度的数量,然后在进度条上设置该值。
【解决方案2】:

要运行此示例,您需要一个可以从http://res.freestockphotos.biz/pictures/16/16242-illustration-of-a-green-snake-pv.png 获取的蛇图像。这个网址我是直接用的,不过你得先下载图片再用。

  1. 您需要一个用于 ProgressBar 的控件模板,因为您还想显示百分比状态。
  2. 否则正常的 ProgressBar 就可以了。

代码可以按原样使用:

<Window x:Class="WpfControlTemplates._32794074.Win32794074"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Win32794074" Height="600" Width="1000">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="397*"/>
            <RowDefinition Height="173*"/>
        </Grid.RowDefinitions>
        <ProgressBar x:Name="PBarCustom" Width="958" Height="200"  Maximum="958"  Value="958" Foreground="#FFE6E61F" Margin="17,185,17,11.932" ValueChanged="PBarCustom_ValueChanged">
            <ProgressBar.Background>
                <ImageBrush ImageSource="http://res.freestockphotos.biz/pictures/16/16242-illustration-of-a-green-snake-pv.png"/>
            </ProgressBar.Background>
            <ProgressBar.Template>
                <ControlTemplate>
                    <Grid Background="{TemplateBinding Background}">
                        <Rectangle x:Name="Thumb" HorizontalAlignment="Left" Fill="#FFC5EA1F" Stroke="#FF0DB442" Width="{TemplateBinding Width}" />

                        <Ellipse Fill="#FF7DEEDE" Height="124"  Stroke="#FF0DB442" Width="150" VerticalAlignment="Center" HorizontalAlignment="Center" Opacity="0.3"/>
                        <Label x:Name="tbStatus" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontWeight="Bold" FontSize="75" Foreground="#FF21BD76" Content="0"  />

                    </Grid>
                </ControlTemplate>
            </ProgressBar.Template>

        </ProgressBar>
        <Button x:Name="BtnLoadSnake" Content="Load Snake" HorizontalAlignment="Left" Margin="462,14.068,0,0" VerticalAlignment="Top" Width="75" Click="BtnLoadSnake_Click" Grid.Row="1"/>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfControlTemplates._32794074
{
    /// <summary>
    /// Interaction logic for Win32794074.xaml
    /// </summary>
    public partial class Win32794074 : Window
    {
        public Win32794074()
        {
            InitializeComponent();

        }

        DispatcherTimer timer;
        private void BtnLoadSnake_Click(object sender, RoutedEventArgs e)
        {
            BtnLoadSnake.IsEnabled = false;
            PBarCustom.Value = PBarCustom.Maximum;
            Rectangle thumb = (Rectangle)PBarCustom.Template.FindName("Thumb", PBarCustom);
            thumb.Width = PBarCustom.Value;

            Label status = (Label)PBarCustom.Template.FindName("tbStatus", PBarCustom);
            status.Content = ((int)(100 - ((100 * PBarCustom.Value) / PBarCustom.Maximum))).ToString();

            Dispatcher disp = PBarCustom.Dispatcher;

            EventHandler pBarCallbackHandler = new EventHandler(pBarCallback);

            timer = new DispatcherTimer(TimeSpan.FromSeconds(0.5), DispatcherPriority.Normal, pBarCallback, disp);            

        }

        private void pBarCallback(object sender, EventArgs e)
        {
            PBarCustom.Value -= 13;
            Rectangle thumb = (Rectangle)PBarCustom.Template.FindName("Thumb", PBarCustom);
            thumb.Width = PBarCustom.Value;

            Label status = (Label)PBarCustom.Template.FindName("tbStatus", PBarCustom);
            status.Content = ((int)(100 - ((100 * PBarCustom.Value) / PBarCustom.Maximum))).ToString();

            if (PBarCustom.Value == 0)
                timer.Stop();
        }

        private void PBarCustom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {            
            if(e.NewValue == 0)
                BtnLoadSnake.IsEnabled = true;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多