Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

UWP Code: Fotos von Smartphone lesen

23.12.2020 (👁15472)

UWP Code: Fotos von Smartphone lesen

 

Dieser Beitrag zeigt den C# Code für eine Windows 10 Desktop Anwendung, welche die Fotos und Videos aus einem Smartphone liest und anzeigt.

Dabei ist das Smartphone über ein USB Kabel mit dem Computer verbunden.

Die Anwendung funktioniert auch im Standby Lockin Bildschirm des Smartphones.

 

Was macht die Windows App:

Die Anwendung liest unter Removable Devices das Phone oder DCIM Verzeichnis.

Dann wird der Ordner: Camera gesucht und alle Files im Ordner angezeigt. Die Files enthalten den Contenttype Video oder Image.

 

C#  und Xaml Code

 

<UserControl

    x:Class="UserControls.UcFile"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:UserControls"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="100"

    d:DesignWidth="100" Loading="UserControl_Loading" Loaded="UserControl_Loaded">

    <Grid Width="100" Height="100" BorderBrush="Red" BorderThickness="1">

        <Image x:Name="tnImage" Stretch="UniformToFill" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ></Image>

    </Grid>

</UserControl>

 

 

 

Mainpage.xaml

<Page

    x:Class="Smartphone_Imagefolder_lesen.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:Smartphone_Imagefolder_lesen"

    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 x:Name="MainGrid">

        <Grid.RowDefinitions>

            <RowDefinition Height="50"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <!-- Row_0 -->

        <Button x:Name="BtnGetDevices" Grid.Row="0" Click="BtnGetDevices_Click" VerticalAlignment="Stretch" >GetFolders</Button>

        <!--/ Row_0 -->

       

        <!--Image_Grid-->

        <ScrollViewer Grid.Row="1" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" >

            <GridView x:Name="ctlFiles" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" 

                      IsMultiSelectCheckBoxEnabled="True"  

                      SelectionMode="Single"                       

                      >

            </GridView>

        </ScrollViewer>

        <!--/ Image_Grid-->

    </Grid>

</Page>

 

 

using System;

using Windows.UI.Xaml;          //EventArgs

using Windows.UI.Xaml.Controls; //Page

using Windows.Storage.Pickers;

using Windows.Storage;

using Windows.Storage.AccessCache;

using System.Threading.Tasks;       //Tasks

using System.Linq;                  //FirstOrDefault()

using System.Collections.Generic;   //IReadOnlyList

using UserControls;      //UserControls

namespace Smartphone_Imagefolder_lesen

{

    /// <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 MainPage()

        {

            this.InitializeComponent();

        }

        private async void BtnGetDevices_Click(object sender, RoutedEventArgs e)

        {

            //get camera folder

            StorageFolder cameraFolder = await fxGetCameraFolder();

            if (cameraFolder != null)

            {

                //*read photos+videos

                fxReadFiles_inFolder(cameraFolder);

            }

        }

        public async Task<StorageFolder> fxGetCameraFolder()

        {

            //------------< fxGetCameraFolder()------------

            StorageFolder cameraFolder = null;

            // Get the logical root folder for all external storage devices.

            StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;

            // Get the first child folder, which represents the SD card.

            StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault();

            if (sdCard != null)

            {

                //-----< Is sdCard >-----

                //*get Smartphone as drive.

                //*smartphone has to be connected and unlocked

                IReadOnlyList<StorageFolder> folderList = await sdCard.GetFoldersAsync();

                //----< @loop: Smartphone folders >----

                //*read folder and search for /DCIM/camera

                foreach (StorageFolder folder in folderList)

                {

                    //---< check smartphone Folder >----

                    String sFoldername = folder.Name;

                    Console.WriteLine( "Smartphone Folder:" + sFoldername);

                    //--< 1.fast: look for /DCIM/camera >--

                    Console.WriteLine("read DCIM");

                   

                    StorageFolder folderDCIM = await folder.GetFolderAsync("DCIM");

                    if (folderDCIM != null)

                    {

                        //-< lookup: camera folder >-

                        Console.WriteLine("read camera folder");

                        cameraFolder = await folderDCIM.GetFolderAsync("camera");

                        if (cameraFolder != null)

                        {

                            break; //*stop searchloop

                        }

                        //-</ lookup: camera folder >-

                    }

                    //--</ 1.fast: look for /DCIM/camera >--

                    //--< 2.slow: loop for /DCIM/camera >--

                    foreach (StorageFolder subFolder in folderList)

                    {

                        if (subFolder.Name == "DCIM")

                        {

                            //--< @Loop: Search camera >--

                            foreach (StorageFolder subSubFolder in folderList)

                            {

                                //---</ check camera folder >----

                                if (folder.Name == "camera")

                                {

                                    //< IsCamera folder >

                                    cameraFolder = folder;

                                    break;

                                    //</ IsCamera folder >

                                }

                                //---< check camera folder >----

                            };

                            //--</ @Loop: Search camera >--

                        }

                        //--</ 2.slow: loop for /DCIM/camera >--

                    }

                    //---</ check smartphone Folder >----

                }

                //----</ @loop: Smartphone folders >----

                //-----</ is sdCard >-----

            }

            return cameraFolder;

            //------------</ fxGetCameraFolder()------------

        }

        public async void fxReadFiles_inFolder(StorageFolder folder)

        {

            //------------< fxReadFiles_inFolder()------------

            ctlFiles.Items.Clear();

            //< check >

            if (folder == null) return;

            //</ check >

            //----< @Loop: Files in Folder >----

            foreach (StorageFile file in await folder.GetFilesAsync())

            {

                //--< File >--

                Console.WriteLine(file.DisplayName);

                GridViewItem item = new GridViewItem();

                item.Margin = new Thickness(1);

                if (file.ContentType.Contains("video") || file.ContentType.Contains("image"))

                {

                    //----< Ist Image >----

                    UcFile ucImage = new UcFile();

                    ucImage.file = file;

                    //item.Content = ucImage;

                    //< anfuegen >

                    item.Content = ucImage;

                    ctlFiles.Items.Add(item);

                    //< anfuegen >

                    //----</ Ist Image >----

                }

                //--</ File >--

            }

            //----</ @Loop: Files in Folder >-----

            //------------</ fxReadFiles_inFolder()------------

        }

    }

}

 

 

UcFile.xaml.cs

using Microsoft.UI.Xaml;

using System;

using Windows.UI.Xaml.Media.Imaging;    // Bitmap

using Windows.Storage;                  //Files File.AccessMode

using Windows.Storage.FileProperties;   //Thumbnails

using System.Threading.Tasks;           //Tasks

using Windows.Graphics.Imaging;        //BitmapEncoder

using Windows.UI.Xaml.Controls;

using Windows.UI.Popups;

namespace UserControls

{

    public sealed partial class UcFile : UserControl

    {

        StorageItemThumbnail imgThumbnail;

        StorageFile _file;

        public StorageFile file

        {

            set { _file = value; }

        }

        #region Properties

        //========================< Region: Properties >========================

        //setzen und lesen von Eigenschaften

        //========================</ Region: Properties >========================

        #endregion

        #region UserControl

        //========================< Region: Page >========================

        public UcFile()

        {

            this.InitializeComponent();

        }

        private void UserControl_Loading(Windows.UI.Xaml.FrameworkElement sender, object args)

        {

        }

        private void UserControl_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)

        {

            fl_load_Image();

        }

        //========================< Region: UserControl >========================

        #endregion

        private async void fl_load_Image()

        {

            //------------< fl_load_Image() >------------

            //--< show Image as Thumbnail >--

            try

            {

                imgThumbnail = await _file.GetThumbnailAsync(ThumbnailMode.SingleItem, 100, ThumbnailOptions.ResizeThumbnail);

                //-< show Image  >-

                BitmapImage bitmapImage = new BitmapImage();

                bitmapImage.SetSource(imgThumbnail);

                tnImage.Source = bitmapImage;

                //-</ show Image >-

                //tbxTitle.Text = _file.DisplayName;

            }

            catch (Exception ex)

            {

                //clsDebug.debug_Exception(ex, "Error " + filename_cached);

                //HResult==-2147467261->.wmf

                await new MessageDialog(ex.Message).ShowAsync();

            }

            //--</ show Image as Thumbnail >--

            //------------</ fl_load_Image() >------------

        }

    }

}