Tichau/FileConverter · error · Exception

The conversion preset must be valid.

Error message

The conversion preset must be valid.

What it means

ConversionJob_ImageMagick.Convert() throws Exception when this.ConversionPreset is null before branching on isInputFilePdf (ConvertPdf() vs single-page MagickReadSettings path) and writing images via ImageMagick. This job is what ConversionJobFactory.Create returns for Pdf, Avif, Jpg, Png and Webp outputs, so it is a high-traffic converter; the guard protects the MagickNET calls that would NRE on a null preset. As elsewhere, the real ctor rejects null presets, so the throw is reachable via the parameterless design-mode ctor or direct/reflection Convert().

Source

Thrown at Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs:62

            {
                using (MagickImageCollection images = new MagickImageCollection())
                {
                    MagickReadSettings settings = new MagickReadSettings();
                    settings.Density = new Density(1, 1);
                    images.Read(this.InputFilePath);

                    return images.Count;
                }
            }

            return 1;
        }

        protected override void Convert()
        {
            if (this.ConversionPreset == null)
            {
                throw new Exception("The conversion preset must be valid.");
            }

            this.CurrentOutputFilePathIndex = 0;

            if (this.isInputFilePdf)
            {
                this.ConvertPdf();
            }
            else
            {
                this.pageCount = 1;
                MagickReadSettings readSettings = new MagickReadSettings();

                string inputExtension = System.IO.Path.GetExtension(this.InputFilePath).ToLowerInvariant();
                switch (inputExtension)
                {
                    case ".avif":
                        // Explicitly set AVIF format for proper reading

View on GitHub (pinned to 6c157a411f)

Solutions

  1. Create via new ConversionJob_ImageMagick(conversionPreset, inputFilePath) or ConversionJobFactory.Create.
  2. Use PrepareConversion() + StartConversion() rather than calling Convert().
  3. Early-return in design-time when ConversionPreset is null.
  4. Ensure preset deserialization from Settings.user.xml succeeded before job creation.

Example fix

// before
var job = new ConversionJob_ImageMagick();
job.Convert(); // throws: preset null

// after
var job = ConversionJobFactory.Create(conversionPreset, inputFilePath);
job.PrepareConversion(outputFilePaths);
job.StartConversion();
Defensive patterns

Strategy: validation

Validate before calling

if (job == null || job.ConversionPreset == null)
{
    throw new InvalidOperationException(
        "ConversionJob_ImageMagick requires a non-null ConversionPreset; use ConversionJobFactory.Create.");
}
job.StartConversion();

Type guard

private static bool HasValidPreset(ConversionJob job) => job != null && job.ConversionPreset != null;

Try / catch

try
{
    job.StartConversion();
}
catch (Exception ex) when (ex.Message.Contains("conversion preset must be valid"))
{
    logger.Error(ex, "ImageMagick job created without a preset; rebuilding.");
    job = ConversionJobFactory.Create(LoadPreset(), inputPath);
}

Prevention

When it happens

Trigger: ConversionJob_ImageMagick built via its parameterless ctor then Convert() invoked; or Convert() called via reflection on an instance whose preset is null. The factory requires a non-null preset, so the normal factory path cannot reach this guard.

Common situations: XAML design-time instantiation; tests calling Convert() directly; an image/PDF conversion pipeline that created the job before the preset was loaded; a serialization path that reconstituted the job without a preset.

Related errors


AI-assisted analysis of Tichau/FileConverter@6c157a411f (2026-08-13). Data as JSON: /api/errors/fa9d6f0da0edf3b8. Report an issue: GitHub.