Skip to main content
 首页 » 编程设计

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

2024年02月06日12lvdongjie

所以我确实喜欢 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 个文本框)保存在图像中心。