| 
 在开发中,涉及图片的处理经常需要用到WritableBitmap对象,这个对象可以直接作为数据源赋值给Image控件,但若要保存这个对象,我们只能将其序列化保存为一个图片文件,自然需要用到图片的编解码库:  
将WritableBitmap保存为图片文件  
 
 
 
WriteableBitmap^ wb =“您的源”; 
IBuffer^ buffer = wb->PixelBuffer; 
DataReader^ dataReader = DataReader::FromBuffer(buffer); 
Array^ fileContent =ref new Array(buffer->Length); 
dataReader->ReadBytes(fileContent); 
task(ApplicationData::Current->LocalFolder->CreateFileAsync(pathName, CreationCollisionOption::ReplaceExisting)).then([this,fileContent,wb](StorageFile^ file) 
{   
task(file->OpenAsync(FileAccessMode::ReadWrite)).then([this,fileContent,wb](IRandomAccessStream^ _stream) 
{ 
task(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(encoder->FlushAsync()).then([=](void) 
{ 
//Jpeg File Save Success! 
}); 
}); 
}); 
}); 
 
  将图片文件读取到WritableBitmap对象 
 
 
 
task  ReadTask(ApplicationData::Current->LocalFolder->GetFileAsync(filename)); 
ReadTask.then([=](StorageFile^ sampleFile) 
{ 
task(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); 
}); 
}); 
 
   |