Reputation: 287
I can easily create a BitmapImage from a resource JPG image file using the following code...
Windows::Foundation::Uri^ uri = ref new Windows::Foundation::Uri(L"ms-appx:///Hippo.JPG");
Imaging::BitmapImage^ image = ref new Imaging::BitmapImage(uri);
But WritableBitmap does not take an Uri. I see a SetSource method, but that needs a IRandomaccessStream and not an Uri. And I have no clue how to create one from a JPG file. I searched the net over and over again, but could not find a straight forward answer. Any help would be greatly appreciated.
I want something like this...
Windows::UI::Xaml::Media::Imaging::WriteableBitmap image = ref new Windows::UI::Xaml::Media::Imaging::WriteableBitmap();
image->SetSource(somehowGetRandomAccessStreamFromUri);
But, how do I get the IRandomaccessStream instance from a uri? I started working on C++ Metro app only today, so might be wrong, but I find it to be overly complicated with too much of onion peeling.
Upvotes: 2
Views: 2410
Reputation: 31724
In C# you would do something like
var storageFile = await Package.Current.InstalledLocation.GetFileAsync(relativePath.Replace('/', '\\'));
var stream = await storageFile.OpenReadAsync();
var wb = new WriteableBitmap(1, 1);
wb.SetSource(stream);
I think in C++/CX you would do something like this:
#include <ppl.h>
#include <ppltasks.h>
...
Concurrency::task<Windows::Storage::StorageFile^> getFileTask
(Package::Current->InstalledLocation->GetFileAsync(L"Assets\\MyImage.jpg"));
auto getStreamTask = getFileTask.then(
[] (Windows::Storage::StorageFile ^storageFile)
{
return storageFile->OpenReadAsync();
});
getStreamTask.then(
[] (Windows::Storage::Streams::IRandomAccessStreamWithContentType^ stream)
{
auto wb = ref new Windows::UI::Xaml::Media::Imaging::WriteableBitmap(1, 1);
wb->SetSource(stream);
});
Upvotes: 2