How to create Random PictureViewer application using XAML




Here I will show how to create an application through which we can access pictures from computer drives.












 Code for name.cs

using System;
using Windows.Storage;
using Windows.UI.Xaml.Media.Imaging;
namespace RemoveUnwantedFilesfromMp3Folders.Models
{
    public class MyFile
    {
        public int Id { get; set; }
        public StorageFile StoreFile { get; set; }
        public string tittle { get; set; }
        public DateTimeOffset dateTaken { get; set; }
        public string cameralModel { get; set; }
        public string cameralManufacture { get; set; }
        public string Name { get; set; }
        public double latitude { get; set; }
        public double longtitude { get; set; }
        public bool Selected { get; set; }
        public bool used { get; set; }
       // public IObservable<StorageFile> FirstorDefault { get; set; }
        public BitmapImage Images;
    }
}



Application Code:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

using Windows.UI.Xaml.Media;
using System.Linq;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace RemoveUnwantedFilesfromMp3Folders
{
    /// <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 static string png = ".png";
        public static string jpg = ".jpg";
        private ObservableCollection<MyFile> obserfiles;
        private ObservableCollection<StorageFile> AllFiles;
        public MainPage()
        {
            this.InitializeComponent();
            obserfiles = new ObservableCollection<MyFile>();
        }
         private async Task RetrieveFilesFromFolders(ObservableCollection<StorageFile> list, StorageFolder parent)
        {
            foreach (var item in await parent.GetFilesAsync())
            {
                if (item.FileType == png || item.FileType == jpg)
                  list.Add(item);
             }
                foreach (var item in await parent.GetFoldersAsync())
             {
                await RetrieveFilesFromFolders(list, item);
             }

       }
        // Create  helper File remover
        private  async Task <List<StorageFile>>                                                                                                 NotMp3FilesAlreadyAddedto(ObservableCollection<StorageFile> allFiles)
        {
            Random random = new Random();
            var FileCounts = allFiles.Count;
            var NotMp3Files = new List<StorageFile>();

            while (NotMp3Files.Count <= FileCounts)
            {
               var RandomNumberNotMp3File = random.Next(FileCounts);
                var NotMp3File =  allFiles[RandomNumberNotMp3File];
         
       
                //Don't copy files 2wice
                //Don't add empty files.
              ImageProperties Notmp3ImageProperties = await                                                                                       NotMp3File.Properties.GetImagePropertiesAsync();
               /*  bool isDublicate = false;
               foreach (var file in NotMp3Files)
                {
                    var myfile = new MyFile();
                    ImageProperties fileImageProperties = await file.Properties.GetImagePropertiesAsync();
                    myfile.tittle = Notmp3ImageProperties.Title;
                    if (myfile.tittle == fileImageProperties.Title)
                         isDublicate = true;
                }
               if (!isDublicate)*/

                    NotMp3Files.Add(NotMp3File);
           }
   

            return NotMp3Files;
        }
 
        private async Task MoveNotMp3FilesTonewFolder(List<StorageFile> files)
        {
            int id = 0;
            //create a new path to move the files to.
            //
   
            foreach (var file in files)
            {
         
              ImageProperties fileImageProperties = await file.Properties.GetImagePropertiesAsync();
StorageItemThumbnail currentThumb = await                                                                                    file.GetThumbnailAsync(ThumbnailMode.ListView, 200,                                                                         ThumbnailOptions.UseCurrentScale);
                var images = new BitmapImage();
                images.SetSource(currentThumb);
                var myfile = new MyFile();
                myfile.Id = id;
                myfile.dateTaken = fileImageProperties.DateTaken;
                myfile.cameralModel = fileImageProperties.CameraModel;
                myfile.cameralManufacture = fileImageProperties.CameraManufacturer;
               // myfile.latitude = (double)fileImageProperties.Latitude;
                //myfile.longtitude = (double)fileImageProperties.Longitude;
                myfile.Images = images;
                myfile.StoreFile = file;
                obserfiles.Add(myfile);
                id++;
            }
        }
        private async Task<ObservableCollection<StorageFile>> setupPictureList()
        { // 1. Get access to picture library

            StorageFolder folder = KnownFolders.PicturesLibrary;
 var allFiles = new ObservableCollection<StorageFile>();
await RetrieveFilesFromFolders(allFiles, folder);
 return allFiles;
        }
        private async Task PrepareNewGame()
        {
              obserfiles.Clear();
            // 2. choose random picture from librabry
            var NotMp3Files = await NotMp3FilesAlreadyAddedto(AllFiles);

            // 3.Pluck off meta data from selected pictures
            await MoveNotMp3FilesTonewFolder(NotMp3Files);
        }

        private void HamburgerButton_Click(object sender, RoutedEventArgs e)
        {
            MySplitView.IsPaneOpen = !MySplitView.IsPaneOpen;
        }
 
        private async void LoadUpPicture_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (RandomlyPictureLoader.IsSelected)
            {
                SolidColorBrush brush = new SolidColorBrush(Colors.Blue);

                InstructionTextBlock.TextWrapping = TextWrapping.Wrap;
                InstructionTextBlock.Text = RandomPicloaderTextBlock.Text;
                InstructionTextBlock.Foreground = brush;
                InstructionTextBlock.FontSize = 50;
                Developer.TextWrapping = TextWrapping.Wrap;
                Developer.Text = "@Tolulope Check Out My Hotest Collections";
                startupProgresssRing.IsActive = true;

                AllFiles = await setupPictureList();

                await PrepareNewGame();
                RandomlyPictureLoader.IsSelected = true;
                startupProgresssRing.IsActive = false;
            }
            if (Clear.IsSelected)
            {
         
                    obserfiles.Clear();
                    SolidColorBrush brush = new SolidColorBrush(Colors.Red);
                    InstructionTextBlock.Text = ClearTextBlock.Text;
                    InstructionTextBlock.Foreground = brush;
                    InstructionTextBlock.FontSize = 50;
                    Developer.Text = string.Empty;
                    RandomlyPictureLoader.IsSelected = false;
                dateTakenTextBlock.Text = string.Empty;
                pictureNameTextBlock.Text = string.Empty;
                cameralManufactureTextBlock.Text = string.Empty;
                cameralModelTextBlock.Text = string.Empty;
            }
        }

       private void SongGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var clickedPicture = (MyFile)e.ClickedItem;// var correctPicture = from items in obserfiles where item.Selected ==    clickedPicture.Selected select item;
            foreach (var item in obserfiles)
            {
         
                //var correctedpicture = obserfiles.FirstOrDefault(itemp => itemp.Selected);
                if (item.Selected == clickedPicture.Selected)
                {
           
                    dateTakenTextBlock.Text = string.Format("Date Picture Was taken: {0}",  item.dateTaken);
                    pictureNameTextBlock.Text = string.Format("Name:{0}", item.Name);
                    cameralManufactureTextBlock.Text = string.Format("Camera Manufacture: {0}",          item.cameralManufacture);
                    cameralModelTextBlock.Text = string.Format("Camera Model:{0}",                                                     item.cameralModel);
                }
            }
         
       }

        private void InnerFlyoutButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
        {

        }
    }
}

Happy coding and hope it helps.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

New Post

New style string formatting in Python

In this section, you will learn the usage of the new style formatting. Learn more here . Python 3 introduced a new way to do string formatti...