Ok, so I know I should be using the ImageMagick DLL... and I'm learning, testing, and working up to doing that. But in the mean time I'm using the inefficient method of calling Imagemagick's convert.exe via a process.
It works fine with hundreds of images when I'm testing. But then I move my WindowsForms program to a faster machine and it crashes every time at the same point.
It's a two step Process call. The first time loop through all the files and produce the PNGs. Then I loop through all the PNGs and composite it with a background and output a JPG. But EVERY time at exactly 22 images it errors "System.OutOfMemoryException: Out of memory." Is there something that is filling up that I need to kill or something?
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();
}