类似浏览器的图像浏览器的最佳控件

本文关键字:浏览器 控件 最佳 图像 | 更新日期: 2023-09-27 18:19:35

我想创建一个界面类似于Windows资源管理器的程序,即显示缩略图、名称、大小等,并且正在寻找要使用的控件。

我想让它做的是:

  • 显示所有图片是由代码选择的文件夹。可以有数百个,使用数百MB,所以我只需要它来渲染可见的部分,而不是整个东西
  • 允许选择一个或多个图片与代码的其他部分一起使用(应用筛选器、复制到其他地方等)。当然,某些选定的图像可能不可见,因此可能无法呈现
  • 它应该允许每个项目使用几行信息(名称、大小、日期…)
  • 缩略图的大小应该很容易更改,如果一列中有或多或少的项目,我需要回流这些项目(这可以用代码完成,但我更喜欢它在控件中)。当然,这不应该改变所选项目的列表(因此,所选项目不是按行和列标识的,而是按索引标识的)

我现在拥有的是这个(稍后将转到另一个功能)

thumb[] thumbs;
public MainWindow() {
    InitializeComponent();
    int i;
    string[] files=Directory.GetFileSystemEntries(@"C:'images","*.jpg",SearchOption.AllDirectories);
    System.Drawing.Size mySize=new System.Drawing.Size(128,128);
    thumbs=new thumb[files.Length];
    for(i=0; i<files.Length ;i++){
        thumbs[i]=new thumb(files[i],mySize);
    }
    //MessageBox.Show("Loaded "+i.ToString()+" images");
}

这个(稍后我可能会改用Image.GetThumbnailImage()作为调整大小的基础):

class thumb {
    public Bitmap bmp;
    public Size   originalSize;
    public string path;
    public thumb(string path, Size targetSize) {
        Bitmap tempBmp;
        this.path=path;
        tempBmp=new Bitmap(path);
        originalSize=tempBmp.Size;
        bmp=new Bitmap(tempBmp,targetSize);
        tempBmp.Dispose(); //get our memory back
    }
}

类似浏览器的图像浏览器的最佳控件

您可以将ListBox用作包含缩略图、名称等控件的容器。您可以为ListBoxItem创建一个DataTemplate,也可以创建新的UserControl并将其用作ListBox的Content。

使用WrapPanel可以使其对应用程序窗口的大小非常敏感。

这里有一个示例,您需要根据需要进行更改,但这是一个很好的起点。

<ListBox x:Name="ListOfImages" ItemsSource="{Binding Images}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Width="120" Margin="10" HorizontalAlignment="Left">
                <Image Source={Binding ImagePath}" />
                <TextBlock Text={Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel />
    </ItemsPanelTemplate>
</ListBox>