Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c# - Set image source

I am trying to set image source to something from my computer (not in the assets).
This is how I am trying to do this:

Uri uri = new Uri(@"D:Riot Gamesabout.png", UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);

this.image1.Source = imgSource;

I tried almost everything I could find in the internet but nothing seem to work.

Any idea why?

XAML:

<UserControl
    x:Class="App11.VideoPreview"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App11"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="250"
    d:DesignWidth="250">

    <Grid>
        <Button Height="250" Width="250" Padding="0" BorderThickness="0">
            <StackPanel>
                <Image Name="image1" Height="250" Width="250"/>
                <Grid Margin="0,-74,0,0">
                    <Grid.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" Opacity="0.75">
                            <GradientStop Color="Black"/>
                            <GradientStop Color="#FF5B5B5B" Offset="1"/>
                        </LinearGradientBrush>
                    </Grid.Background>
                    <TextBlock x:Name="textBox1" TextWrapping="Wrap" Text="test" FlowDirection="RightToLeft" Foreground="White" Padding="5"/>
                </Grid>
            </StackPanel>
        </Button>
    </Grid>
</UserControl>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot access disk drives directly from your windows metro apps. Extracted from File access permissions in windows store apps

You can access certain file system locations, like the app install directory, app data locations, and the Downloads folder, with Windows Store apps by default. Apps can also access additional locations through the file picker, or by declaring capabilities.

But there are some special folders which you can access like Pictures library, documents library etc. by enabling capabilities from your package manifest file. So, this code will work after enabling pictures library from manifest file (copy about.png file in pictures library folder)

    private async void SetImageSource()
    {
        var file = await 
          Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("about.png");
        var stream = await file.OpenReadAsync();
        var bitmapImage = new BitmapImage();
        bitmapImage.SetSource(stream);

        image1.Source = bitmapImage;
    }

But ideal solution would be to include you file in you application and set its build action to Content so that it can be copied in your Appx folder along with other content files. Then you can set the image source like this -

    public MainPage()
    {
        this.InitializeComponent();
        Uri uri = new Uri(BaseUri, "about.png");
        BitmapImage imgSource = new BitmapImage(uri);
        this.image1.Source = imgSource;
    }

OR you can simply do this in XAML only :

<Image x:Name="image1" Source="ms-appx:/about.png"/>

Here is list of special folders which you can access from your application -

  1. Local App data
  2. Roaming app data
  3. Temporary app data
  4. App installed location
  5. Downloads folder
  6. Documents library
  7. Music library
  8. Pictures library
  9. Videos library
  10. Removable devices
  11. Home group
  12. Media Server devices

To enable capabilities from your manifest file, double click on Package.appxmanifest file in your solution and check Pictures Library checkbox under capabilities tab to enable it for your application. Likewise you can do it for other folders which you want to access.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...