none
metro C++ 里面如何将安装包里面的某个图片转换成 Stream RRS feed

  • 问题

  • C# 我是这样实现的 现在转换成C++ 代码该怎么实现呢?

    StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\Backgroud.jpg");

               Stream stream=   await file.OpenStreamForReadAsync();
               byte[] bytes = ConvertStreamTobyte(stream);
               InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
               var outputStream = randomAccessStream.GetOutputStreamAt(0);
               DataWriter datawriter = new DataWriter(outputStream);
               datawriter.WriteBytes(bytes);
               await datawriter.StoreAsync();
               BitmapImage bitmapImage = new BitmapImage();
               bitmapImage.SetSource(randomAccessStream);
               image1.Source = bitmapImage;

    2012年5月16日 8:32

答案

  • 这样就可以了:

    #include "pch.h"
    #include "BlankPage.xaml.h"
    #include <collection.h>
    #include <ppltasks.h>
    
    using namespace Application6;
    
    using namespace Concurrency;
    using namespace Platform;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml;
    using namespace Windows::UI::Xaml::Controls;
    using namespace Windows::UI::Xaml::Controls::Primitives;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Input;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Media::Imaging;
    using namespace Windows::UI::Xaml::Navigation;
    using namespace Windows::ApplicationModel;
    using namespace Windows::Storage;
    using namespace Windows::Storage::Streams;
    
    ......
    
    	task<StorageFile^>(Package::Current->InstalledLocation->GetFileAsync("Assets\\Background.jpg")).then([this](StorageFile^ file){
    		 task<IBuffer^>(FileIO::ReadBufferAsync(file)).then([this](IBuffer^ buffer){			
    			InMemoryRandomAccessStream^ randomAccessStream = ref new InMemoryRandomAccessStream();
    			IOutputStream^ outputStream = randomAccessStream->GetOutputStreamAt(0);
    			DataWriter^ datawriter = ref new DataWriter(outputStream);
    			datawriter->WriteBuffer(buffer);
    			task<unsigned int>(datawriter->StoreAsync()).then([this, randomAccessStream](unsigned int i){
    				BitmapImage^ bitmapImage = ref new BitmapImage();
    				bitmapImage->SetSource(randomAccessStream);
    				image1->Source = bitmapImage;
    			});
    		});	
    	});
      
    


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 john_shen 2012年5月17日 10:57
    2012年5月17日 10:45
    版主

全部回复

  • 这样就可以了:

    #include "pch.h"
    #include "BlankPage.xaml.h"
    #include <collection.h>
    #include <ppltasks.h>
    
    using namespace Application6;
    
    using namespace Concurrency;
    using namespace Platform;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml;
    using namespace Windows::UI::Xaml::Controls;
    using namespace Windows::UI::Xaml::Controls::Primitives;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Input;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Media::Imaging;
    using namespace Windows::UI::Xaml::Navigation;
    using namespace Windows::ApplicationModel;
    using namespace Windows::Storage;
    using namespace Windows::Storage::Streams;
    
    ......
    
    	task<StorageFile^>(Package::Current->InstalledLocation->GetFileAsync("Assets\\Background.jpg")).then([this](StorageFile^ file){
    		 task<IBuffer^>(FileIO::ReadBufferAsync(file)).then([this](IBuffer^ buffer){			
    			InMemoryRandomAccessStream^ randomAccessStream = ref new InMemoryRandomAccessStream();
    			IOutputStream^ outputStream = randomAccessStream->GetOutputStreamAt(0);
    			DataWriter^ datawriter = ref new DataWriter(outputStream);
    			datawriter->WriteBuffer(buffer);
    			task<unsigned int>(datawriter->StoreAsync()).then([this, randomAccessStream](unsigned int i){
    				BitmapImage^ bitmapImage = ref new BitmapImage();
    				bitmapImage->SetSource(randomAccessStream);
    				image1->Source = bitmapImage;
    			});
    		});	
    	});
      
    


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 john_shen 2012年5月17日 10:57
    2012年5月17日 10:45
    版主
  • Thanks Bob!~
    2012年5月17日 10:58