C# WPF 实现原神官网立绘图画显示效果

By jerryxjr1220 at 2023-08-25 • 0人收藏 • 402人看过

WPF对于图片的渲染效果要比WinForm出色很多,模仿原神官网立绘图画显示效果

screenshots.png

动画效果:https://note.youdao.com/s/WNX2e9RX


前端Xaml:

<Window x:Class="YuanShenBrowser.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:YuanShenBrowser"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="125"/>
        </Grid.RowDefinitions>
        <!--背景图片-->
        <Image Grid.RowSpan="2" Stretch="UniformToFill" Source="https://uploadstatic.mihoyo.com/contentweb/20200211/2020021114220951905.jpg"/>
        <!--角色立绘-->
        <Image x:Name="charPic" Grid.RowSpan="2" d:Source="https://webstatic.mihoyo.com/upload/contentweb/2022/06/30/c8a5da498d29faa1c8f2e2bfb60efbd5_7098200614657426672.png" />
        <!--角色列表-->
        <ListBox x:Name="charListBox" Grid.Row="1" VerticalAlignment="Bottom" Background="#33f0f3ff" HorizontalAlignment="Center" SelectionChanged="charListBox_SelectionChanged">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </Grid>
</Window>

后端主程序:

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Text.Json.Serialization;
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.Navigation;
using System.Windows.Shapes;

namespace YuanShenBrowser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public JToken list { get; set; }

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var client = new HttpClient();
            var req = new HttpRequestMessage(HttpMethod.Get, "https://content-static.mihoyo.com/content/ysCn/getContentList?pageSize=206&pageNum=1&order=asc&channelId=350");
            req.Headers.Add("Accept", "*/*");
            req.Headers.Add("User-Agent", "Mozilla 5.0");
            var resp = await client.SendAsync(req);
            var result = await resp.Content.ReadAsStringAsync();
            
            list = JObject.Parse(result)["data"]["list"];

            foreach (var item in list)
            {
                //var cpurl = item["ext"][1]["value"][0]["url"].ToString();
                var iconurl = item["ext"][0]["value"][0]["url"].ToString();
                var name = item["title"].ToString();
                charListBox.Items.Add(new Image { Source = new BitmapImage(new Uri(iconurl)), Name=name, Stretch = Stretch.UniformToFill, Height = 100 });

            }

            
        }

        private void charListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var img = e.AddedItems[0] as Image;
            foreach (var item in list)
            {
                if (item["title"].ToString() == img.Name)
                {
                    var cpurl = item["ext"][1]["value"][0]["url"].ToString();
                    charPic.Source = new BitmapImage(new Uri(cpurl));
                    return;
                }

            }
        }
    }
}

5 个回复 | 最后更新于 2023-08-29
2023-08-25   #1

全屏当桌面效果也非常好

无标题1.png

YuanShenBrowser.zip

大小只有145k

2023-08-26   #2

旁边可以加个切换城市列表加载更多人物立绘

无标题1.png


定制化RadioButton

<!--城市列表-->
<ListBox x:Name="cityListBox" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Background="Transparent">
    <ListBox.Resources>
        <Style TargetType="RadioButton">
            <Setter Property="FontSize" Value="24"/>
            <Setter Property="Foreground" Value="OrangeRed" />
            <Setter Property="Margin" Value="5,10,20,0" />
            <Setter Property="VerticalContentAlignment" Value="Center"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            
        </Style>
    </ListBox.Resources>
    <RadioButton Content="蒙德城" x:Name="rb150" />
    <RadioButton Content="须弥城" x:Name="rb324" />
    <RadioButton Content="稻妻城" x:Name="rb350" />
</ListBox>

定制化下方的头像列表(头像+姓名)并异步加载

private async Task Load(string id)
{
    var client = new HttpClient();
    var req = new HttpRequestMessage(HttpMethod.Get, $"https://content-static.mihoyo.com/content/ysCn/getContentList?pageSize=206&pageNum=1&order=asc&channelId={id}");
    req.Headers.Add("Accept", "*/*");
    req.Headers.Add("User-Agent", "Mozilla 5.0");
    var resp = await client.SendAsync(req);
    var result = await resp.Content.ReadAsStringAsync();

    list = JObject.Parse(result)["data"]["list"];
    charListBox.Items.Clear();
    foreach (var item in list)
    {
        var iconurl = item["ext"][0]["value"][0]["url"].ToString();
        var name = item["title"].ToString();
        var panel = new StackPanel();
        var img = new Image { Source = new BitmapImage(new Uri(iconurl)), Name = name, Stretch = Stretch.UniformToFill, Height = 100 };
        panel.Children.Add(img);
        panel.Children.Add(new Label() { Content = name, Foreground = Brushes.White, HorizontalAlignment = HorizontalAlignment.Center });
        charListBox.Items.Add(panel);
    }

    
}


2023-08-26   #3

回复#2 @jerryxjr1220 :

这个好玩⊙▽⊙,赞

2023-08-26   #4

还可以做背景变换的动画,把变化速度调慢时候当桌面时缓慢变化。

screenshots.gif

WPF应该有更优雅的动画制作语句,不过我只叠加了一张图片,然后调了调透明度

private void RunAnimation()
{
    Application.Current.Dispatcher.Invoke(() =>
    {
        // 在UI线程上访问或修改UI元素的代码
        var o = this.animationImage.Opacity;
        if (appear)
            o += 0.01;
        else
            o -= 0.01;
        if (o >= 0.98)
            appear = false;
        else if (o <= 0.02)
            appear = true;
        this.animationImage.Opacity = o;
        
    });
    
    _ = Task.Delay(20)
        .ContinueWith(t => RunAnimation());
}


2023-08-29   #5

非常好看

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...