【问题标题】:Open an image with the Windows default editor in C#在 C# 中使用 Windows 默认编辑器打开图像
【发布时间】:2013-04-15 18:25:37
【问题描述】:

在我的 C# 应用程序中,我想启动默认的图像编辑器来编辑图像。

当我使用System.Diagnostics.Process.Start("C:\\image.png") 时,它会使用Windows Photo Viewer 打开图像文件。

当我在 Windows 资源管理器中右键单击图像文件时,会出现一个“编辑”菜单项,它会启动 Microsoft Paint(默认情况下)。我想在我的应用程序中做同样的事情(即使用默认图像编辑器打开文件)。

我不想通过 Process.Start("mspaint.exe C:\\image.png") 对 MS Paint 进行硬编码。我更喜欢使用用户设置的默认图像编辑器程序(可能与 MS Paint 不同)。

有没有办法做到这一点?

谢谢 弗兰克

【问题讨论】:

标签: c# .net windows image


【解决方案1】:

您可以尝试使用动词 edit 启动进程。

ProcessStartInfo startInfo = new ProcessStartInfo("C:\\image.png");
startInfo.Verb="edit";

Process.Start(startInfo);

【讨论】:

  • 如果您只想使用与文件扩展名关联的程序显示图像,则似乎没有必要指定 ProcessStartInfo.Verb。
  • 不,但如果你想编辑它,这几乎是问题的本质。
  • 这适用于任何文件,而不仅仅是图像,但是,我不得不求助于try { } catch (Win32Exception) { } 来处理未定义默认编辑操作的文件。
  • 在 windows 8.1 中使用时,它会在 photviewer 中打开并引发异常。
【解决方案2】:

如果你想通过默认的 Windows 编辑器在图片框中打开你的图像,试试这个;

//Create temporary file name
String TMP_IMAGE = "tempImage" +DateTime.Now.Millisecond +".bmp";

//get the folder of application
string PATH_APP =                
        System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"\tempImage\";

//Create a subFolder tempImage
Directory.CreateDirectory(PATH_APP);

//Save a new path in a variable
String NEW_PATH = PATH_APP + TMP_IMAGE;

//Save the image in the pictureBox in the new path and file name
pictureBox.Image.Save(NEW_PATH);

//Lunch the process with defaoul image editor in the comouter
ProcessStartInfo startInfo = new ProcessStartInfo(NEW_PATH);
Process.Start(startInfo);

【讨论】:

猜你喜欢
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 2013-02-28
  • 2012-11-17
  • 2013-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多