dotnet/wpf · error · InvalidOperationException

Image_EncoderNoPreview

Error message

Image_EncoderNoPreview

What it means

The BitmapEncoder.Preview setter rejects a preview image when the codec does not support a container-level preview (_supportsPreview is false), throwing InvalidOperationException(Image_EncoderNoPreview). Previews are only supported by codecs that can embed them (e.g. HD Photo/WMP).

Solutions

  1. Only set Preview on codecs that support it (e.g. WmpBitmapEncoder)
  2. Remove the Preview assignment for other formats
  3. Use the frame Thumbnail support instead where the codec allows it
  4. Gate the assignment on codec capability info

Example fix

// before
pngEncoder.Preview = preview; // throws
// after
if (encoder is WmpBitmapEncoder wmp) { wmp.Preview = preview; }
Defensive patterns

Strategy: type-guard

Validate before calling

if (encoder is not WmpBitmapEncoder) preview = null; // only WMP supports container previews

Type guard

static bool SupportsPreview(BitmapEncoder e) => e is WmpBitmapEncoder;

Try / catch

try { encoder.Preview = previewImage; }
catch (InvalidOperationException) { /* preview unsupported by codec; drop it */ }

Prevention

When it happens

Trigger: Assigning encoder.Preview = someBitmapSource on an encoder whose codec lacks preview support (checked in the setter after EnsureBuiltIn and null checks).

Common situations: Trying to embed previews in Png/Jpeg/Tiff encoders, which do not support global previews; code copied from WMP/HD Photo workflows.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/b4c31a1a1e4499f8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs:222

        /// </summary>
        public virtual BitmapSource Preview
        {
            get
            {
                VerifyAccess();
                EnsureBuiltIn();
                return _preview;
            }
            set
            {
                VerifyAccess();
                EnsureBuiltIn();

                ArgumentNullException.ThrowIfNull(value);

                if (!_supportsPreview)
                {
                    throw new InvalidOperationException(SR.Image_EncoderNoPreview);
                }

                _preview = value;
            }
        }

        /// <summary>
        /// The info that identifies this codec.
        /// </summary>
        public virtual BitmapCodecInfo CodecInfo
        {
            get
            {
                VerifyAccess();
                EnsureBuiltIn();
                EnsureUnmanagedEncoder();

                // There should always be a codec info.

View on GitHub (pinned to 81131a70a4)