[英](UWP) Save Grid as png
我正在开发一个UWP应用程序,该应用程序具有带有孩子,图像和TextBlock的网格。 我有两件事想实现,需要帮助。
如何将Grid元素及其子内容另存为具有预定义名称的本地文件夹中的图像(最好是PNG)?
如何找回此保存的图像,然后将其作为附件包含在其中,以便与其他兼容应用程序共享?
例:
<StackPanel Orientation="Vertical">
<Grid Name="myGrid"
HorizontalAlignment="Stretch"
Background="Black">
<Image Name="myImage"
Source="Assets/image1.png"
Stretch="Uniform"/>
<TextBlock Name="myTextBlock"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextWrapping="Wrap"
Text="Sample user text"/>
</Grid>
<Button Name="saveButton"
Content="Save"
Margin="10,10,0,0"
Click="saveButton_Click" />
<Button Name="shareButton"
Content="Share"
Margin="10,10,0,0"
Click="shareButton_Click" />
</StackPanel>
编辑:在saveButton_Click事件中尝试了这段代码。 似乎不起作用。
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(myGrid);
var pixelBuffer = await rtb.GetPixelsAsync();
var pixels = pixelBuffer.ToArray();
var displayInformation = DisplayInformation.GetForCurrentView();
var stream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint)rtb.PixelWidth,
(uint)rtb.PixelHeight,
displayInformation.RawDpiX,
displayInformation.RawDpiY,
pixels);
await encoder.FlushAsync();
stream.Seek(0);
FileStream filestream = File.Create("C:\\Users\\pcUser\\Desktop\\testimage.png", (int)stream.Size);
EDIT2:尝试了这段代码。 似乎有些工作,但我只得到一个黑色图像,这可能是因为它仅保存myGrid,而不是孩子的内容。
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(quoteGrid);
var pixelBuffer = await rtb.GetPixelsAsync();
var pixels = pixelBuffer.ToArray();
var displayInformation = DisplayInformation.GetForCurrentView();
var stream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint)rtb.PixelWidth,
(uint)rtb.PixelHeight,
displayInformation.RawDpiX,
displayInformation.RawDpiY,
pixels);
await encoder.FlushAsync();
stream.Seek(0);
var wbm = new WriteableBitmap(rtb.PixelWidth, rtb.PixelHeight);
await wbm.SetSourceAsync(stream);
StorageFolder folder = ApplicationData.Current.LocalFolder;
if (folder != null)
{
StorageFile file = await folder.CreateFileAsync("testImage" + ".png", CreationCollisionOption.ReplaceExisting);
using (var storageStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, storageStream);
var pixelStream = wbm.PixelBuffer.AsStream();
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)wbm.PixelWidth, (uint)wbm.PixelHeight, displayInformation.RawDpiX, displayInformation.RawDpiY, new byte[pixelStream.Length]);
await encoder.FlushAsync();
}
}
如何将Grid元素及其子内容另存为具有预定义名称的本地文件夹中的图像(最好是PNG)?
我检查了您的代码。 您不需要使用WriteableBitmap
,只需将文件流传递给BitmapEncoder.CreateAsync
方法。 请检查以下代码以获取详细信息:
private async void saveButton_Click(object sender, RoutedEventArgs e)
{
RenderTargetBitmap rtb = new RenderTargetBitmap();
await rtb.RenderAsync(myGrid);
var pixelBuffer = await rtb.GetPixelsAsync();
var pixels = pixelBuffer.ToArray();
var displayInformation = DisplayInformation.GetForCurrentView();
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("testImage" + ".png", CreationCollisionOption.ReplaceExisting);
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint)rtb.PixelWidth,
(uint)rtb.PixelHeight,
displayInformation.RawDpiX,
displayInformation.RawDpiY,
pixels);
await encoder.FlushAsync();
}
}
如何找回此保存的图像,然后将其作为附件包含在其中,以便与其他兼容应用程序共享?
对于您的情况,您将图片保存在本地文件夹中。 因此,如果要获取它,则需要使用Storage API,例如:
var file = await ApplicationData.Current.LocalFolder.GetFileAsync("testImage.png");
关于“将其作为附件添加,以与其他兼容应用程序共享”,有关详细信息,您可以参考官方共享内容源应用程序示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.