I am new to WinRT c++. I am trying to pass an StorageFile image from C# and open the file and set it as source in BitmapImage in WinRT to extract height and width of image. I am using the following code.
auto openOperation = StorageImageFile->OpenAsync(FileAccessMode::Read); // from http://msdn.microsoft.com/en-us/library/windows/desktop/hh780393%28v=vs.85%29.aspx
openOperation->Completed = ref new
AsyncOperationCompletedHandler<IRandomAccessStream^>(
[=](IAsyncOperation<IRandomAccessStream^> ^operation, AsyncStatus status)
{
auto Imagestream = operation->GetResults();
BitmapImage^ bmp = ref new BitmapImage();
auto bmpOp = bmp->SetSourceAsync(Imagestream);
bmpOp->Completed = ref new
AsyncActionCompletedHandler (
[=](IAsyncAction^ action, AsyncStatus status)
{
action->GetResults();
UINT32 imageWidth = (UINT32)bmp->PixelWidth;
UINT32 imageHeight = (UINT32)bmp->PixelHeight;
});
});
This code does not seem to work. after the line BitmapImage^ bmp = ref new BitmapImage(); the debugger stops saying no source code is found. Can you help me write the correct code?
I think you meant to write
openOperation->Completed
+=
ref new...
andbmpOp->Completed
+=
ref new...
. I'm not an expert in C++, but from what I have seen - async operations are typically wrapped increate_task
calls. Not really sure why - maybe to avoid subscribing to events without unsubscribing?I think it should look roughly like this: