Windows 8 学习笔记(二十三)--WritableBitmap的读写操作C++
在开发中,涉及图片的处理经常需要用到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((StorageFile^ file)
{
task(file->OpenAsync(FileAccessMode::ReadWrite)).then((IRandomAccessStream^ _stream)
{
task(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId,_stream)).then((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对象
taskReadTask(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);
});
});
页:
[1]