Skip to content

PNG image iTXt metadata broke sometime in the last 6-9 months #11559

@muebel201

Description

@muebel201

Description

We have code for setting iTXt metadata in a PNG image roughly equivalent to the last answer in:
https://stackoverflow.com/questions/26225373/edit-png-metadata-add-itxt-value
Code repeated here:
`// Source - https://stackoverflow.com/a/38254480
// Posted by jmik, modified by community. See post 'Timeline' for change history
// Retrieved 2026-04-02, License - CC BY-SA 3.0

using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PNGEncoder
{
class Program
{
static void Main(string[] args)
{
var width = 256;
var height = 256;

        var pngMetadata = new BitmapMetadata("png");

        pngMetadata.SetQuery("/iTXt/Keyword", "keyword0".ToCharArray());
        pngMetadata.SetQuery("/iTXt/TextEntry", "textentry0");

        pngMetadata.SetQuery("/[1]iTXt/Keyword", "keyword1".ToCharArray());
        pngMetadata.SetQuery("/[1]iTXt/TextEntry", "textentry1");

        var bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray8, null);

        var pixels = new byte[width * height];
        for (var y = 0; y < height; y++)
        {
            for (var x = 0; x < width; x++)
            {
                pixels[y * width + x] = (byte)(255 * (((x >> 4) ^ (y >> 4)) & 1));
            }
        }

        bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width, 0);

        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmap, null, pngMetadata, null));

        using (var stream = File.Create("checkerBoard.png"))
        {
            encoder.Save(stream);
        }
    }
}

}
`
The code has been working for years. Sometime in the last 6-9 months, it appears that a Windows update package has broken the iTXt metadata function. Rather than inserting the specified "TextEntry", it just inserts what appears to be an empty XMP-based iTXt block. The code in questions functions correctly on all (tested) PCs running Windows 11 Update 22H2, but does not function on any (tested) PCs running Windows 11 Update 25H2.

Reproduction Steps

See Description.

Expected behavior

See Description.

Actual behavior

See Description.

Regression?

See Description.

Known Workarounds

None known.

Impact

Breaks all applications using the feature.

Configuration

See Description

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions