I
I
ichernob2015-09-21 18:56:41
Windows phone
ichernob, 2015-09-21 18:56:41

Why aren't changes applied to WritableBitmap?

I'm trying to figure out WritableBitmap, but I ran into a problem. When the ImageControl_Tapped event is raised, nothing happens. BUT if you create a bitmap directly in the event method (similar to how it is done in MainPage (), then the changes are visible, but I need to see all the changes made, and not the last one. What's wrong?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace WritableBitmapApp
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    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)
            {

            }
        }
    }
}

<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>
</Page>

Answer the question

In order to leave comments, you need to log in

Similar questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question