dotnet / dotnet/wpf

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

Open
#11,559 2 comments 0 reactions 0 assignees View on GitHub
External Non-WPF Issue
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### 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](url)
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.