C# WPF image控件 图片 Source

加载图片的几种方式

1、加载网络图片

<Image Source="http://example.com/path/to/image.jpg" Stretch="UniformToFill"/>

 

2、加载项目中的图片

<Image Source="path/to/your/image.jpg" Stretch="UniformToFill"/>

图片需先加入项目:

 

 

3、加载本地图片(相对路径)

<Image Source="pack://siteoforigin:,,,/imgs/1.jpg" />

在后台代码中,则写成:

img.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/imgs/1.jpg", UriKind.Absolute));

虽然是相对路径,但是参数必须选择 UriKind.Absolute。搞不懂。

 

这种写法,图片不必事先加入项目,只需要在程序运行时,该路径下的图片存在即可(如果图片不存在,则程序直接崩溃并退出)

 

 

如果写成:

img.Source = new BitmapImage(new Uri(@"imgs/1.jpg", UriKind.Relative));

则要求图片必须先添加的项目的资源中,因为以上写法是以下写法的简写:

img.Source = new BitmapImage(new Uri(@"pack://application:,,,/imgs/1.jpg", UriKind.Absolute));

 

4、加载本地图片(绝对路径)

<Image Source="pack://siteoforigin:,,,/e:/imgs/1.jpg" />

 

 

路径斜杠的两张书写格式:

img.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/E:\\04Window.png", UriKind.Absolute));

或

img.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/E:/04Window.png", UriKind.Absolute));

 

可以直接写文件路径?:

img.Source = new BitmapImage(new Uri(@"E:\04 Window.png", UriKind.Absolute));

 

循环显示文件夹中的图片:

    public partial class MainWindow : Window
    {
        string[] files = Directory.GetFiles(@"C:\Users\Administrator\source\repos\WpfApp3\bin\Debug\imgs");   
        int position = -1;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnPre_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            position++; 
            if(position == files.Length)
            {
                position = 0;
            }
            img.Source = new BitmapImage(new Uri(files[position], UriKind.Absolute));
            
        }
    }

或者(把文件路径写成相对路径)

    public partial class MainWindow : Window
    {
        string[] files = Directory.GetFiles(@".\imgs"); 
        int position = -1;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnPre_Click(object sender, RoutedEventArgs e)
        {
            position--;
            if (position < 0)
            {
                position = files.Length - 1;
            }
            string str = Directory.GetCurrentDirectory();
            img.Source = new BitmapImage(new Uri(str + files[position], UriKind.Absolute));
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            position++; 
            if(position == files.Length)
            {
                position = 0;
            }                        
            string str = Directory.GetCurrentDirectory();            
            img.Source = new BitmapImage(new Uri(str + files[position], UriKind.Absolute));
        }
    }

 或

public partial class MainWindow : Window
{
    string[] files = Directory.GetFiles("imgs"); 
    int position = -1;

    public MainWindow()
    {
        InitializeComponent();
        //img.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/Pics/mm.jpg", UriKind.Absolute));
    }

    private void btnPre_Click(object sender, RoutedEventArgs e)
    {
        position--;
        if (position < 0)
        {
            position = files.Length - 1;
        }
        img.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/" + files[position], UriKind.Absolute));
    }

    private void btnNext_Click(object sender, RoutedEventArgs e)
    {
        position++; 
        if(position == files.Length)
        {
            position = 0;
        }            
        img.Source = new BitmapImage(new Uri("pack://siteoforigin:,,,/" + files[position], UriKind.Absolute));
    }
}

 

posted @ 2025-03-26 23:03  竹楼风雨声  阅读(515)  评论(0)    收藏  举报