我想在 Windows Phone 8.1 中将画布作为图像共享。为此,我首先将画布转换为图像,然后共享它。我尝试了我的 Windows 8.1 代码。没有发生错误,但共享源应用程序中没有图像,仅出现描述和标题。
这是代码:
private async void DataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
e.Request.Data.Properties.Title = "My app";
e.Request.Data.Properties.Description = "app description";
DataRequest request = e.Request;
// Request deferral to wait for async calls
DataRequestDeferral deferral = request.GetDeferral();
// XAML objects can only be accessed on the UI thread, and the call may come in on a background thread
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
try
{
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
// Render to an image at the current system scale and retrieve pixel contents
await renderTargetBitmap.RenderAsync(SavedCanvas);
var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
// Encode image to an in-memory stream
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
DisplayInformation.GetForCurrentView().LogicalDpi,
DisplayInformation.GetForCurrentView().LogicalDpi,
pixelBuffer.ToArray());
await encoder.FlushAsync();
request.Data.Properties.Thumbnail = RandomAccessStreamReference.CreateFromStream(stream);
// e.Request.Data.Properties.Thumbnail=(RandomAccessStreamReference.CreateFromStream(stream));
// Set content of the DataProviderRequest to the encoded image in memory
request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));
}
finally
{
deferral.Complete();
}
});
}
这在 Windows 8.1 中工作得很好,我认为在这里也应该工作得很好。在消息、OneNote 等共享应用程序中看不到图像。
需要帮助。谢谢。
您将位图传递给不支持位图的应用程序,则该位图将被忽略。通常需要发送位图文件。
您可以保存文件,然后加载该存储文件,也可以在内存中创建一个存储文件。
出于测试目的,我会将文件保存到 StorageFile,确保文件可以在应用程序中正确显示,然后确保它在共享时正常工作。
该示例可能会有所帮助。http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)