Mat*_*ner 0 c# process imagemagick-convert
好的,所以我知道我应该使用ImageMagick DLL ......我正在学习,测试并努力做到这一点.但与此同时,我正在使用通过一个进程调用Imagemagick的convert.exe的低效方法.
当我测试时,它可以正常处理数百张图像.但后来我将我的WindowsForms程序移动到更快的机器上,并且每次都在同一点崩溃.
这是一个两步过程调用.第一次循环遍历所有文件并生成PNG.然后我循环遍历所有PNG并将其与背景合成并输出JPG.但是每次正好在22个图像上出现错误"System.OutOfMemoryException:Out of memory".有什么东西可以填满,我需要杀死什么?
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();
}
Run Code Online (Sandbox Code Playgroud)
乍一看,您正在使用Image.FromFile两次makeJPG()而不是处理对象.Image.FromFile通常会创建需要处理的非托管GDI +句柄.
从文档:
文件保持锁定状态,直到图像被丢弃.
所以乍一看我假设你只是在内存中加载太多图像,我会尝试:
private void makeJPG(string source, string background, string output)
{
using(var backgroundImg = Image.FromFile(background))
using(var sourceImg = Image.FromFile(source))
{
float BGimg = backgroundImg.Height;
float SubjectImg = sourceImg.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();
}
}
Run Code Online (Sandbox Code Playgroud)
因为你实际上没有使用图像(只是获得它们的高度),你可以让这些using块更小,但我会把它留给你.
......但......
此外,你说它恰好发生在22张图片上(这对于内存不足会很奇怪,除非图像一直很大),但是阅读相同的文档:
如果文件没有有效的图像格式或者GDI +不支持文件的像素格式,则此方法抛出OutOfMemoryException异常.
因此,请确保第22个图像("源"或"背景",取决于它抛出的位置)具有正确的格式
| 归档时间: |
|
| 查看次数: |
409 次 |
| 最近记录: |