WPF application
.NET 5.0.0-preview.7.20364.11
Microsoft.Windows.CsWinRT 0.1.0-prerelease.200629.3
Microsoft.Windows.SDK.NET 10.0.18362.3-preview
Version 16.7.0 Preview 6.0
I have been using code similar to the following to produce an image of a page in a pdf document in WPF application using WinRT APIs. The same type of code works on .NET 4.8 and .NET Core 3.1.6 using the older strategy of consuming WinRT APIs in .NET desktop apps. Appreciate that these are prerelease/preview packages and that perhaps things might not be expected to work yet.
var file = await StorageFile.GetFileFromPathAsync(path);
using var stream = await file.OpenReadAsync();
var pdfDocument = await pdfDocument.LoadFromStreamAsync(stream);
using var pdfPage = pdfDocument.GetPage(0);
using var winrtStream = new InMemoryRandomAccessStream());
await pdfPage.RenderToStreamAsync(winrtStream, new PdfPageRenderOptions());
using normalStream = winrtStream.AsStream();
var imageSource = new System.Windows.Media.Imaging.BitmapImage();
imageSource.BeginInit();
imageSource.CacheOption = BitmapCacheOption.OnLoad;
imageSource.StreamSource = normalStream;
imageSource.EndInit();
Throws System.InvalidCastException.
System.InvalidCastException: Specified cast is not valid.
at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
Same exception thrown if I attempt to do anything with the "normalStream" produced by WindowsRuntimeStreamExtensions.AsStream e.g.
using normalStream = winrtStream.AsStream();
using var memoryStream = new MemoryStream();
normalStream.CopyTo(memoryStream);
WPF application
.NET 5.0.0-preview.7.20364.11
Microsoft.Windows.CsWinRT 0.1.0-prerelease.200629.3
Microsoft.Windows.SDK.NET 10.0.18362.3-preview
Version 16.7.0 Preview 6.0
I have been using code similar to the following to produce an image of a page in a pdf document in WPF application using WinRT APIs. The same type of code works on .NET 4.8 and .NET Core 3.1.6 using the older strategy of consuming WinRT APIs in .NET desktop apps. Appreciate that these are prerelease/preview packages and that perhaps things might not be expected to work yet.
Throws
System.InvalidCastException.Same exception thrown if I attempt to do anything with the "normalStream" produced by
WindowsRuntimeStreamExtensions.AsStreame.g.