locked
How can I open an image

    Question

  • I want to open an image and set it as Source of Image_1.

    Next is code , but I don't know how to use CreateStreamedFileAsync. Anyone can help me.

    Windows::Storage::StreamedFileDataRequestedHandler^ dataRequested;
    Windows::Storage::Streams::IRandomAccessStreamReference^ thumbnail;
    IAsyncOperation<Windows::Storage::StorageFile^>^ fileAsync = StorageFile::CreateStreamedFileAsync(TEXT("Assets/win8.png"), dataRequested, thumbnail);
    	
    auto imageTask = create_task(fileAsync);
    imageTask.then([this](StorageFile^ file)
    {
    	if (file)
    	{
    		// Ensure the stream is disposed once the image is loaded
    		create_task(file->OpenAsync(Windows::Storage::FileAccessMode::Read)).then([this](IRandomAccessStream^ fileStream)
    		{
    		// Set the image source to the selected bitmap
    		auto bitmapImage = ref new BitmapImage();
    		bitmapImage->DecodePixelHeight = 100;
    		bitmapImage->DecodePixelWidth = 100;
    
    		bitmapImage->SetSource(fileStream);
    		Image_1->Source = bitmapImage;
    	});
                     }
    });

    Friday, September 21, 2012 3:58 AM

Answers

  • CreateStreamedFileAsync is for when you want to generate the file data at runtime. The StreamedFileDataRequestedHandler gets called to provide the stream with the data. If you want to load the image from a file then you can use StorageFile::GetFileFromApplicationUriAsync(ref new Uri("ms-appx:///Assets/Win8.png"));

    Take a look at the XAML images sample for a complete example.

    --Rob

    • Marked as answer by Jesse Jiang Thursday, September 27, 2012 2:53 AM
    Sunday, September 23, 2012 4:17 AM
    Owner