Skip to content

Fix EXIF UserComment encoding for non-ASCII comments#353

Closed
deeferentleeg wants to merge 1 commit into
Ruben2776:devfrom
deeferentleeg:fix/issue-324-exif-usercomment-encoding
Closed

Fix EXIF UserComment encoding for non-ASCII comments#353
deeferentleeg wants to merge 1 commit into
Ruben2776:devfrom
deeferentleeg:fix/issue-324-exif-usercomment-encoding

Conversation

@deeferentleeg

Copy link
Copy Markdown
Contributor

Fixes #324

Root cause

GetUserComment decoded the entire UserComment byte array with Encoding.ASCII, including the 8-byte EXIF encoding identifier that prefixes the value. This caused three problems:

  • The encoding header bytes were decoded as garbage and included in the output.
  • Comments with an ASCII prefix were discarded (string.Empty returned).
  • Comments with a UNICODE prefix were never re-decoded as UTF-16LE — only a string Replace("UNICODE", "") was performed on the already-ASCII-decoded text, so CJK and other non-ASCII comments stayed garbled.

Fix

Read the first 8 bytes as the encoding identifier and decode only the remaining bytes (commentBytes[8..]) with the encoding indicated by the header, per the EXIF UserComment specification:

  • ASCIIEncoding.ASCII
  • UNICODEEncoding.Unicode (UTF-16LE)
  • JIS → UTF-8 fallback (true JIS X0208 decoding is rare in the wild; UTF-8 is a safe best-effort)
  • undefined / all-NUL header → Encoding.UTF8

Trailing NUL padding and whitespace are trimmed from the decoded result, which is returned as string.Empty when null/empty/whitespace.

Notes

  • No public API change; single method replaced.
  • Preserves the existing null/length guard and try/catch defensive style.
  • Uses only System.Text encodings already imported (Encoding.ASCII, Encoding.Unicode, Encoding.UTF8).

Fixes Ruben2776#324

The UserComment tag was decoded with Encoding.ASCII over the entire
byte array, including the 8-byte EXIF encoding header. This garbled
the header bytes into the output, discarded ASCII-prefixed comments,
and never re-decoded UNICODE-prefixed comments as UTF-16LE (only a
string Replace was applied), so CJK and other non-ASCII comments
stayed mojibake.

Read the 8-byte header and decode only the remaining bytes with the
encoding it indicates per the EXIF UserComment spec: ASCII, UNICODE
(UTF-16LE), JIS (UTF-8 fallback), or undefined (UTF-8). Trim trailing
NUL padding and whitespace. No public API change; single method.
@Ruben2776

Copy link
Copy Markdown
Owner

Chinese characters just turn into question marks with your changes. It doesn't fix it.

@Ruben2776

Copy link
Copy Markdown
Owner

Thank you for the PR, however it got fixed in #359

@Ruben2776 Ruben2776 closed this Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EXIF property "Comment" decoding incorrect

2 participants