gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:44:32 24 4
gpt4 key购买 nike

在 OnLoaded() 方法中显示图像之前,我试图获取图像的尺寸。我想同步(而不是异步)执行此操作,因为我需要在程序继续之前知道图像的尺寸。我正在制作一个 map 程序,其中背景图像的坐标、比例和平移偏移量很重要,并且都依赖于从应用程序一开始就知道图像的尺寸。

我已经阅读了大部分关于 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 并返回一个包含尺寸的点。 IE。公共(public)点 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 Rectangle();
ImageBrush imgBrushBackground1 = new ImageBrush();
imgBrushBackground1.ImageSource = new BitmapImage(new Uri(@"ms-appx:///Assets/NewSpaceBackground.png"));
backgroundRect1.Fill = imgBrushBackground1;
//backgroundMap.Add(backgroundRect1);
}

public async Task<Point> getImageDimensions(string strURI)
{
Point p;
StorageFile photo;
try
{
var uri = new Uri(strURI);
photo = await StorageFile.GetFileFromApplicationUriAsync(uri);
}
catch (Exception e)
{
Debug.WriteLine(e.StackTrace);
return p;
}
//read in the bitmap stream
IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

//convert the image to a bitmap
SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

//print out the dimensions
Debug.WriteLine(softwareBitmapBGR8.PixelWidth + " " + softwareBitmapBGR8.PixelHeight);
p.X = softwareBitmapBGR8.PixelWidth;
p.Y = softwareBitmapBGR8.PixelHeight;

return p;
}
}
}

最佳答案

I am trying to get an image's dimensions before I display it in the OnLoaded() method. I want to do this synchronously (not asynchronously) because I need to know the image's dimensions before the program continues.

在constructor中调用异步函数会遇到一些问题,具体可以引用Stephen Cleary的博客:Async OOP 2: Constructors .

在UWP中,可以将页面初始化代码(getImageDimensions和显示图片的代码)放在Page.OnNavigatedTo中方法并在其中使用 await:

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
Point p = await getImageDimensions("ms-appx:///Assets/NewSpaceBackground.png");
Debug.WriteLine("p.X: " + p.X + " p.Y: " + p.Y);

Rectangle backgroundRect1 = new Rectangle();
ImageBrush imgBrushBackground1 = new ImageBrush();
imgBrushBackground1.ImageSource = new BitmapImage(new Uri(@"ms-appx:///Images/image03.jpg"));
backgroundRect1.Fill = imgBrushBackground1;
...
}

更新:

Would you recommend an easier, synchronous, way to get the dimensions?

大部分文件相关操作都被设计成异步的,用户可以使用/不使用await来选择同步或异步操作。

确实有一种简单的方法可以获取图像的尺寸:

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///images/image01.png"));
var properties=await file.Properties.GetImagePropertiesAsync();
var width = properties.Width;
var height = properties.Height;

关于c# - 如何在 UWP 应用程序中同步获取图像的尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39923712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com