XAML
<Grid x:Name="GalleryLayout" HorizontalAlignment="Center" Background="White" VerticalAlignment="Bottom">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid x:Name="GalleryGrid" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ScrollViewer x:Name="Scroll" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" CanContentScroll="True">
<StackPanel x:Name="GalleryPanel" Grid.Column="1" VerticalAlignment="Center" Margin="20,0,20,0" CanHorizontallyScroll="True" Orientation="Horizontal"/>
</ScrollViewer>
</Grid>
C#
public partial class GalleryPhoto : UserControl, ISwitchable
{
private String[] list; //Array wich will contain the path to all images in a directory : ex C:\\MyPictures\\rainbowdash.jpg
string pathString; //Path to the directory where the image are (ex C:\\MyPictures)
public GalleryPhoto()
{
InitializeComponent();
//Here the code for the Height and Width of XAML buttons...
pathString = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\" + "Photo";
list = Directory.GetFileSystemEntries(pathString, "*.*");
this.Gallery.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2;
for (int index = 0; index < list.Length; index++) //Create a button for each image found and paint it with an ImageBrush
{
System.Drawing.Image image = System.Drawing.Image.FromFile(list[index]);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);
BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()
);
bitmap.Dispose();
ImageBrush brush = new ImageBrush(bitmapSource);
Button button = new Button();
button.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 10;
button.Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height / 10;
button.Background = brush;
this.Gallery.Children.Add(button);
}
}
请温柔,拜托,这是我的第一次......:$