WindowsAppSDK icon indicating copy to clipboard operation
WindowsAppSDK copied to clipboard

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

Open bjorn-malmo opened this issue 1 year ago • 0 comments

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

bjorn-malmo avatar May 03 '24 10:05 bjorn-malmo