我正在尝试用 C++ 编写 WP 应用程序,并且在模糊 .png 图像时遇到了一个奇怪的问题。它有时有效,有时无效。当它不起作用时,它看起来好像图像是不可见的。只是为了确保我在 C# 中实现了同样的东西并且它工作得很好。这是我的 C# 代码
private async void blurDefaultAvatar() {
try {
var storageFile = await Package.Current.InstalledLocation.GetFileAsync("Assets\\menu_user.png");
using(var imgStream = await storageFile.OpenReadAsync()) {
using(var stream = await getBlurredImageStreamWithStream(imgStream, CanvasBitmapFileFormat.Png)) {
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
blurredAvatar.Source = bitmap;
}
}
} catch(Exception e) {
System.Diagnostics.Debug.WriteLine("Avatar load fail: {0}", e.Message);
}
}
private async Task<IRandomAccessStream> getBlurredImageStreamWithStream(IRandomAccessStream stream, CanvasBitmapFileFormat format) {
try {
var device = new CanvasDevice();
var bitmap = await CanvasBitmap.LoadAsync(device, stream);
var renderer = new CanvasRenderTarget(device, bitmap.SizeInPixels.Width, bitmap.SizeInPixels.Height, bitmap.Dpi);
using(var ds = renderer.CreateDrawingSession()) {
var blur = new GaussianBlurEffect();
blur.BlurAmount = 30.0f;
blur.Source = bitmap;
ds.DrawImage(blur);
}
var imgStream = new InMemoryRandomAccessStream();
await renderer.SaveAsync(imgStream, format);
return imgStream;
} catch(Exception e) {
System.Diagnostics.Debug.WriteLine("Avatar blur fail: {0}", e.Message);
return null;
}
}
在 C++ 中或多或少(我希望)等价
void MainPage::blurDefaultAvatar(){
concurrency::create_task(Package::Current->InstalledLocation->GetFileAsync(L"Assets\\menu_user.png")).then([](concurrency::task<StorageFile^> t){
try{
auto storageFile = t.get();
return concurrency::create_task(storageFile->OpenReadAsync());
} catch(Exception^ e){
std::wstringstream wss;
wss<<"\nAvatar not found: '"<<e->Message->Data()<<"'\n";
OutputDebugString(wss.str().c_str());
return concurrency::create_task(concurrency::create_async([]()->IRandomAccessStreamWithContentType^{ return nullptr; }));
}
}, concurrency::task_continuation_context::use_current()).then([this](concurrency::task<IRandomAccessStreamWithContentType^> t){
try{
auto imgStream = t.get();
concurrency::create_task(getBlurredImageStreamWithStream(imgStream, CanvasBitmapFileFormat::Png)).then([this](IRandomAccessStream^ stream){
if(stream!=nullptr && stream->Size>0){
auto bitmap = ref new BitmapImage();
bitmap->SetSource(stream);
blurredAvatar->Source = bitmap;
}
});
} catch(Exception^ e){
std::wstringstream wss;
wss<<"\nAvatar failed to read: '"<<e->Message->Data()<<"'\n";
OutputDebugString(wss.str().c_str());
}
});
}
IAsyncOperation<IRandomAccessStream^>^ MainPage::getBlurredImageStreamWithStream(IRandomAccessStream^ stream, CanvasBitmapFileFormat format){
return concurrency::create_async([stream, format]() -> IRandomAccessStream^{
auto imgStream = ref new InMemoryRandomAccessStream();
auto device = ref new CanvasDevice();
return concurrency::create_task(CanvasBitmap::LoadAsync(device, stream)).then([stream, device, format, imgStream](concurrency::task<CanvasBitmap^> t){
try {
auto bitmap = t.get();
auto renderer = ref new CanvasRenderTarget(device, bitmap->SizeInPixels.Width, bitmap->SizeInPixels.Height, bitmap->Dpi);
auto ds = renderer->CreateDrawingSession();
auto blur = ref new GaussianBlurEffect();
blur->BlurAmount = 30.0f;
blur->Source = bitmap;
ds->DrawImage(blur);
return concurrency::create_task(renderer->SaveAsync(imgStream, format));
} catch(Exception^ e){
std::wstringstream wss;
wss<<"\nBitmap load fail: '"<<e->Message->Data()<<"'\n";
OutputDebugString(wss.str().c_str());
return concurrency::create_task(concurrency::create_async([]()->void{}));
}
}, concurrency::task_continuation_context::use_current()).then([imgStream](concurrency::task<void> t){
try{
t.get();
return imgStream;
} catch(Exception^ e){
std::wstringstream wss;
wss<<"\nStream save fail: '"<<e->Message->Data()<<"'\n";
OutputDebugString(wss.str().c_str());
return (InMemoryRandomAccessStream^)nullptr;
}
}).get();
});
}
通过按下按钮调用方法。知道可能出了什么问题吗?