【问题标题】:How to draw in Windows Phone 8.1 on WriteableBitmap?如何在 Windows Phone 8.1 上的 WriteableBitmap 上绘图?
【发布时间】:2015-09-22 05:56:57
【问题描述】:

我正在尝试使用绘图板制作应用程序。我选择WriteableBitmapEx 用于我的目的并开始开发。我遵循了这个库官方网站上的简短教程,但不幸的是我无法在我的位图上使用SetPixel(...) 进行绘制。谁能帮我解决一下?

XAML 代码:

<Page
x:Class="WritableBitmapApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WritableBitmapApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <Image x:Name="ImageControl"
           Source="Hobbit.png"
           Tapped="ImageControl_Tapped"
           Height="512"
           Width="350"/>
</Grid>

以及 C# 代码:

public sealed partial class MainPage : Page
{
    public WriteableBitmap writeableBmp;

    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;

        writeableBmp = BitmapFactory.New(512, 350);
        writeableBmp.Clear(Colors.Black);
        ImageControl.Source = writeableBmp;
        writeableBmp.GetBitmapContext();
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.
    /// This parameter is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        // TODO: Prepare page for display here.

        // TODO: If your application contains multiple pages, ensure that you are
        // handling the hardware Back button by registering for the
        // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
        // If you are using the NavigationHelper provided by some templates,
        // this event is handled for you.
    }

    private void ImageControl_Tapped(object sender, TappedRoutedEventArgs e)
    {
        var pnt = e.GetPosition(ImageControl);

        try
        {
            writeableBmp.DrawLine(0, 0, (int)pnt.X, (int)pnt.Y, Colors.White);
            ImageControl.Source = writeableBmp;
            writeableBmp.GetBitmapContext();
            writeableBmp.Invalidate();
            ImageControl.Source = writeableBmp;
        }
        catch(Exception ex)
        {

        }
    }
}

【问题讨论】:

  • 首先,你应该严格避免空的catch块。
  • 我只用它来通过调试器查找任何异常

标签: c# xaml windows-phone-8.1 writeablebitmapex


【解决方案1】:

您没有处理 BitmapContext。试试这样:

using (writeableBmp.GetBitmapContext()) 
{
   writeableBmp.Clear();
   writeableBmp.DrawLine(0, 0, (int)pnt.X, (int)pnt.Y, Colors.White);   
} // Invalidates on exit of using block

我还建议您查看 WriteableBitmapEx 提供的示例: https://writeablebitmapex.codeplex.com/SourceControl/latest 特别是曲线样本已经使用指针输入实现了点的绘制。它绘制样条曲线,但您可以根据需要进行调整。 \trunk\Source\WriteableBitmapExCurvesSample.WinRT\WriteableBitmapExCurvesSample.WinRT.csproj

【讨论】:

  • 看看样本。如果您在那里查看并复制代码会更容易。样品包含您需要的一切。比猜测代码和 XAML 的其他部分更容易。谁知道也许你在白色背景上用白色画。 ;)
  • 我使用黑色背景和白色。这些示例适用于 WP 8,而在 WP 8.1 中,一些方法如“FromResource(paath_to_image)”不存在。
  • 检查 .WinRT 示例。这些适用于 Windows 应用商店应用程序,但它是相同的 WinRT API 层。例如:WriteableBitmapExCurvesSample.WinRT
猜你喜欢
  • 2016-05-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
相关资源
最近更新 更多