#

WPF: Image zur Laufzeit erstellen
 
Aufgabe:
in einem WPF Anwendung Fotos zur Laufzeit in ein Wrap-Panel hinzufügen.
 
 
Lösung:
In WPF erstellt man für Bilder und Fotos zur Laufzeit ein Image als Instanz der Image-Klasse.
Das Image dient der Anzeige als Ausgabeelement.
 
Danach erstellt man eine BitmapImage, welche das Foto liest und übergibt den Datenstrom an das Ausgabeelement

Image newImage = new Image();
BitmapImage src = new BitmapImage();
src.UriSource = new Uri(Image_with_Path, UriKind.Absolute);

 
Anschliessend fügt man das Anzeigelement einem Panel oder Grid hinzu, welches sich schon in dem Anzeige Window befindet.
 
Betrifft: WPF, C#
Hier wurden alle Fotos aus einem Verzeichnis in ein WPF Windows, Wrappanel geladen

 

//----------------< add_Image() >----------------
Image newImage = new Image();

//< source >
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(Image_with_Path, UriKind.Absolute);
src.EndInit();
newImage.Source = src;
//</ source >
 
newImage.Stretch = Stretch.Uniform;
newImage.Height = 100;
 
//< add >
panel_Images.Children.Add(newImage);
//</ add >
//----------------</ add_Image() >----------------
 

 
 
 
Kompletter C# Code zum Einfügen aller Fotos in eine WPF Anwendung, Wrappanel

private void get_Images(String folder_Path)
{
//----------------< get_Images() >----------------
//-< check >-
if (folder_Path == "") return;
//-</ check >-
 
try
{
//--------< Folder >--------
DirectoryInfo folder = new DirectoryInfo(folder_Path);
if (folder.Exists)
{
//------< @Loop: Files >------
foreach (FileInfo fileInfo in folder.GetFiles())
{
//----< File >----
if (".jpg|.jpeg|.png".Contains(fileInfo.Extension.ToLower()))
{
//----< IsPhoto >----
String sDate = fileInfo.LastWriteTime.ToString("yyyy-MM-dd");
//Debug.WriteLine("#Debug: File: " + fileInfo.Name + " Date:" + sDate);
add_Image(fileInfo.FullName);
//----</ IsPhoto >----
}
//----</ File >----
}
//------</ @Loop: Files >------
}
//--------</ Folder >--------
}
catch
{
}
//----------------</ get_Images() >----------------
}
 
 
 
private void add_Image(String Image_with_Path)
{
//----------------< add_Image() >----------------
Image newImage = new Image();

//< source >
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(Image_with_Path, UriKind.Absolute);
src.EndInit();
newImage.Source = src;
//</ source >
 
newImage.Stretch = Stretch.Uniform;
newImage.Height = 100;
 
//< add >
panel_Images.Children.Add(newImage);
//</ add >
//----------------</ add_Image() >----------------
}
 

 
 
Notwendige Namespaces:

//--< using >--
using Microsoft.Win32; //FileDialog
using WinForms = System.Windows.Forms ; //FolderDialog
using System.IO; //Folder, Directory
using System.Diagnostics; //Debug.WriteLine
//--</ using >--

 
 
Und zudem der XAML Code des Fensters
Mit dem Wrappanel in de Mitte

 

<Window x:Class="Gruppiere_Verzeichnis.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Gruppiere_Verzeichnis"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Name="gridBase">
<Grid.RowDefinitions>
<RowDefinition Name="rowSelect" Height="11*"/>
<RowDefinition Height="96*"/>
<RowDefinition Name="rowExtented" Height="11*"/>
</Grid.RowDefinitions>
 
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Grid.Row="0">
<Button x:Name="btnRead_Folder" Content="Read" Click="btnRead_Folder_Click" Width="64" />
 
<TextBox x:Name="tbxFolder" TextWrapping="Wrap" Text="" HorizontalAlignment="Left" Width="341" Margin="0,0,0,-0.2" />
<Button x:Name="btnSelect_Folder" Content=".." Click="btnSelect_Folder_Click" Width="38" />
</StackPanel>
 
<Button x:Name="btnRegister" Content="Register" Click="btnRegister_Click" Width="64" Grid.Row="2" HorizontalAlignment="Left" />
 
<Grid Name="gridFolders" Grid.Row="1">
<WrapPanel Name="panel_Images"></WrapPanel>
</Grid>
</Grid>
</Window>

 
Mobile

.

yesmovies