dotnet / dotnet/wpf

JpegBitmapEncoder default QualityLevel issue

Open
#11,803 2 comments 1 reaction 0 assignees View on GitHub
Bug Cost:S Priority:3
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

If to set QualityLevel of JpegBitmapEncoder to 75, the quality level of a generated image will be 90, not 75.

This happens because SetupFrame method checks if the value of the level is a default one (that is 75) and doesn't set ImageQuality property of the native encoder.

But, according to https://learn.microsoft.com/en-us/windows/win32/wic/jpeg-format-overview the default value of the quality in the native encoder is not 0.75, but 0.9.

### Reproduction Steps

Execute the following code:

```
using var imageStreamSource = new FileStream("input.png", FileMode.Open, FileAccess.Read, FileShare.Read);

var decoder = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

var frame = decoder.Frames[0];

var encoder = new JpegBitmapEncoder()
{
QualityLevel = 75,
};

encoder.Frames.Add(decoder.Frames[0]);

using var fileStream = new FileStream("output.jpg", FileMode.Create);
encoder.Save(fileStream);
```

### Expected behavior

Saved JPEG image has Quality Level 75

### Actual behavior

Saved JPEG image has Quality Level 90

### Regression?

_No response_

### Known Workarounds

_No response_

### Impact

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by locating the JpegBitmapEncoder SetupFrame method and inspect how QualityLevel is passed to the native encoder, then run the supplied PNG-to-JPEG reproduction. Done means setting QualityLevel to 75 produces a saved JPEG with quality level 75 rather than 90.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.