gpt4 book ai didi

c# - Windows Phone 8.1 在图像上写文字

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:20 24 4
gpt4 key购买 nike

所以我确实喜欢 3 小时的研究,但还没有任何工作。我得到了在运行时将 BitmapImage 作为属性持有的对象。我想在这个 BitmapImage 上写文字。我如何使用 C# 和 XAML 做到这一点?

我阅读了一些关于能够构建 WriteableBitmap 和 .render() 方法的内容,但我只在 Windows.UI.Xaml.Media.Imaging 命名空间中找到了一个 WriteableBitmap 类。

谁能阻止我从一个片段开始?

最佳答案

我找到了一种让图片中的文字正常工作的好方法。我使用网格控件将一个图像和两个文本框相互映射。确保首先声明图像,这意味着它首先被渲染。

<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 个文本框)保存在图像中心。

关于c# - Windows Phone 8.1 在图像上写文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25066298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com