Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to rotate 180 degrees and crop with the Jpeg bitmap encoder #4386

Open
bjorn-malmo opened this issue May 3, 2024 · 0 comments
Open

Comments

@bjorn-malmo
Copy link

Describe the bug

When specifying BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees and setting BitmapTransform.Bounds for the Jpeg Bitmap encoder, an ArgumentException is thrown.

Specifying a different rotation flag or excluding the Bounds or choosing a different encoder (Png/Bitmap) it is successful.

Steps to reproduce the bug

Create a Windows application

  • Target framework 8.0
  • Any target/supported OS version

The code example below can be used to reproduce the issue

using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;

internal class Program
{
    private static async Task Main(string[] args)
    {
#if true
        var http = new HttpClient();
        var response = await http.GetAsync("https://distributedmedical.com/wp-content/uploads/2020/05/DM-Logo_white.png");
        var source = response.Content.ReadAsStream().AsRandomAccessStream();
#else
        var file = await StorageFile.GetFileFromPathAsync(@"C:\Users\bjorn\Downloads\temp.jpg");
        var source = await file.OpenReadAsync();
#endif

        // Get bitmap from stream
        var decoder = await BitmapDecoder.CreateAsync(source);
        var bitmap = await decoder.GetSoftwareBitmapAsync();

        // Create rotated and cropped Jpeg        
        var destination = new InMemoryRandomAccessStream();

        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destination);
        encoder.SetSoftwareBitmap(bitmap);
        encoder.IsThumbnailGenerated = false;

        // The combination of rotation = 180 degrees and specified Bounds will throw exception
        encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
        encoder.BitmapTransform.Bounds = new BitmapBounds(10, 10, 10, 10);

        await encoder.FlushAsync();
    }
}

Expected behavior

The input parameters in the example provided should generate an output image rotated and cropped.

Screenshots

No response

NuGet package version

None

Packaging type

Unpackaged

Windows version

Windows 11 version 22H2 (22621, 2022 Update)

IDE

Visual Studio 2022

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants