Windows 8 學習筆記(二十三)–WritableBitmap的讀寫操作C++

來源:互聯網
上載者:User

在開發中,涉及圖片的處理經常需要用到WritableBitmap對象,這個對象可以直接作為資料來源賦值給Image控制項,但若要儲存這個對象,我們只能將其序列化儲存為一個圖片檔案,自然需要用到圖片的編解碼庫:

將WritableBitmap儲存為圖片檔案

WriteableBitmap^ wb =“您的源”;IBuffer^ buffer = wb->PixelBuffer;DataReader^ dataReader = DataReader::FromBuffer(buffer);Array<unsigned char,1>^ fileContent =ref new Array<unsigned char,1>(buffer->Length);dataReader->ReadBytes(fileContent);task<StorageFile^>(ApplicationData::Current->LocalFolder->CreateFileAsync(pathName, CreationCollisionOption::ReplaceExisting)).then([this,fileContent,wb](StorageFile^ file){  task<IRandomAccessStream^>(file->OpenAsync(FileAccessMode::ReadWrite)).then([this,fileContent,wb](IRandomAccessStream^ _stream){
task<BitmapEncoder^>(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId,_stream)).then([this,fileContent,wb](BitmapEncoder^ encoder){ encoder->SetPixelData(BitmapPixelFormat::Bgra8,BitmapAlphaMode::Straight,wb->PixelWidth ,wb->PixelHeight,96,96,fileContent); task<void>(encoder->FlushAsync()).then([=](void) { //Jpeg File Save Success! }); }); });});

 將圖片檔案讀取到WritableBitmap對象

task<StorageFile^>  ReadTask(ApplicationData::Current->LocalFolder->GetFileAsync(filename));ReadTask.then([=](StorageFile^ sampleFile){task<IRandomAccessStream^>(sampleFile->OpenAsync(FileAccessMode::Read)).then([=](IRandomAccessStream^ readStream){      int nWidth = 163;      int nHeigh = 202;      WriteableBitmap^ bitmapSnap = ref new WriteableBitmap(nWidth,nHeigh);      readStream->Seek(0);     bitmapSnap->SetSource(readStream);});});

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.