保存WriteableBitmap图片
关于保存图片的问题。 在程序中使用的图片很经常会是位图的格式, 可是在metro中如磁铁等 是不支持位图格式的所有需要转换。 可以通过WriteableBitmap 的方式把位图保存为jpg格式,然后再使用。
WriteableBitmap^ wb = (WriteableBitmap^)img_pdfpage->Source;//必须保证你的图片是通过WriteableBitmap获取的 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^>(KnownFolders::DocumentsLibrary->CreateFileAsync("1.jpg", 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) { //BitmapPixelFormat::Rgba8这个值根据具体情况选择
encoder->SetPixelData(BitmapPixelFormat::Rgba8,BitmapAlphaMode::Straight,wb->PixelWidth ,wb->PixelHeight,96,96,fileContent); encoder->FlushAsync(); /* task<void>(encoder->FlushAsync()).then([this](void) { }); */ }); }); });