Browse Source

refactored using an ImageMagick wrapper

Michael Hoskins 5 years ago
parent
commit
3415671e2a

+ 1 - 2
MovieBarcodeGenerator/App.config

@@ -8,8 +8,7 @@
   </startup>
   
   <appSettings>
-    <add key="folderImagick" value="C:\utils\ImageMagick-6.9.2-Q16" />
-    <add key="folderFFMpeg" value="C:\utils\ffmpeg\bin" />
+    <add key="folderFFMpeg" value="C:\utils\ffmpeg-20190210\bin" />
   </appSettings>
   
   <log4net>

+ 12 - 12
MovieBarcodeGenerator/BarcodeGenerator.cs

@@ -1,4 +1,5 @@
-using System;
+using ImageMagick;
+using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
@@ -26,10 +27,6 @@ namespace MovieBarcodeGenerator {
 
         public static void Generate(string inputFile, string outputFile, int height, int barWidth, int iterations) {
             log.Debug("Generate()");
-            if (!Directory.Exists(imagickPath)) {
-                log.ErrorFormat("ImageMagick was not found at '{0}'.", imagickPath);
-                throw new Exception("ImageMagick was not found.");
-            }
             if (File.Exists(outputFile)) {
                 log.InfoFormat("Output file '{0}' exists. Deleting file.", outputFile);
                 File.Delete(outputFile);
@@ -49,13 +46,16 @@ namespace MovieBarcodeGenerator {
 
             log.Debug("Scrunching PNGs together.");
             // use ImageMagick to crush the generated PNGs together
-            Process p = new Process();
-            p.StartInfo.FileName = Path.Combine(imagickPath, "convert.exe");
-            p.StartInfo.WorkingDirectory = Path.GetDirectoryName(outputFile);
-            p.StartInfo.Arguments = String.Format("out_*.png +append \"{0}\"", outputFile);
-            p.StartInfo.UseShellExecute = false;
-            p.Start();
-            p.WaitForExit();
+            using (MagickImageCollection images = new MagickImageCollection()) {
+                foreach (var file in Directory.GetFiles(Path.GetDirectoryName(outputFile), "out_???.png")) {
+                    images.Add(new MagickImage(file));
+                }
+                // create a strip from all those images
+                using (IMagickImage result = images.AppendHorizontally()) {
+                    // Save the result
+                    result.Write(outputFile);
+                }
+            }
 
             log.Debug("Deleting temporary work files.");
             // clean up the work files

+ 4 - 0
MovieBarcodeGenerator/MovieBarcodeGenerator.csproj

@@ -36,6 +36,9 @@
       <HintPath>..\packages\log4net.2.0.4\lib\net45-full\log4net.dll</HintPath>
       <Private>True</Private>
     </Reference>
+    <Reference Include="Magick.NET-Q8-AnyCPU, Version=7.11.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
+      <HintPath>..\packages\Magick.NET-Q8-AnyCPU.7.11.0\lib\net40\Magick.NET-Q8-AnyCPU.dll</HintPath>
+    </Reference>
     <Reference Include="NDesk.Options, Version=0.2.1.0, Culture=neutral, processorArchitecture=MSIL">
       <HintPath>..\packages\NDesk.Options.0.2.1\lib\NDesk.Options.dll</HintPath>
       <Private>True</Private>
@@ -43,6 +46,7 @@
     <Reference Include="System" />
     <Reference Include="System.Configuration" />
     <Reference Include="System.Core" />
+    <Reference Include="System.Drawing" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="Microsoft.CSharp" />

+ 1 - 0
MovieBarcodeGenerator/packages.config

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
   <package id="log4net" version="2.0.4" targetFramework="net45" />
+  <package id="Magick.NET-Q8-AnyCPU" version="7.11.0" targetFramework="net45" />
   <package id="NDesk.Options" version="0.2.1" targetFramework="net45" />
 </packages>