像progressbar一样加载C#WPF图像

像progressbar一样加载C#WPF图像,c#,wpf,image,performance,progress-bar,C#,Wpf,Image,Performance,Progress Bar,我有一个进度条和一个图像。 当进度条值为50时,图像加载50%。 我试图添加图像作为progressbar前景,但它有绿色阴影。太难看了 我该怎么做呢?我也有类似的情况。 如果希望滚动条只是一个矩形, 最简单的方法是: 1-将图像添加到窗口中 2-在其上放置网格,使网格隐藏图像 3-通过编程更改网格的宽度或高度 如果您需要示例代码,请告诉我。要运行此示例,您需要一个可以从中获得的snake图像。我直接使用了这个url,但你应该先下载图片,然后再使用它 您需要ProgressBar的控件模板,因为

我有一个进度条和一个图像。 当进度条值为50时,图像加载50%。 我试图添加图像作为progressbar前景,但它有绿色阴影。太难看了

我该怎么做呢?

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

1-将图像添加到窗口中

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

3-通过编程更改网格的宽度或高度


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

要运行此示例,您需要一个可以从中获得的snake图像。我直接使用了这个url,但你应该先下载图片,然后再使用它

  • 您需要ProgressBar的控件模板,因为您也想显示百分比状态
  • 否则,正常的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;
            }
        }
    }
    
    
    使用制度;
    使用System.Collections.Generic;
    使用System.Linq;
    使用系统文本;
    使用System.Threading.Tasks;
    使用System.Windows;
    使用System.Windows.Controls;
    使用System.Windows.Data;
    使用System.Windows.Documents;
    使用System.Windows.Input;
    使用System.Windows.Media;
    使用System.Windows.Media.Imaging;
    使用System.Windows.Shapes;
    使用System.Windows.Threading;
    命名空间WpfControlTemplates.\u 32794074
    {
    /// 
    ///Win32794074.xaml的交互逻辑
    /// 
    公共部分类Win32794074:窗口
    {
    公共Win32794074()
    {
    初始化组件();
    }
    调度定时器;
    私有无效BtnLoadSnake_单击(对象发送方,路由目标)
    {
    BtnLoadSnake.IsEnabled=false;
    PBarCustom.Value=PBarCustom.max;
    矩形thumb=(矩形)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.max)).ToString();
    Dispatcher disp=PBarCustom.Dispatcher;
    EventHandler pBarCallbackHandler=新的EventHandler(pBarCallback);
    计时器=新调度程序(时间跨度从秒(0.5),调度优先级正常,pBarCallback,调度);
    }
    私有void pBarCallback(对象发送方,事件参数e)
    {
    PBarCustom.Value-=13;
    矩形thumb=(矩形)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.max)).ToString();
    如果(PBarCustom.Value==0)
    timer.Stop();
    }
    私有void PBarCustom_值已更改(对象发送方,RoutedPropertyChangedEventArgs e)
    {            
    如果(e.NewValue==0)
    BtnLoadSnake.IsEnabled=true;
    }
    }
    }
    
    Well颜色完全是个人喜好的问题,我相信有人会喜欢你的groovy绿色阴影图片进度条。@Gabo我假设你想使用与进度条背景相同的图像。谢谢,但我如何计算百分比?(使用的progressbar.maxvalue约为100000)进度的最大值为100。您应该计算实际工作进度的百分比,然后在进度条上设置该值。