I have managed to create my first iot project and can get an image to load on a button press on my Raspberry Pi2.
I have tried to put together code that will get me a random image from a set of images but I have a few areas I am falling over.
1) I don't know how to scan a USB stick from the Pi
2) I cannot seem to load an image from a file, only from an embedded resource.
3) I cannot seem to work out how to close the app, I would have thought App.exit would do it but it appears not.
Basically I am just playing around and I thought a good learning project would be to create a digital photo frame which randomly shows images from a USB stick, it's not gone well for me at all.
If I can get it to work on the button press I should be able to drop in a timer and have it run all the time quite easily.
Here is my code, you can see from my commented out parts what I have tried:
' The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class MainPage
Inherits Page
Dim random As Random = New Random()
Private Sub ClickMe_Click(sender As Object, e As RoutedEventArgs)
'Dim DirectoryPath As String = "F:/VB.Net files/Images/"
'Dim finfo As FileInfo = New FileInfo(GetRandomImageFilePath(DirectoryPath))
'Dim filename As String = finfo.Name.Replace(finfo.Extension, "")
'BBPLogo.Source = New BitmapImage(New Uri("file:///" & finfo.FullName, UriKind.Relative))
BBPLogo.Source = New BitmapImage(New Uri("ms-appx:///Assets/Bowral-Bricks.png", UriKind.Absolute))
End Sub
Private Sub ExitButton_Click(sender As Object, e As RoutedEventArgs) Handles ExitButton.Click
End Sub
Public Function GetRandomImageFilePath(ByVal folderPath As String) As String
Dim files() As String = Directory.GetFiles(folderPath, "*.png")
Return files(random.Next(0, files.Length))
End Function
End Class
Not sure if you need it but here is the xaml
<Page
x:Class="PiTest01.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PiTest01"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Height="480" Width="800">
<Grid>
<Grid.Background>
<ImageBrush Stretch="Fill"/>
</Grid.Background>
<Image x:Name="BBPLogo" HorizontalAlignment="Left" Height="480" VerticalAlignment="Top" Width="800" RenderTransformOrigin="1.163,0.596" Stretch="UniformToFill"/>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Margin="78,18,436,164" Height="178">
<Button x:Name="ClickMe" Content="Click Me!" Margin="10" HorizontalAlignment="Center" Click="ClickMe_Click"/>
<Button x:Name="ExitButton" Content="Exit" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="126"/>
</StackPanel>
</Grid>
</Page>
I've tested the following on my Raspberry Pi so in theory it's all doable.
(1) Finding image files on USB drive
Firstly, you will need to add the "Removable Storage" capability to your app: open Package.appxmanifest, navigate to the Capabilities tab, and ensure that "Removable Storage" is checked. This will allow your app access to files on the USB drive. Secondly, Universal applications can only enumerate/read/modify file types that have been explicitly declared in the manifest under "File Type Associations". Again open Package.appxmanifest, navigate to the Declarations tab, select "File Type Associations", and hit Add. You will now need to fill in the fields on the right-hand pane according to what file types your app wants to support: details can be found on MSDN.
You can then enumerate removable drives and their sub-files/folders like in these examples. For example, to enumerate top-level files (C#, sorry):
var drives = await Windows.Storage.KnownFolders.RemovableDevices.GetItemsAsync();
foreach (var drive in drives)
{
var files = await drive.GetFilesAsync();
}
Also remember that only sub-folders and explicitly associated file types will show up.
(2) Loading image from a file
It is not possible to load a file directly from a file:// URI. Instead you need to go via a StorageFile and stream, like for example:
var file = await StorageFile.GetFileFromPathAsync(@"E:\test.png");
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
var bmp = new BitmapImage();
await bmp.SetSourceAsync(stream);
ImageControl.Source = bmp;
}
(3) Exiting the application
App.Current.Exit(); appears to work for me, exiting the app and weirdly showing the "Please wait while we set up your device" progress spinner which apparently still lurks behind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With