如何通过 C# 应用程序使用命令提示符来转换图像?

问题描述 投票:0回答:1

我正在使用 MagickImage 从命令提示符转换图像。但是,当我尝试对 C# 进程使用相同的命令时,它不起作用。有什么想法可能是这样吗?

这是我的代码: `private void ConvertButton_Click(对象发送者,EventArgs e) {

// TO DO: Get the input and output paths for the file
string input = opnfd.FileName;
string name = Path.GetFileNameWithoutExtension(input);

// TO DO: Initiate the process for using the Magick CLT
using (Process Magick = new Process())
{
    // TO DO: Give the process the propper filename, argument and properties
    ProcessStartInfo info = new ProcessStartInfo();
    info.FileName = "cmd.exe";
    info.CreateNoWindow = true;

    // use a process to get the parent directory of the image
    string parent = Directory.GetParent(input).ToString();
    
    string command = String.Format("convert {0}{1} -resize 256x256 {2}\\output{3}", name, richTextBox1.Text, parent, comboBox1.Text);

    info.WorkingDirectory = parent;
    info.Arguments = command;
    Magick.StartInfo = info;
    if (Magick.Start()) MessageBox.Show("Conversion successful");
    else MessageBox.Show("Conversion failed");
    Magick.Close();

}

// command: convert INPUT -resize 256x256 OUTPUT
// example: convert Xbox_Controller.png -resize 256x256 C:\Users\Costin\Desktop\output.ico

}`

我正在 Windows 窗体应用程序中尝试所有这些。使用 .NET 6,0(因为它具有 LTS)。我将 Windows 终端设置为默认终端,但我认为这不会影响它,因为通过终端从 cmd 运行它仍然有效。我的 ImageMagick 版本是 ImageMagick-7.1.1-15-Q16-x64。

c# imagemagick imagemagick-convert
1个回答
0
投票

正如其他人已经提到的,您应该提供更多细节,而不仅仅是“它不起作用”,包括您尝试过什么以及您看到的结果是什么。

我用一些假设查看了您的脚本(例如您似乎正在使用 OpenFileDialog),并注意到两件事:您似乎没有在其中的任何地方指定 ImageMagick,并且您的命令字符串似乎有点奇怪。

我对您的代码做了一些小的更改,可以正常工作:

    // TO DO: Get the input and output paths for the file
    string input = opnfd.FileName;
    string inputFileName = Path.GetFileName(input);
    // string parentDirectory = Path.GetDirectoryName(input);         an alternative to below on getting the path to the directory

    // TO DO: Initiate the process for using the Magick CLT
    using (Process Magick = new Process())
    {
        // TO DO: Give the process the propper filename, argument and properties
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = "magick";           // <- you can just specify the imagmeagick directly here
        info.CreateNoWindow = true;

        // use a process to get the parent directory of the image
        string parent = Directory.GetParent(input).ToString();

        string command = String.Format("convert {0} -resize 256x256 {1}{2}output{3}", inputFileName, parentDirectory, Path.DirectorySeparatorChar, comboBox1.Text);

        info.WorkingDirectory = parent;
        info.Arguments = command;
        Magick.StartInfo = info;

        if (Magick.Start()) MessageBox.Show("Conversion successful");
        else MessageBox.Show("Conversion failed");
        Magick.Close();

    }

    // command: convert INPUT -resize 256x256 OUTPUT
    // example: convert Xbox_Controller.png -resize 256x256 C:\Users\Costin\Desktop\output.ico

请注意,您之前使用过:

info.FileName = "cmd.exe";

您可以直接指定要运行的程序,而不用指定cmd。例如。如果您为 ImageMagick 设置了环境变量,则只需指定将调用该实用程序的“magick”,或者提供 ImageMackig 的完整安装路径。在此示例中,我假设环境变量可用并已使用:

info.FileName = "magick";   // or the full path, @"C:\Program Files\ImageMagick-7.1.1-Q16\magick.exe"

如果您确实想将其保留为“cmd.exe”,那么您必须确保添加到命令字符串/C,以指示 shell 执行该字符串。

string command = String.Format("/C convert {0} -resize 256x256 {1}{2}output{3}", inputFileName, parentDirectory, Path.DirectorySeparatorChar, comboBox1.Text);

但是如果你直接在startInfo中指定程序,则不需要这个。

否则,我不确定您为什么尝试获取不带文件扩展名的文件名,但您可以使用 Path.GetFileName(input) 来获取带扩展名的文件名(例如 Xbox_Controller.png),我添加了一个示例使用 Path.GetDirectoryName(input) 获取文件所在文件夹的实际路径。

我稍微调整了你的命令字符串,你可能需要进一步调整它,因为尚不完全清楚你打算如何指示它。本质上,它现在采用带扩展名的文件名,并提供输入文件所在文件夹的路径以及操作系统的默认文件夹分隔符,并在末尾添加所需的目标文件扩展名(似乎来自组合框)。

尽管如此,正如其他人已经分享的那样,也存在现有的 Nuget 包,并且使用 .WaitForExit() 等进一步改进代码以确保流程相应完成可能是有意义的。

© www.soinside.com 2019 - 2024. All rights reserved.