WPF image control source

后端 未结 3 1551
梦如初夏
梦如初夏 2021-01-17 23:53

im trying to recreate a very simple example of a C# project i WPF, its a simple image viewer.. from the sam\'s teach yourself C#, ive managed to get the open file dialog to

3条回答
  •  春和景丽
    2021-01-18 00:13

    You can also add the image as a resource, ie Add Existing item and change the image's Build Action property to Resource

    then reference it this way

    BitmapImage bitImg = new BitmapImage();
    bitImg.BeginInit();
    bitImg.UriSource = new Uri("./Resource/Images/Bar1.png", UriKind.Relative);
    bitImg.EndInit();
    
    ((Image)sender).Source = bitImg;
    

    This way you dont need to include the image with the program, its bundled into the package as a resource

提交回复
热议问题