UWP BitmapEncoder SetProperties is not supported

1

So I'm creating a UWP app and need to write text and images to an image file and save them. I've currently created a D2D wrapper in C++ using this source and then a C# wrapper that simply lets you access the functions of the C++ class in nice C# code (it does nothing else but access the WINMD C++ wrapper).

However, after we write the image and text to the stream, we need to be able to save the file with EXIF headers containing info about rotation and location.

We are using encoder.BitmapProperties.SetPropertiesAsync to accomplish this, but it returns:

Exception thrown: 'System.Exception' in Tester.exe

Additional information: The operation is unsupported. (Exception from HRESULT: 0x88982F81)

There is no inner exception and the stack trace is:

   at Windows.Graphics.Imaging.BitmapEncoder.get_BitmapProperties()
   at Tester.ViewModels.<_SaveImageWithPropertiesAsync>d__56.MoveNext()

I want to try to avoid manually adding the headers, as I fear the returned stream from the D2DContext doesn't contain any header information or whatnot so it can't append anything to it.

Here is the relevant part of my code:

C++

Again, you can find the original source here

IRandomAccessStream^ D2DWrapper::EndDraw()
{
    DX::ThrowIfFailed(
        m_d2dContext->EndDraw()
    );

    // If needPreview is true, we return a valid IRandomAccessStream reference.

    GUID wicFormat = GUID_ContainerFormatBmp;
    ComPtr<IStream> stream;
    ComPtr<ISequentialStream> ss;
    auto inMemStream = ref new InMemoryRandomAccessStream();
    DX::ThrowIfFailed(
        CreateStreamOverRandomAccessStream(inMemStream, IID_PPV_ARGS(&stream))
        );

    SaveBitmapToStream(m_targetBitmap, m_wicFactory, m_d2dContext, wicFormat, stream.Get());

    return inMemStream;

}

C# Wrapper

public IRandomAccessStream EndDraw() {
    return _context.EndDraw()
}

C# Tester

async Task _SaveImageWithPropertiesAsync(UWPImaging.Image image, PhotoOrientation photoOrientation)
{
    var ras = await image.EndDraw();

    var decoder = await BitmapDecoder.CreateAsync(ras);

    var file = await KnownFolders.PicturesLibrary.CreateFileAsync(
        "test.jpeg",
        CreationCollisionOption.GenerateUniqueName);

    using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
    {
        var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);

        var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(photoOrientation, PropertyType.UInt16) } };

        await encoder.BitmapProperties.SetPropertiesAsync(properties); // This is where the error occurs

        await encoder.FlushAsync();
    }
}

Any help on how to get those headers in would be much appreciated!

Thanks,

Nolan

c#
c++
graphics
directx
uwp
asked on Stack Overflow Jun 1, 2016 by Nolan Blew

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0