【发布时间】:2015-01-23 16:39:15
【问题描述】:
好的,所以我知道我应该使用 ImageMagick DLL...我正在学习、测试并努力做到这一点。但与此同时,我正在使用通过进程调用 Imagemagick 的 convert.exe 的低效方法。
当我在测试时,它可以很好地处理数百张图片。但后来我将我的 WindowsForms 程序移到一台速度更快的机器上,它每次都在同一点崩溃。
这是一个两步流程调用。第一次遍历所有文件并生成 PNG。然后我遍历所有 PNG 并将其与背景合成并输出 JPG。但每次恰好有 22 张图像时,它都会出错“System.OutOfMemoryException:内存不足”。有什么东西在填满我需要杀掉吗?
foreach (string file in files)
{
try
{
string captureImg = Path.GetFileName(file);
string maskImg = file.Replace("X.JPG", "Y.JPG");
string OutputImage = string.Format("{0}.png", Path.GetFileNameWithoutExtension(captureImg));
string output = Path.Combine(destFolder, OutputImage);
//MessageBox.Show(output);
progressBarImage.Value = progressBarImage.Value + 1;
lblStatus.Text = string.Format("Image {0} of {1}", progressBarImage.Value, maxFiles);
makePNG(file, maskImg, output);
Application.DoEvents();
}
catch (Exception)
{
}
}
if (chkBG.Checked)
{
//try
//{
string JPGdir = Path.Combine(destFolder, "JPGs");
string[] PNGfiles = Directory.GetFiles(destFolder, "*C.PNG");
lblProgress.Text = "Generating JPGs with Background";
progressBarImage.Value = 0;
progressBarImage.Maximum = files.Length;
message = "PNG and JPG Export Complete";
if (!Directory.Exists(JPGdir))
{
Directory.CreateDirectory(JPGdir);
}
foreach (string PNGfile in PNGfiles)
{
Application.DoEvents();
string outputJPG = string.Format("{0}.jpg", Path.GetFileNameWithoutExtension(PNGfile));
string result = Path.Combine(JPGdir, outputJPG);
progressBarImage.Value += 1;
lblStatus.Text = string.Format("Image {0} of {1}", progressBarImage.Value, files.Length);
makeJPG(PNGfile, txtBackground.Text, result);
//MessageBox.Show(PNGfile);
}
private void makePNG(string source, string mask, string output)
{
if (!source.EndsWith("Y.JPG"))
{
Process proc = new Process();
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = @"""C:\Program Files\ImageMagick-6.9.0-Q16\convert.exe""";
proc.StartInfo.Arguments = string.Format(@"{0} {1} -alpha off -compose copy-opacity -level 5% -composite {2}", source, mask, output);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
}
}
private void makeJPG(string source, string background, string output)
{
float BGimg = Image.FromFile(background).Height;
float SubjectImg = Image.FromFile(source).Height;
float ResultHeight = 100 * (BGimg / SubjectImg);
int Height = Convert.ToInt32(ResultHeight);
Process procJPG = new Process();
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
procJPG.EnableRaisingEvents = false;
procJPG.StartInfo.FileName = @"""C:\Program Files\ImageMagick-6.9.0-Q16\convert.exe""";
procJPG.StartInfo.Arguments = string.Format(@"{1} ( {0} -resize {3}% ) -gravity South -composite {2}", source, background, output, Height);
procJPG.StartInfo.UseShellExecute = false;
procJPG.StartInfo.RedirectStandardOutput = true;
procJPG.StartInfo.CreateNoWindow = true;
procJPG.Start();
procJPG.WaitForExit();
}
【问题讨论】:
-
你为什么要重定向标准输出?
-
Application.DoEvents();尽量不要使用它.. 这不是一个好习惯 -
@MethodMan 我不同意。
Application.DoEvents()非常好如果适当使用(尽管适当使用它是困难的部分;-)) -
@MethodMan 我已经阅读了很多关于它的内容(包括那些说“它是邪恶的,从不使用它”),但是在我多年做 Windows.Forms 的过程中,我已经成功地在很多情况下,特别是在直接处理消息队列以处理 WinForms 中不存在的复杂功能时。它基本上等同于我们在直接 Win32 C++ 中使用的旧
PeekMessage循环,如果您了解使用它的含义,它没有任何问题。是“DoEvents() 的错误用法”使它变得邪恶,而不是函数本身
标签: c# process imagemagick-convert