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  阅读(2338)  评论(1编辑  收藏  举报

导航

作者:LucyTangLucyTang's Blog on 博客园
出处:http://www.cnblogs.com/jing870812/

本作品由LucyTang创作,采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问或者授权方面的协商,请给我留言