C# загрузить .png изображение в элемент изображения - PullRequest
0 голосов
/ 16 апреля 2020

Это мой первый вопрос на форуме, я не читал никаких правил или других, поэтому извините меня, если я совершу какую-то ошибку. (например, плохой пост или пост, где кто-то другой уже задавал этот вопрос)

У меня странная проблема в моем проекте WPF c#. У меня в голове что-то действительно странное, но я думаю, что смогу это сделать. Я создаю игру в шахматы онлайн (только для пользователей в той же локальной сети), но не только своими собственными способностями, но и копирую что-то, надеясь, что это поможет мне быть быстрее, потому что я не настолько опытен с c#. Я уже создал шахматную доску, но я должен добавить фигуры на соответствующем квадрате. У меня есть папка со всем .png изображением пешки, коня, слона e cc ... Если вы читаете в кодовых словах, как "torrenero", "torrebianco", "renero", это потому, что я итальянец, и я называю изображение в таким образом, чтобы запомнить, как я их назвал

Вот мой код XAML:

<Window x:Class="WpfApp1.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:WpfApp1"
    mc:Ignorable="d"
    Title="HTTPChess.com" Height="842.506" Width="1215">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />

    </Grid.RowDefinitions>

    <Border Grid.Column="0" Grid.Row="0" Background="White" />
    <Image x:Name="imgTorreNero"></Image>
    <Border Grid.Column="1" Grid.Row="0" Background="Black" />
    <Image x:Name="imgCavalloNero"></Image>
    <Border Grid.Column="2" Grid.Row="0" Background="White" />
    <Image x:Name="imgAlfiereNero"></Image>
    <Border Grid.Column="3" Grid.Row="0" Background="Black" />
    <Image x:Name="imgReginaNero"></Image>
    <Border Grid.Column="4" Grid.Row="0" Background="White" />
    <Image x:Name="imgReNero"></Image>
    <Border Grid.Column="5" Grid.Row="0" Background="Black" />
    <Image x:Name="imgAlfiere2Nero"></Image>
    <Border Grid.Column="6" Grid.Row="0" Background="White" />
    <Image x:Name="imgCavallo2Nero"></Image>
    <Border Grid.Column="7" Grid.Row="0" Background="Black" />
    <Image x:Name="imgTorre2Nero"></Image>
    <Border Grid.Column="0" Grid.Row="1" Background="Black" />
    <Image x:Name="imgPedoneNero"></Image>
    <Border Grid.Column="1" Grid.Row="1" Background="White" />
    <Image x:Name="imgPedone2Nero"></Image>
    <Border Grid.Column="2" Grid.Row="1" Background="Black" ></Border>
    <Image x:Name="imgPedone3Nero"></Image>
    <Border Grid.Column="3" Grid.Row="1" Background="White" />d
    <Image x:Name="imgPedone4Nero"></Image>
    <Border Grid.Column="4" Grid.Row="1" Background="Black" />
    <Image x:Name="imgPedone5Nero"></Image>
    <Border Grid.Column="5" Grid.Row="1" Background="White" />
    <Image x:Name="imgPedone6Nero"></Image>
    <Border Grid.Column="6" Grid.Row="1" Background="Black" />
    <Image x:Name="imgPedone7Nero"></Image>
    <Border Grid.Column="7" Grid.Row="1" Background="White" />
    <Image x:Name="imgPedone8Nero"></Image>
    <Border Grid.Column="0" Grid.Row="2" Background="White" />
    <Border Grid.Column="1" Grid.Row="2" Background="Black" />
    <Border Grid.Column="2" Grid.Row="2" Background="White" />
    <Border Grid.Column="3" Grid.Row="2" Background="Black" />
    <Border Grid.Column="4" Grid.Row="2" Background="White" />
    <Border Grid.Column="5" Grid.Row="2" Background="Black" />
    <Border Grid.Column="6" Grid.Row="2" Background="White" />
    <Border Grid.Column="7" Grid.Row="2" Background="Black" />
    <Border Grid.Column="0" Grid.Row="3" Background="Black" />
    <Border Grid.Column="1" Grid.Row="3" Background="White" />
    <Border Grid.Column="2" Grid.Row="3" Background="Black" />
    <Border Grid.Column="3" Grid.Row="3" Background="White" />
    <Border Grid.Column="4" Grid.Row="3" Background="Black" />
    <Border Grid.Column="5" Grid.Row="3" Background="White" />
    <Border Grid.Column="6" Grid.Row="3" Background="Black" />
    <Border Grid.Column="7" Grid.Row="3" Background="White" />
    <Border Grid.Column="0" Grid.Row="4" Background="White" />
    <Border Grid.Column="1" Grid.Row="4" Background="Black" />
    <Border Grid.Column="2" Grid.Row="4" Background="White" />
    <Border Grid.Column="3" Grid.Row="4" Background="Black" />
    <Border Grid.Column="4" Grid.Row="4" Background="White" />
    <Border Grid.Column="5" Grid.Row="4" Background="Black" />
    <Border Grid.Column="6" Grid.Row="4" Background="White" />
    <Border Grid.Column="7" Grid.Row="4" Background="Black" />
    <Border Grid.Column="0" Grid.Row="5" Background="Black" />
    <Border Grid.Column="1" Grid.Row="5" Background="White" />
    <Border Grid.Column="2" Grid.Row="5" Background="Black" />
    <Border Grid.Column="3" Grid.Row="5" Background="White" />
    <Border Grid.Column="4" Grid.Row="5" Background="Black" />
    <Border Grid.Column="5" Grid.Row="5" Background="White" />
    <Border Grid.Column="6" Grid.Row="5" Background="Black" />
    <Border Grid.Column="7" Grid.Row="5" Background="White" />
    <Border Grid.Column="0" Grid.Row="6" Background="White" />
    <Image x:Name="imgPedoneBianco"></Image>
    <Border Grid.Column="1" Grid.Row="6" Background="Black" />
    <Image x:Name="imgPedone2Bianco"></Image>
    <Border Grid.Column="2" Grid.Row="6" Background="White" />
    <Image x:Name="imgPedone3Bianco"></Image>
    <Border Grid.Column="3" Grid.Row="6" Background="Black" />
    <Image x:Name="imgPedone4Bianco"></Image>
    <Border Grid.Column="4" Grid.Row="6" Background="White" />
    <Image x:Name="imgPedone5Bianco"></Image>
    <Border Grid.Column="5" Grid.Row="6" Background="Black" />
    <Image x:Name="imgPedone6Bianco"></Image>
    <Border Grid.Column="6" Grid.Row="6" Background="White" />
    <Image x:Name="imgPedone7Bianco"></Image>
    <Border Grid.Column="7" Grid.Row="6" Background="Black" />
    <Image x:Name="imgPedone8Bianco"></Image>
    <Border Grid.Column="0" Grid.Row="7" Background="Black" />
    <Image x:Name="imgTorreBianco"></Image>
    <Border Grid.Column="1" Grid.Row="7" Background="White" />
    <Image x:Name="imgCavalloBianco"></Image>
    <Border Grid.Column="2" Grid.Row="7" Background="Black" />
    <Image x:Name="imgAlfiereBianco"></Image>
    <Border Grid.Column="3" Grid.Row="7" Background="White" />
    <Image x:Name="imgReginaBianco"></Image>
    <Border Grid.Column="4" Grid.Row="7" Background="Black" />
    <Image x:Name="imgReBianco"></Image>
    <Border Grid.Column="5" Grid.Row="7" Background="White" />
    <Image x:Name="imgAlfiere2Bianco"></Image>
    <Border Grid.Column="6" Grid.Row="7" Background="Black" />
    <Image x:Name="imgCavallo2Bianco"></Image>
    <Border Grid.Column="7" Grid.Row="7" Background="White" />
    <Image x:Name="imgTorre2Bianco"></Image>

    <Label x:Name="lbTitolo" Content="      Chess.com" Grid.Column="7" HorizontalAlignment="Left" Margin="233,10,-272,0" VerticalAlignment="Top" Width="140" Height="35" FontSize="15" FontWeight="Bold" Background="#FFD1D0D0"/>

</Grid>

И мой код xaml.cs:

using System;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WpfApp1
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Thread CaricaUI = new Thread(CaricaUIElaborazione);
        }

        void CaricaUIElaborazione()
        {
            int i;
// these are the name that i gave to image, there are pawn,knight,bishop,queen and more important knight
            string[] NomiFoto = new string[] { "torrenero", "torrebianco", "cavallobianco", "cavallonero", "alfierenero", "alfierebianco", "rebianco", "renero", "reginanero", "reginabianco", "pedonebianco", "pedonenero" };
            for(i = 0; i != NomiFoto.Length; i++)
            {
                GetPhotoFile(NomiFoto[i]);
            }
        }
        public ImageSource GetPhotoFile(string nome)
        {
            // Create source
            BitmapImage myBitmapImage = new BitmapImage();

            // BitmapImage.UriSource must be in a BeginInit/EndInit block
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(@"\Pezzi\" + nome + ".png"); // Pezzi = pieces

            myBitmapImage.DecodePixelWidth = 200;
            myBitmapImage.EndInit();

            // set image source
            if (nome == "torrenero") // torrenero = blackknight
            {
                imgTorre2Nero = myBitmapImage;
                imgTorreNero = myBitmapImage;
            }
        }
    }
}

Хорошо, моя самая большая проблема в файле xaml.cs внизу (здесь: publi c ImageSource GetPhotoFile (строка nome)), Visual Studio говорит, что я не могу конвертировать систему. Windows .Media.Imaging. BitmapImage для системы. Windows .Controls.Image, я искал решение, но ничего, поэтому я прошу решение или другой способ загрузить изображение и избежать проблемы. спасибо за все время, отведенное.

...