public void removeImage(Media imageToRemove)
 {
     Post post = DataContext as Post;
     post.Media.Remove(imageToRemove);
     foreach (var el in imageWrapPanel.Children)
     {
         if ((el as FrameworkElement).Tag == imageToRemove)
         {
             imageWrapPanel.Children.Remove(el);
             break;
         }
     }
     ToggleGalleryControlsVisibility();
 }
 private Button BuildTappableImageElement(BitmapImage image, Media currentMedia)
 {
     Button imageOuterButton = new Button();
     imageOuterButton.Tag = currentMedia;
     imageOuterButton.Tap += sp_Tap;
     float width = 180F;
     int height = (int)(width / image.PixelWidth * image.PixelHeight);
     imageOuterButton.Width = width;
     imageOuterButton.Height = height;
     Style btnStyle = App.Current.Resources["BasicButtonStyle"] as Style;
     imageOuterButton.Style = btnStyle;
     imageOuterButton.Background = new ImageBrush { ImageSource = image };
     return imageOuterButton;
 }
 private void sp_Tap(object sender, System.Windows.Input.GestureEventArgs e)
 {
     Control tappedObj = (Control)sender;
     Media currentMedia = (Media)tappedObj.Tag;
     _lastTappedMedia = currentMedia;
     NavigationService.Navigate(new Uri("/ImageDetailsPage.xaml", UriKind.Relative));
 }
        private void AddNewMediaStream(Stream bitmapStream, string originalFilePath)
        {
            BitmapImage image = new BitmapImage();
            try
            {
                image.SetSource(bitmapStream);
            }
            catch (Exception)
            {
                MessageBoxResult result = MessageBox.Show(_localizedStrings.Prompts.MediaErrorPictureCorruptedOrUnreadable, _localizedStrings.PageTitles.Error, MessageBoxButton.OK);
                return;
            }

            // Save to isolated storage.
            // The OriginalFilename is a GUID.  Since we're saving to IsolatedStorage, use this as a filename to avoid collisions.
            string localfilename = Path.Combine(Media.MEDIA_IMAGE_DIRECTORY, Path.GetFileName(originalFilePath));
            DateTime capture = DateTime.Now;
            string fileNameFormat = "SavedPicture-{0}{1}{2}{3}{4}{5}{6}"; //year, month, day, hours, min, sec, file extension
            string filename = string.Format(fileNameFormat,
                capture.Year,
                capture.Month,
                capture.Day,
                capture.Hour,
                capture.Minute,
                capture.Second,
                Path.GetExtension(originalFilePath));

            Media currentMedia = null;
            try
            {
                currentMedia = new Media(App.MasterViewModel.CurrentBlog, filename, localfilename, bitmapStream, App.MasterViewModel.CurrentBlog.PreserveBandwidth);
            }
            catch (Exception)
            {
                MessageBoxResult result = MessageBox.Show(_localizedStrings.Prompts.MediaErrorCannotWritePicture, _localizedStrings.PageTitles.Error, MessageBoxButton.OK);
                return;
            }
            currentMedia.CanBeFeatured = false;
            Post post = App.MasterViewModel.CurrentPost;
            post.Media.Add(currentMedia);

            //update the UI
            imageWrapPanel.Children.Add(BuildTappableImageElement(image, currentMedia));
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (e.Content is ImageDetailsPage)
            {
                (e.Content as ImageDetailsPage).TappedImage = _lastTappedMedia;
                _lastTappedMedia = null;
            }

            base.OnNavigatedFrom(e);

            //store transient data in the State dictionary
            if (State.ContainsKey(TITLEKEY_VALUE))
            {
                State.Remove(TITLEKEY_VALUE);
            }
            State.Add(TITLEKEY_VALUE, titleTextBox.Text);

            Post post = App.MasterViewModel.CurrentPost;
            if (post != null && post.Gallery.Enabled)
            {
                uploadImagesAsGalleryCheckbox.IsChecked = true;
               }
        }
        //called from ImageDetailsPage
        public void UpdateFeaturedImage(Media featuredImage)
        {
            if (featuredImage.IsFeatured)
            {
                foreach (Media m in App.MasterViewModel.CurrentPost.Media)
                {
                    m.IsFeatured = m.Equals(featuredImage);
                }
            }

            //update the UI
            foreach (Canvas c in imageWrapPanel.Children)
            {
                Image img = (Image)c.Children[1];
                Media current_media = (Media)c.Tag;
                if (current_media.IsFeatured)
                {
                    img.Visibility = Visibility.Visible;
                }
                else
                {
                    img.Visibility = Visibility.Collapsed;
                }
            }
        }
        private Canvas BuildTappableImageElement(BitmapImage image, Media currentMedia)
        {
            Button imageOuterButton = new Button();
            imageOuterButton.Tag = currentMedia;
            imageOuterButton.Tap += sp_Tap;
            float width = 180F;
            int height = (int)(width / image.PixelWidth * image.PixelHeight);
            imageOuterButton.Width = width;
            imageOuterButton.Height = height;
            Style btnStyle = App.Current.Resources["BasicButtonStyle"] as Style;
            imageOuterButton.Style = btnStyle;
            imageOuterButton.Background = new ImageBrush { ImageSource = image };

            Image img = new Image();
            img.Source = new BitmapImage(new Uri("/Images/star.png", UriKind.Relative));
            img.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            img.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
            img.Width = 30;
            img.Height = 30;
            if (!currentMedia.IsFeatured)
            {
                img.Visibility = Visibility.Collapsed;
            }

            Canvas canvas = new Canvas();
            canvas.Width = width;
            canvas.Height = height;
            canvas.Children.Add(imageOuterButton);
            canvas.Children.Add(img);
            canvas.Tag = currentMedia;

            Canvas.SetTop(img, (height - 45));
            Canvas.SetLeft(img, (width - 45));

            return canvas;
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (e.Content is ImageDetailsPage)
            {
                (e.Content as ImageDetailsPage).TappedImage = _lastTappedMedia;
                _lastTappedMedia = null;
            }
            else if (e.Content is BlogPanoramaPage)
            {
                //remove the listers!
                postFormatsPicker.SelectionChanged -= listPicker_SelectionChanged;
                postFormatsPicker.ItemsSource = null;
            }

            if (State.ContainsKey(TITLEKEY_VALUE))
            {
                State.Remove(TITLEKEY_VALUE);
            }
            State.Add(TITLEKEY_VALUE, titleTextBox.Text);

            if (State.ContainsKey(TAGSKEY_VALUE))
            {
                State.Remove(TAGSKEY_VALUE);
            }
            State.Add(TAGSKEY_VALUE, tagsTextBox.Text);

            base.OnNavigatedFrom(e);
        }