【问题标题】:Windows Phone 8.1 write Text on an ImageWindows Phone 8.1 在图像上写入文本
【发布时间】:2014-07-31 18:24:16
【问题描述】:

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

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

谁能阻止我从sn-p开始?

【问题讨论】:

  • 在 Windows 8 中,要实际更改 WritableBitmap,您必须获取底层流并手动取消引用正确位置以设置像素。类似的东西可能适用于 WP8。我记得有一个库 WritableBitmapEx 以更友好的方式公开了这些操作,但我似乎无法在 atm 找到它。

标签: c# xaml windows-phone-8.1


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多