任何方式保存图像文件编程创建的UIElement? [英] Any way to save an UIElement created programmatically in an image file?
问题描述
我想保存在一款Windows Phone 8.1(C#)应用程序中的JPG / PNG / BMP图像编程方式创建的UIElement。
我使用使用方法RenderAsync(),但只适用于在XAML代码中创建用户界面元素的类RenderTargetBitmap。当我用它直接在C#创建用户界面元素我有这样的例外:System.ArgumentException(值没有在预期的范围内)
我做错,还是这个类不允许的UIElement(S)的渲染程序创建的?有没有办法做到这一点在Windows Phone 8.1吗? !谢谢
下面是我使用的代码:
私有静态异步无效RenderText(字符串文字,诠释的宽度,高度INT,INT字号,串imagename)
{
RenderTargetBitmap b =新RenderTargetBitmap();
变种帆布=新的Grid();
canvas.Width =宽度;
canvas.Height =高度;
VAR背景=新的Canvas();
background.Height =宽度;
background.Width =高度;
的SolidColorBrush背景色=新的SolidColorBrush(Colors.Red);
background.Background =背景色;
变种文本块=新的TextBlock();
textBlock.Text =文本;
textBlock.FontWeight = FontWeights.Bold;
textBlock.TextAlignment = TextAlignment.Left;
textBlock.HorizontalAlignment = HorizontalAlignment.Center;
textBlock.VerticalAlignment = VerticalAlignment.Stretch;
textBlock.Margin =新厚度(35);
//textBlock.Width = b.PixelWidth - textBlock.Margin.Left * 2;
textBlock.TextWrapping = TextWrapping.Wrap;
textBlock.Foreground =新的SolidColorBrush(Colors.White); //对瓷砖
textBlock.FontSize =字号文本的颜色;
canvas.Children.Add(文字块);
等待b.RenderAsync(背景);
等待b.RenderAsync(画布);
//获取像素
VAR pixelBuffer =等待b.GetPixelsAsync();
//获取本地文件夹。
StorageFolder本地= Windows.Storage.ApplicationData.Current.LocalFolder;
//创建一个新的文件夹名称DataFolder。
VAR dataFolder =等待local.CreateFolderAsync(DataFolder,
CreationCollisionOption.OpenIfExists);
StorageFile文件=等待dataFolder.CreateFileAsync(imagename,CreationCollisionOption.ReplaceExisting);
//使用编码图像磁盘上
选定的文件(VAR FILESTREAM =等待file.OpenStreamForWriteAsync())
{
变种编码=等待BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId,fileStream.AsRandomAccessStream());
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(UINT)b.PixelWidth,
(UINT)b.PixelHeight,
DisplayInformation.GetForCurrentView()LogicalDpi,
DisplayInformation.GetForCurrentView()LogicalDpi,
pixelBuffer.ToArray())。
等待encoder.FlushAsync();
}
}
据不与XAML创建的元素,但这些只工作(如 MSDN说: )在的VisualTree
:
渲染的快照
的UIElement
可视化树以图像源。
块引用>
所以,如果你比如添加元素到当前页面的方法将工作:
LayoutRoot.Children.Add(画布);
I'm trying to save an UIElement created programmatically in a JPG/PNG/BMP image in a Windows Phone 8.1 (C#) application.
I'm using the class RenderTargetBitmap using the method RenderAsync() but it only works with UI elements created in the XAML code. When I use it on UI elements created directly in C# I have this exception: "System.ArgumentException (Value does not fall within the expected range.)"
Am I doing something wrong or this class doesn't allow rendering of UIElement(s) created programmatically? Is there any way to do this on Windows Phone 8.1? Thank you!
Here's the code I use:
private static async void RenderText(string text, int width, int height, int fontsize, string imagename) { RenderTargetBitmap b = new RenderTargetBitmap(); var canvas = new Grid(); canvas.Width = width; canvas.Height = height; var background = new Canvas(); background.Height = width; background.Width = height; SolidColorBrush backColor = new SolidColorBrush(Colors.Red); background.Background = backColor; var textBlock = new TextBlock(); textBlock.Text = text; textBlock.FontWeight = FontWeights.Bold; textBlock.TextAlignment = TextAlignment.Left; textBlock.HorizontalAlignment = HorizontalAlignment.Center; textBlock.VerticalAlignment = VerticalAlignment.Stretch; textBlock.Margin = new Thickness(35); //textBlock.Width = b.PixelWidth - textBlock.Margin.Left * 2; textBlock.TextWrapping = TextWrapping.Wrap; textBlock.Foreground = new SolidColorBrush(Colors.White); //color of the text on the Tile textBlock.FontSize = fontsize; canvas.Children.Add(textBlock); await b.RenderAsync(background); await b.RenderAsync(canvas); // Get the pixels var pixelBuffer = await b.GetPixelsAsync(); // Get the local folder. StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; // Create a new folder name DataFolder. var dataFolder = await local.CreateFolderAsync("DataFolder", CreationCollisionOption.OpenIfExists); StorageFile file = await dataFolder.CreateFileAsync(imagename, CreationCollisionOption.ReplaceExisting); // Encode the image to the selected file on disk using (var fileStream = await file.OpenStreamForWriteAsync()) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream.AsRandomAccessStream()); encoder.SetPixelData( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)b.PixelWidth, (uint)b.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } }
解决方案It is not working only with elements created in XAML but with these that (as MSDN says) are in
VisualTree
:Renders a snapshot of a
UIElement
visual tree to an image source.So your method will work if you add your elements for example to your current Page:
LayoutRoot.Children.Add(canvas);
这篇关于任何方式保存图像文件编程创建的UIElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!