Windows 8 学习笔记(二十三)--WritableBitmap的读写操作C++
在开发中,涉及图片的处理经常需要用到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); }); });
posted on 2012-12-27 14:42 ShinyTang 阅读(2343) 评论(1) 编辑 收藏 举报