WicImagingFactory.CreateDecoderFromFileName | FileNotFoundException

Gavin Williams 761 Reputation points
2021-03-11T12:30:43.36+00:00

I have the following code

cs
private static async Task<BitmapSource> LoadBitmapSourceUsingWIC(Folders folder, string path)
{
    StorageFile file = await GetStorageFileAsync(folder, path);
    BitmapSource bmSource;
    unsafe
    {
        IWICImagingFactory* wicImagingFactory = CoreGraphics.WicImagingFactory.GetPtr();
        IWICBitmapDecoder* bmDecoder;
        IWICBitmapFrameDecode* bmFrame = null;

        IntPtr fileNameIntPtr = Marshal.StringToHGlobalAnsi(file.Path);

        HRESULT hr = wicImagingFactory->CreateDecoderFromFilename(
            (ushort*)fileNameIntPtr,
            null,
            GENERIC_READ,
            WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
            &bmDecoder);

But it's throwing a FileNotFoundException. The file (StorageFile) is valid.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Gavin Williams 761 Reputation points
    2021-03-11T13:13:20.537+00:00

    I had to use this instead...

    IntPtr fileNameIntPtr = Marshal.StringToHGlobalUni(file.Path);

    There is so much inconsistency in Windows API's with strings. Why not just fix up the API's across Windows and between languages and make a collection of API's that are really easy to use together?

    0 comments No comments

0 additional answers

Sort by: Most helpful