所以我确实喜欢3小时的研究时间,却什么也没做。我在运行时将持有BitmapImage作为属性的对象。我想在此BitmapImage上写文本。如何使用C#和XAML做到这一点?
我读到一些有关能够构造WriteableBitmap和.render()方法的信息,但是我只在Windows.UI.Xaml.Media.Imaging命名空间中找到了WriteableBitmap类。
谁能阻止我摘录?
我找到了一种使图像中的文字起作用的好方法。我使用网格控件将一个Image和两个TextBox相互映射。确保先声明图像,这意味着先渲染图像。
<Grid Grid.Row="1" x:Name="Capture_Grid">
<Image Binding="{Binding Image}" />
<TextBox x:Name="UpperCaptionBorder_TextBox" Style="{StaticResource CaptionTextBoxStyle}"
Text="text" TextWrapping="Wrap" FontSize="32"
HorizontalAlignment="Center" VerticalAlignment="Bottom"
SelectionChanged="UpperCaption_TextBox_SelectionChanged"
/>
<TextBox x:Name="LowerCaptionBorder_TextBox" Style="{StaticResource CaptionTextBoxStyle}"
Text="text" TextWrapping="Wrap" FontSize="32"
HorizontalAlignment="Center" VerticalAlignment="Bottom"
SelectionChanged="LowerCaption_TextBox_SelectionChanged"
/>
</Grid>
然后,我可以将整个Grid控件另存为“ Scresn Shot”,但宽度和高度将仅与Grid实际一样。
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(Capture_Grid);
var pixels = await renderTargetBitmap.GetPixelsAsync();
Grid element = Capture_Grid;
//文件扩展名更正
var file = await KnownFolders.PicturesLibrary.CreateFileAsync("pic.png", CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await
BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
byte[] bytes = pixels.ToArray();
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
96, 96, bytes);
await encoder.FlushAsync();
}
它还将图像(网格+ 2个文本框)保存在图像中心中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句