使用过程的内存不足

时间:2015-01-23 16:39:15

标签: 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();
        }

1 个答案:

答案 0 :(得分:5)

乍一看,您在Image.FromFile中使用了makeJPG()两次并且没有处理这些对象。 Image.FromFile通常会创建需要处理的非托管GDI +句柄。

来自documentation

  

文件保持锁定状态,直到图像被丢弃。

所以乍一看我假设你只是在内存中加载了太多的图片,我试试:

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();
  }
}

由于您实际上没有使用图像(只是获得它们的高度),您可以将这些using块缩小,但我会留给您。

......但......

关于OutOfMemoryException

另外,你说它完全出现在22张图片中(除非图像一直很大,这对于内存不足会很奇怪),但阅读相同的文档:

  

如果文件没有有效的图像格式,或者GDI +不支持文件的像素格式,则此方法会抛出OutOfMemoryException异常。

所以请确保第22张图片("来源"或"背景",取决于它所投放的位置)具有正确的格式

相关问题