我正在開發一個UWP應用程序,該應用程序具有帶childs,Image和TextBlock的網格。我有兩件事情我想實現並需要幫助。(UWP)將網格另存爲PNG
如何使用預定義名稱將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);
編輯2:試了這一點的代碼。這似乎有點工作,但我只得到一個黑色的圖像,這可能是因爲它只是保存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();
}
}
看起來您可以使用Windows.UI.Xaml.Media.Imaging命名空間中的RenderTargetBitmap類執行第1步。本指南的第一部分很好地解決了這個問題:http://metulev.com/render-xaml- to-image-and-more/ –
@LincolnGreen那麼我該如何保存RenderTargetBitmap對象?我對Windows編程非常陌生。 –
只需將流保存到文件中 – chris579