Skia.WPF Uno Platform 项目中未显示本地图像
Local Image not displayed in Skia.WPF Uno Platform project
我将图像保存到工作目录,然后想在 uno Skia.WPF 应用程序中显示它们。
然而他们从未出现过。
这就是我创建 ImageSource 的方式:
public static async Task<ImageSource> GenerateSource()
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".png");
var pickerResult = await picker.PickSingleFileAsync("tileset");
if (pickerResult is null)
return null;
using var stream = await pickerResult.OpenReadAsync();
string imagePath = Path.GetFullPath(Path.Combine("cache", "image.png"));
Directory.CreateDirectory(Path.GetDirectoryName(imagePath));
using (var bitmapImageStream = File.Open(imagePath,
FileMode.Create,
FileAccess.Write,
FileShare.None))
{
await stream.AsStreamForRead().CopyToAsync(bitmapImageStream);
bitmapImageStream.Flush(true);
}
var imgSrc = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
imgSrc.UriSource = new Uri($"file://{imagePath}");
var width = imgSrc.PixelHeight;
return imgSrc;
}
但是当我直接在 Xaml 中使用路径时它也不起作用。
<Image Source="file://cache/image.png" Width="32" Height="32" />
通过 http url 使用来自 Internet 的图像有效。
Uno 当前不支持 file:
URI,但是,您仍然可以使用 ms-appdata
方案通过复制到适当的文件夹来显示本地图像。如果您将文件复制到 ApplicationData.Current.TemporaryFolder
,您将能够通过 ms-appdata:///temp/{imageName}
URI 引用该路径。
我将图像保存到工作目录,然后想在 uno Skia.WPF 应用程序中显示它们。
然而他们从未出现过。 这就是我创建 ImageSource 的方式:
public static async Task<ImageSource> GenerateSource()
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".png");
var pickerResult = await picker.PickSingleFileAsync("tileset");
if (pickerResult is null)
return null;
using var stream = await pickerResult.OpenReadAsync();
string imagePath = Path.GetFullPath(Path.Combine("cache", "image.png"));
Directory.CreateDirectory(Path.GetDirectoryName(imagePath));
using (var bitmapImageStream = File.Open(imagePath,
FileMode.Create,
FileAccess.Write,
FileShare.None))
{
await stream.AsStreamForRead().CopyToAsync(bitmapImageStream);
bitmapImageStream.Flush(true);
}
var imgSrc = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
imgSrc.UriSource = new Uri($"file://{imagePath}");
var width = imgSrc.PixelHeight;
return imgSrc;
}
但是当我直接在 Xaml 中使用路径时它也不起作用。
<Image Source="file://cache/image.png" Width="32" Height="32" />
通过 http url 使用来自 Internet 的图像有效。
file:
URI,但是,您仍然可以使用 ms-appdata
方案通过复制到适当的文件夹来显示本地图像。如果您将文件复制到 ApplicationData.Current.TemporaryFolder
,您将能够通过 ms-appdata:///temp/{imageName}
URI 引用该路径。