问题描述
前段时间,我在 VB.Net 中编写了一个程序,它类似于熟悉的 Windows 图像查看器,但具有更多优点。今天我想进一步开发该程序,另外还可以根据图像尺寸(即百万像素)进行排序,并且图像按此顺序显示(降序)。 所以我对您的关注是:我如何按百万像素然后按名称排序?我还不太擅长列表和它们的排序命令。 我编写了一个名为“图像属性”的单独类。在这里,对于每个图像,传输完整路径和尺寸并计算百万像素。
Form_Main 中的代码
Imports Microsoft.WindowsAPICodePack.Dialogs
Public notinheritable Class Form_Main
Private Shared Form2 As Form_Anzeige
''' <summary>
''' List with file names
''' </summary>
Public Liste_mit_Dateinamen As List(Of String)
Private ReadOnly Deu As New System.Globalization.CultureInfo("de-DE")
''' <summary>
''' List with instances of the class Bildeigenschaften (image properties)
''' </summary>
Private Liste_Klasse_Bildeigenschaften As New List(Of Bildeigenschaften)
Private Sub Form_Main_Load(sender As Object,e As EventArgs) Handles MyBase.Load
Dim Screens As Screen() = Screen.AllScreens
If Screens.Length = 1 AndAlso (Screens(0).Bounds.Width < 1920 OrElse Screens(0).Bounds.Height < 1080) Then
MessageBox.Show($"Es wurde kein zweiter Bildschirm angeschlossen und der erste hat eine zu kleine Auflösung{Environment.NewLine}(1920 × 1080 wäre nötig).","Info",MessageBoxButtons.OK,MessageBoxIcon.@R_310_4045@ion)
Return
End If
End Sub
Private Sub Button_Start_Click(sender As Object,e As EventArgs) Handles Button_Start.Click
Dim path_to_folder As String = ""
Using OFolderD As New CommonopenFileDialog
OFolderD.Title = "Ordner auswählen"
OFolderD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
OFolderD.IsFolderPicker = True
If OFolderD.ShowDialog() = CommonFileDialogResult.Ok Then
path_to_folder = OFolderD.FileName
Else
Return
End If
End Using
Dim Liste_FI As New List(Of IO.FileInfo)
Dim DI As IO.DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(path_to_folder)
Dim ExtensionList As List(Of String) = New List(Of String) From {".jpg",".jpeg",".bmp",".png"}
For Each File As IO.FileInfo In DI.GetFiles
If ExtensionList.Contains(File.Extension.ToLower(Deu)) Then
Liste_FI.Add(File)
Using temp_Bmp As New Bitmap(File.FullName)
Liste_Klasse_Bildeigenschaften.Add(New Bildeigenschaften(CUInt(temp_Bmp.Width),CUInt(temp_Bmp.Height),File.Name))
End Using
End If
Next
If Liste_FI.Count = 0 Then
MessageBox.Show("In dem gewählten Ordner befinden sich keine Bilder (jpg,bmp,png).",MessageBoxIcon.@R_310_4045@ion)
Return
End If
Liste_mit_Dateinamen = New List(Of String)
If RadioButton1.Checked Then
Dim query As IEnumerable(Of IO.FileInfo) = Liste_FI.OrderBy(Function(Info As IO.FileInfo) Info.CreationTime)
For Each fi As IO.FileInfo In query
Liste_mit_Dateinamen.Add(fi.FullName)
Next
End If
If RadioButton2.Checked Then
Dim query As IEnumerable(Of IO.FileInfo) = Liste_FI.OrderBy(Function(Info As IO.FileInfo) Info.Name)
For Each fi As IO.FileInfo In query
Liste_mit_Dateinamen.Add(fi.FullName)
Next
End If
If RadioButton3.Checked Then
' here
End If
'Nicht-moDaler Dialog auf großem Bildschirm (mind. 1920 × 1080). Benutzer soll Form2 nach Belieben (irgendwann) wegklicken.
'non-modal dialog on large screen (at least 1920 × 1080). User can close Form2 at will (at some point).
Form2 = New Form_Anzeige
If Not Form2.Visible AndAlso Not Form2.Isdisposed AndAlso Liste_mit_Dateinamen.Count > 0 Then
Form2.Show(Me)
Button_Start.Enabled = False
End If
End Sub
Private Sub Button_Stop_Click(sender As Object,e As EventArgs) Handles Button_Stop.Click
If Form2 IsNot nothing Then
Form2.Timer1.Stop()
End If
End Sub
End Class
“Bildeigenschaften”类代码(“图像属性”)
notinheritable Class Bildeigenschaften
Private Megapixels As UInt32
Private Path_to_the_file As String = ""
Public Sub New(ByVal _width As UInt32,ByVal _height As UInt32,ByVal _path As String)
Me.Megapixels = _width * _height
Me.Path_to_the_file = _path
End Sub
End Class
解决方法
例如
email
Public Class Thing
Public Property First As String
Public Property Second As String
End Class
该代码使用 List(Of T).Sort(Comparison(Of T))
方法。还有其他特定的排序方法,但它们都依赖于类似的原则,即重复比较成对的项目以产生表示它们相对大小的 Dim things As New List(Of Thing)
'...
things.Sort(Function(t1,t2)
Dim result = t1.First.CompareTo(t2.First)
If result = 0 Then
result = t1.Second.CompareTo(t2.Second)
End If
Return result
End Function)
值。在这样的比较中,小于零的值意味着第一项小于第一项,大于零的值意味着第二项小于第一项,零意味着它们相等。这就是为什么在此代码中,当且仅当第一个属性的比较产生零结果时,才比较第二个属性。
有关比较和排序方法的更多信息,请参阅我的博客文章 here。请注意,该帖子分为三个部分。