locked
Loading Image from web path to StorageFile^ in Metro C++

    Question

  • I am trying show an Image in Windows 8 Metro C++ Application . To do so, I need to load image from another computer to StorageFile^ first. I assume it should looks like:

    using namespace Windows::Storage;
    				String^ url= ref new String(L"http://10.52.31.60:8012//PicUrl//a.jpg") ;
    		Uri^ uri = ref new Uri(url);
    				auto getFileTask = create_task(StorageFile::GetFileFromApplicationUriAsync(uri));
    				getFileTask.then([foodClass](StorageFile^ file)
    		{
    			try
    			{
    				file->CopyAsync(ApplicationData::Current->LocalFolder , foodClass->PicName->ToString()+".jpg",NameCollisionOption::ReplaceExisting);
    			}
    			catch(Exception^ ex)
    			{
    		
    			}
    		});
    

    but when the application run to create_task , it crashed and throw below erro message:

    First-chance exception at 0x7470277C in OrderFoodSystem.App.exe: Microsoft C++ exception: Platform::InvalidArgumentException ^ at memory location 0x0271D2DC. HRESULT:0x80070057

    Would be very grateful if someone knows how to do it properly.

    Tuesday, September 25, 2012 7:19 AM

Answers

  • Dave is correct. The GetFileFromApplicationUriAsync api expects the ms-appx:// and ms-appdata:// protocols in the URI which provide access to app content.

    There may be an easier way to accomplish what your wanting. If using XAML, the Image control allows you to set the Source to the desired content:

    <Image Source="http://10.52.31.60:8012/PicUrl/a.jpg" Visibility="Visible" HorizontalAlignment="Left" Height="507" VerticalAlignment="Top" Width="648"/>


    David Lamb

    • Marked as answer by Jesse Jiang Monday, October 1, 2012 6:00 AM
    Tuesday, September 25, 2012 8:09 PM
    Moderator
  • WinRT has a few types that relate to this topic.

    IRandomAccessStream (abstract type with many implementations). This represents large streamable data and is the currency used in APIs that have large inputs and outputs (larger than main memory). many of those APIs also accept StorageFile or a Uri as convenience and as a way to defer the binding to the stream since that can be expensive.

    RandomAccessStreamReference, an object that can produce a stream. this enables deferring the work and expense of opening the stream, you can create one from a Uri, StorageFile or an already open stream. for inputs this encapsulates all 3 of these different objects that can produce (or already are) a stream.

    if you are using a Xaml image control use the Image.Source property, you can provide the uri by using the BitmapSource(uri) constructor. read about that here:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.image.source.aspx

    If you really need a storage file, you can create one from a stream using StorageFile.CreateStreamedFileAsync() and CreateStreamedFileFromUriAsync(). these are rarely needed, but when they are they let you provide the stream of the file without actually creating a file on disk (the system creates one for you).

    Chris

    • Marked as answer by Jesse Jiang Monday, October 1, 2012 6:00 AM
    Wednesday, September 26, 2012 5:11 AM

All replies

  • i think you first have to download it, save it somewhere and then can use it as a IStorageFile
    Tuesday, September 25, 2012 5:07 PM
  • Dave is correct. The GetFileFromApplicationUriAsync api expects the ms-appx:// and ms-appdata:// protocols in the URI which provide access to app content.

    There may be an easier way to accomplish what your wanting. If using XAML, the Image control allows you to set the Source to the desired content:

    <Image Source="http://10.52.31.60:8012/PicUrl/a.jpg" Visibility="Visible" HorizontalAlignment="Left" Height="507" VerticalAlignment="Top" Width="648"/>


    David Lamb

    • Marked as answer by Jesse Jiang Monday, October 1, 2012 6:00 AM
    Tuesday, September 25, 2012 8:09 PM
    Moderator
  • thanks for your reply 

    the app run at windows8 pad, this is client,but the data come from a server by webservice,I want get the data once only,and save the data on local path,so how can I save the image?

    Wednesday, September 26, 2012 1:55 AM
  • how to save the image?
    Wednesday, September 26, 2012 1:55 AM
  • WinRT has a few types that relate to this topic.

    IRandomAccessStream (abstract type with many implementations). This represents large streamable data and is the currency used in APIs that have large inputs and outputs (larger than main memory). many of those APIs also accept StorageFile or a Uri as convenience and as a way to defer the binding to the stream since that can be expensive.

    RandomAccessStreamReference, an object that can produce a stream. this enables deferring the work and expense of opening the stream, you can create one from a Uri, StorageFile or an already open stream. for inputs this encapsulates all 3 of these different objects that can produce (or already are) a stream.

    if you are using a Xaml image control use the Image.Source property, you can provide the uri by using the BitmapSource(uri) constructor. read about that here:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.image.source.aspx

    If you really need a storage file, you can create one from a stream using StorageFile.CreateStreamedFileAsync() and CreateStreamedFileFromUriAsync(). these are rarely needed, but when they are they let you provide the stream of the file without actually creating a file on disk (the system creates one for you).

    Chris

    • Marked as answer by Jesse Jiang Monday, October 1, 2012 6:00 AM
    Wednesday, September 26, 2012 5:11 AM