private async void RunTest()
 {
     await this.WaitForLoadedAsync();
     var wb = new WriteableBitmap(1, 1);
     await wb.Render(this.source);
     this.target.Source = wb;
 }
        public async void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            /* TO DO (and uncomment async above):
            if (_writeableBitmap != null)
            {
                var picker = new FileSavePicker {SuggestedStartLocation = PickerLocationId.PicturesLibrary};
                picker.FileTypeChoices.Add("Image", new List<string> { ".png" });
                picker.DefaultFileExtension = ".png";
                picker.SuggestedFileName = "photo";
                var savedFile = await picker.PickSaveFileAsync();

                try
                {
                    if (savedFile != null)
                    {
                        using (var output = await
                            savedFile.OpenAsync(FileAccessMode.ReadWrite))
                        {
                            var encoder =
                                await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, output);

                            byte[] pixels;

                            using (var stream = _writeableBitmap.PixelBuffer.AsStream())
                            {
                                pixels = new byte[stream.Length];
                                await stream.ReadAsync(pixels, 0, pixels.Length);
                            }

                            encoder.SetPixelData(BitmapPixelFormat.Rgba8,
                                                    BitmapAlphaMode.Straight,
                                                    (uint)_writeableBitmap.PixelWidth,
                                                    (uint)_writeableBitmap.PixelHeight,
                                                    96.0, 96.0,
                                                    pixels);

                            await encoder.FlushAsync();
                            await output.FlushAsync();
                        }
                    }
                }
                catch (Exception ex)
                {
                    var s = ex.ToString();
                }
                finally
                {
                    CheckAndClearShareOperation();
                }
            } */
#if PROBLEM
            if(_writeableBitmap != null)
            {
                using (MemoryStream ms = new MemoryStream())
                {

                    // Convert image to byte[]
                    byte[] imgB = ConvertBitmapToByteArray(_writeableBitmap);
                    string base64image = Convert.ToBase64String(imgB);
                    App.CurrentPatient.ImageEncoded = base64image;
                }
                App.CurrentPatient.ImageName = App.CurrentPatient.FormatImageName();
            }
#endif
#if SECONDTRY
            if(_writeableBitmap != null)
            {
                //App.CurrentPatient.ImageBitmap.SetSource(_bitmap);
                MemoryStream ms = _writeableBitmap.PixelBuffer.AsStream().AsOutputStream();
                App.CurrentPatient.ImageBitmap.SetSource(s);
                App.CurrentPatient.ImageName = App.CurrentPatient.FormatImageName();
            }
#endif
            if (_writeableBitmap != null)
            {
                if(!String.IsNullOrEmpty(CaptionTextBox.Text))
                {   // Added Release 7.
                    // Adapted from https://social.msdn.microsoft.com/Forums/windowsapps/en-US/3af5874c-3d40-486e-a592-2b2044fb377b/problem-with-rendertargetbitmap-?forum=winappswithcsharp
                    var captionedBitmap = new  RenderTargetBitmap();
                    await captionedBitmap.RenderAsync(elementToRender);
                    //image.Source = bitmap;
                    ///IBuffer pixelBuffer = await bitmap.GetPixelsAsync();
                    ///byte[] pixels = pixelBuffer.ToArray();
                    var pixelBuffer = await captionedBitmap.GetPixelsAsync();
                    byte[] pixels = pixelBuffer.ToArray();
                    // Evidently we have to do the next 2 lines, even tho captioned bitmap is same size as uncaptioned:
                    _writeableBitmap = null;
                    _writeableBitmap = new WriteableBitmap((int)captionedBitmap.PixelWidth, (int)captionedBitmap.PixelHeight);
                    using (Stream stream = _writeableBitmap.PixelBuffer.AsStream())
                    {
                        await stream.WriteAsync(pixels, 0, pixels.Length);
                    }
#if FOR_REFERENCE
                    // If this was Win Phone, traditional way to do the same thing is something like...
                    // Adapted from http://bsubramanyamraju.blogspot.in/2014/03/windows-phone-imagemergings-merging.html 
                    TextBlock tb = new TextBlock();
                    tb.Text = CaptionTextBox.Text;
                    tb.TextWrapping = TextWrapping.Wrap;
                    tb.Foreground = new SolidColorBrush(Colors.Black);
                    tb.FontSize = 70;
                    tb.FontWeight = FontWeights.Bold;
                     _writeableBitmap.Render(tb, new TranslateTransform() { X = 25, Y = 191 });
                    _writeableBitmap.Invalidate();
#endif
                }
                //App.CurrentPatient.ImageBitmap.SetSource(_bitmap);
                App.CurrentPatient.ImageWriteableBitmap = _writeableBitmap;
                App.CurrentPatient.ImageName = App.CurrentPatient.FormatImageName();
                //done later: App.CurrentPatient.ImageEncoded = await App.CurrentPatient.FormatImageEncoded();
                App.CurrentPatient.ImageEncoded = ""; // clear out any color swatch beginning with Assets, we have a real image
                if (App.CurrentPatient.SentCode != "" && !App.CurrentPatient.ObjSentCode.IsQueued()) // could look at WhenLocalTime instead
                    App.ReportAltered = true; // tell caller the image changed (though App.ReportAltered might already be true due to other field changes)
            }
            base.GoBack(sender, e);
#if WAS_BUT_DOESNT_LEAVE_NAV_TRAIL_RIGHT
/* WAS:
            if (App.CurrentPatient.SentCode == "Y") // could look at WhenLocalTime instead
            {
                if (_writeableBitmap != null)
                    App.ReportAltered = true; // tell caller the image changed (though App.ReportAltered might already be true due to other field changes)
                this.Frame.Navigate(typeof(BasicPageViewEdit), "pageViewEditReport");
            }
            else
                this.Frame.Navigate(typeof(BasicPageNew), "pageNewReport"); */
            if (App.CurrentPatient.SentCode == "" || App.CurrentPatient.ObjSentCode.IsQueued()) // could look at WhenLocalTime instead
               this.Frame.Navigate(typeof(BasicPageNew), "pageNewReport");
            else 
            {
                if (_writeableBitmap != null)
                    App.ReportAltered = true; // tell caller the image changed (though App.ReportAltered might already be true due to other field changes) //<<<Moved to above
                this.Frame.Navigate(typeof(BasicPageViewEdit), "pageViewEditReport");
            }
#endif
        }