对Explorer类图像浏览器的最佳控制

时间:2014-08-26 09:00:40

标签: c# wpf controls thumbnails

我想用类似于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
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用ListBox作为容纳缩略图,名称等的控件的容器。您可以为ListBoxItem创建DataTemplate或创建新的UserControl并将其用作ListBox的内容。

使用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>