今天我需要在 Windows 照片查看器中打开几张图片,只打开一个实例。
通常,直接打开图像本身就足够了,然后用户使用箭头浏览图像。
但是,当图像位于 Temp 文件夹中时,箭头将被禁用。另一种方法是打开容器文件夹,然后启用箭头。
这是我在 VB 中的代码:
Private Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim filePath As String = "C:\Users\Picsonald\AppData\Local\Temp\SubFolder\photo1.jpg"
Dim directoryPath As String = "C:\Users\Picsonald\AppData\Local\Temp\SubFolder"
OpenPhotoViewer(filePath) ' Open the picture, but arrow are disabled (cause of Temp folder ?) :(
OpenPhotoViewer(directoryPath) ' Open a picture located in the directory, and arrow are enabled :) <- Several pictures can be browsed
End Sub
Private Sub OpenPhotoViewer(pathToOpen As String)
' Finding the PhotoViewer.dll full path...
Dim photoViewerPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles).Replace(" (x86)", String.Empty), "Windows Photo Viewer", "PhotoViewer.dll")
If Not File.Exists(photoViewerPath) Then
photoViewerPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86), "Windows Photo Viewer", "PhotoViewer.dll")
End If
' Construct arguments to specify element to display
Dim argument As String = String.Concat("""", photoViewerPath, """, ImageView_Fullscreen " + pathToOpen)
Dim psi As ProcessStartInfo = New ProcessStartInfo("rundll32.exe", argument)
psi.UseShellExecute = True
Process.Start(psi) ' Run the Microsoft Photo Viewer
End Sub
和 C# 中的相同代码:
private void BtnLoad_Click(object sender, EventArgs e)
{
string filePath = @"C:\Users\Picsonald\AppData\Local\Temp\SubFolder\photo1.jpg";
string directoryPath = @"C:\Users\Picsonald\AppData\Local\Temp\SubFolder";
OpenPhotoViewer(filePath); // Open the picture, but arrow are disabled (cause of Temp folder ?) :(
OpenPhotoViewer(directoryPath); // Open a picture located in the directory, and arrow are enabled :) <- Several pictures can be browsed
}
private void OpenPhotoViewer(string pathToOpen)
{
// Finding the PhotoViewer.dll full path...
string photoViewerPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles).Replace(" (x86)", String.Empty), "Windows Photo Viewer", "PhotoViewer.dll");
if (!File.Exists(photoViewerPath))
{
photoViewerPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86), "Windows Photo Viewer", "PhotoViewer.dll");
}
// Construct arguments to specify element to display
string argument = string.Concat("\"", photoViewerPath, "\", ImageView_Fullscreen " + pathToOpen);
ProcessStartInfo psi = new ProcessStartInfo("rundll32.exe", argument);
psi.UseShellExecute = true;
Process.Start(psi); // Run the Microsoft Photo Viewer
}
但是,必须有比提问者要求的方法更合适的方法,因为使用 Windows 资源管理器,从包含多张图片的文件夹中选择两张图片会导致 Windows 照片查看器仅浏览这两张图片。