小编Ang*_*ino的帖子

如何在UWP应用程序中同步获取图像的尺寸?

我想在OnLoaded()方法中显示图像之前获取图像的尺寸.我想同步(不是异步)这样做,因为我需要在程序继续之前知道图像的尺寸.我正在制作一个映射程序,其中背景图像的坐标,比例和平移偏移很重要,并且都取决于从应用程序的最开始就知道图像的尺寸.

我已经阅读了大部分有关StorageFile,SoftwareBitmap和异步方法调用的msdn文档,并尝试了许多我在网上找到的方法,但没有成功. https://msdn.microsoft.com/en-us/windows/uwp/threading-async/asynchronous-programming-universal-windows-platform-apps

https://msdn.microsoft.com/en-us/windows/uwp/threading-async/call-asynchronous-apis-in-csharp-or-visual-basic

基本上我正在寻找一个同步c#函数,它接受一个图像文件的uri并返回一个包含维度的Point.即公共点getImageDimensions(Uri u){...}

这个功能很容易在Android应用程序中实现,我不明白为什么它在UWP应用程序中如此困难.

如果需要,我可以提供更多详细信息,提前感谢.

我用以下代码得到以下错误:
抛出异常:在App1.exe中
抛出'System.Exception' 异常抛出:mscorlib.ni.dll中的'System.AggregateException'

App1.xaml.cs

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Shapes;

namespace App1
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += OnLoaded;

            Point p = Task.Run(() => getImageDimensions("ms-appx:///Assets/NewSpaceBackground.png")).Result;
            Debug.WriteLine("p.X: " + p.X + " p.Y: " + p.Y);
        }

        void OnLoaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            Rectangle backgroundRect1 = new …
Run Code Online (Sandbox Code Playgroud)

.net c# windows windows-10 windows-10-universal

2
推荐指数
1
解决办法
895
查看次数

标签 统计

.net ×1

c# ×1

windows ×1

windows-10 ×1

windows-10-universal ×1