设置图像源 [英] Set image source

查看:124
本文介绍了设置图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置图像源的东西从我的电脑(在资产不)

这是我在努力做到这一点:

 开放的URI =新的URI(@D:\Riot Games\about.png,UriKind.Absolute); 
的ImageSource imgSource =新的BitmapImage(URI);

this.image1.Source = imgSource;



我试过几乎所有我能找到在互联网上,但没有似乎工作。



任何想法,为什么



XAML:

 <用户控件
X:类=App11.VideoPreview
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns :X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
的xmlns:本地=使用:App11
的xmlns:D =htt​​p://schemas.microsoft。 COM /表达/混合/ 2008
的xmlns:MC =http://schemas.openxmlformats.org/markup-compatibility/2006
MC:可忽略=D
D:DesignHeight =250
D:DesignWidth =250>

<网格和GT;
将按钮高度=250宽度=250填充=0了borderThickness =0>
<&StackPanel的GT;
<图像名称=此搜索HEIGHT =250WIDTH =250/>
<电网保证金=0 -74,0,0>
< Grid.Background>
<一个LinearGradientBrush终点=0.5,1StartPoint可以=0.5,0透明度=0.75>
<渐变停止颜色=黑/>
将;渐变停止颜色=#FF5B5B5B偏移=1/>
< /一个LinearGradientBrush>
< /Grid.Background>
< TextBlock的X:名称=textBox1的TextWrapping =自动换行文本=测试的FlowDirection =从右至左前景=白填充=10/>
< /网格和GT;
< / StackPanel的>
< /按钮>
< /网格和GT;
< /用户控件>


解决方案

您无法从Windows直接访问磁盘驱动器的地铁应用。从文件访问权限在Windows中提取存储应用




您可以访问特定文件系统位置,如应用程序安装
目录,应用数据位置和下载文件夹,在Windows
Store应用程序默认。应用程序还可以访问其他地方
通过文件选择器,或通过声明的能力。




但也有一些特殊的文件夹,其中你可以从你的包清单文件支持功能访问诸如图片库,文档库等。因此,该代码会从清单文件中启用图片库后才能正常运行(复制about.png在图片文件库文件夹)

 专用异步无效SetImageSource()
{
var文件=等待
Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync(about.png);
VAR流=等待file.OpenReadAsync();
变种的BitmapImage =新的BitmapImage();
bitmapImage.SetSource(流);

image1.Source = BitmapImage的;
}



不过,理想的解决方案将包括您在您的应用程序文件,并设置其生成行动的内容,以便它可以在您的 APPX 文件夹与其他内容文件一起复制。然后,你可以这样设置图像源 -

 公开的MainPage()
{
this.InitializeComponent ();
乌里URI =新的URI(基本URIabout.png);
的BitmapImage imgSource =新的BitmapImage(URI);
this.image1.Source = imgSource;
}



或者你可以简单地做这个XAML中的:

 <图像X:NAME =此搜索来源=MS-APPX:/about.png/> 






下面是您可以使用特殊的文件夹列表从您的应用程序 -




  1. 本地应用程序数据

  2. 漫游应用数据

  3. 的临时应用程序数据

  4. 安装应用程序的位置

  5. Downloads文件夹

  6. 文档库

  7. 音乐库

  8. 图片库

  9. 视频库

  10. 移动设备

  11. 家庭组

  12. 媒体服务器设备



要能够通过您的清单文件,双击功能上功能选项卡下的解决方案中的Package.appxmanifest 文件,并检查图片库复选框,以便为您的应用程序。同样,你可以为你要访问其他文件夹做到这一点。



在这里输入的形象描述


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 Games\about.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>

解决方案

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.

这篇关于设置图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆