WPF relative uri

 <Image Grid.Column="0" 
        Source="pack://application:,,,/WpfApp431;component/Images/1.jpg"
        RenderOptions.BitmapScalingMode="Fant"/>
 <Image Grid.Column="1" 
        Source="pack://application:,,,/Images/1.jpg"
        RenderOptions.BitmapScalingMode="Fant"/>
 <Image Grid.Column="2" 
        Source="/Images/1.jpg" 
        RenderOptions.BitmapScalingMode="Fant"/>
 <Image Grid.Column="3" x:Name="img" 
        RenderOptions.BitmapScalingMode="Fant"/>


//cs
img.Source = new BitmapImage(new Uri("pack://application:,,,/Images/1.jpg"));

 

Source="pack://application:,,,/WpfApp431;component/Images/1.jpg"



Source="pack://application:,,,/Images/1.jpg"



Source="/Images/1.jpg"


img.Source = new BitmapImage(new Uri("pack://application:,,,/Images/1.jpg"));

 

 

 

 

//xaml
<Window x:Class="WpfApp431.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:WpfApp431"
        mc:Ignorable="d"
        WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Image Grid.Column="0" 
               Source="pack://application:,,,/WpfApp431;component/Images/1.jpg"
               RenderOptions.BitmapScalingMode="Fant"/>
        <Image Grid.Column="1" 
               Source="pack://application:,,,/Images/1.jpg"
               RenderOptions.BitmapScalingMode="Fant"/>
        <Image Grid.Column="2" 
               Source="/Images/1.jpg" 
               RenderOptions.BitmapScalingMode="Fant"/>
        <Image Grid.Column="3" x:Name="img" 
               RenderOptions.BitmapScalingMode="Fant"/>
    </Grid>
</Window>


//cs
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.Navigation;
using System.Windows.Shapes;

namespace WpfApp431
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            img.Source = new BitmapImage(new Uri("pack://application:,,,/Images/1.jpg"));
        }
    }
}

 

posted @ 2024-09-30 20:53  FredGrit  阅读(4)  评论(0编辑  收藏  举报