【问题标题】:Open image in Windows Photo Viewer在 Windows 照片查看器中打开图像
【发布时间】:2011-07-24 16:20:22
【问题描述】:

如何从 C# 应用程序在 Windows 照片查看器中打开 .jpg 图像?

不是像这样的代码在应用程序中,

FileStream stream = new FileStream("test.png", FileMode.Open, FileAccess.Read);
pictureBox1.Image = Image.FromStream(stream);
stream.Close();

【问题讨论】:

    标签: c# winforms image


    【解决方案1】:

    我认为你可以使用:

    Process.Start(@"C:\MyPicture.jpg");
    

    这将使用与 .jpg 文件关联的标准文件查看器 - 默认情况下是 windows 图片查看器。

    【讨论】:

    • 使用 Process.Start() 最好的部分是它不关心你给它什么样的文件,它只会使用默认查看器。即 PDF 将在 Adob​​e Viewer 中自动打开。
    • 对我不起作用。 The specified executable is not a valid application for this OS platform
    • @SaeedNamati ...在不了解您的操作系统设置的情况下,我无法提供具体帮助。您可能会发现以下内容将帮助您调查这个...stackoverflow.com/questions/46808315/…
    【解决方案2】:

    在一个新的Process开始它

    Process photoViewer = new Process();
    photoViewer.StartInfo.FileName = @"The photo viewer file path";
    photoViewer.StartInfo.Arguments = @"Your image file path";
    photoViewer.Start();
    

    【讨论】:

    • 这个大家都知道,但是Windows中默认的图片查看器叫什么名字呢?
    【解决方案3】:

    代码从 ftp 获取照片并在 Windows 照片查看器中显示照片。 我希望它对你有用。

      public void ShowPhoto(String uri, String username, String password)
            {
                WebClient ftpClient = new WebClient();
                ftpClient.Credentials = new NetworkCredential(username,password);
                byte[] imageByte = ftpClient.DownloadData(uri);
    
    
                var tempFileName = Path.GetTempFileName();
                System.IO.File.WriteAllBytes(tempFileName, imageByte);
    
                string path = Environment.GetFolderPath(
                    Environment.SpecialFolder.ProgramFiles);
    
                // create our startup process and argument
                var psi = new ProcessStartInfo(
                    "rundll32.exe",
                    String.Format(
                        "\"{0}{1}\", ImageView_Fullscreen {2}",
                        Environment.Is64BitOperatingSystem ?
                            path.Replace(" (x86)", "") :
                            path
                            ,
                        @"\Windows Photo Viewer\PhotoViewer.dll",
                        tempFileName)
                    );
    
                psi.UseShellExecute = false;
    
                var viewer = Process.Start(psi);
                // cleanup when done...
                viewer.EnableRaisingEvents = true;
                viewer.Exited += (o, args) =>
                {
                    File.Delete(tempFileName);
                };
    
    
            }
    

    最好的问候...

    【讨论】:

      【解决方案4】:

      我正在尝试其他答案,但它们都返回关于该位置如何不是 OS 应用程序的相同错误,所以我不确定问题出在哪里。然而,我确实发现了另一种打开文件的方法。

      string Location_ToOpen = @"The full path to the file including the file name";
      if (!File.Exists(Location_ToOpen))
      {
         return;
      }
      
      string argument = "/open, \"" + Location_ToOpen + "\"";
      
      System.Diagnostics.Process.Start("explorer.exe", argument);
      

      首先测试文件是否存在。如果它不存在,则会导致错误。

      之后它在不打开文件资源管理器的情况下模拟文件资源管理器上的“打开”请求,然后系统使用默认应用打开文件。

      我目前在我的项目中使用这种方法,所以我希望它也适用于你。

      【讨论】:

        【解决方案5】:
        public void ImageViewer(string path)
                {            
                    Process.Start("explorer.exe",path);            
                }
        

        Path是要预览的图片的文件路径。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多