locked
rotation using bitmapencoder RRS feed

  • Question

  • Hi,

    I am using the following code to save an image

    using (IRandomAccessStream fileStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, fileStream); // Get the pixels from the existing WriteableBitmap WriteableBitmap bitmap = rotatingimage.Source as WriteableBitmap; byte[] pixels = new byte[4 * bitmap.PixelWidth * bitmap.PixelHeight]; using (Stream pixelStream = bitmap.PixelBuffer.AsStream()) { await pixelStream.ReadAsync(pixels, 0, pixels.Length); } // Write those pixels to the first frame encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 72, 72, pixels); encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise180Degrees; await encoder.FlushAsync(); }

    however instead of

     encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise180Degrees;

    I want to set some different rotation angle other than defaults.

    how can I do this

    • Moved by Mike Danes Saturday, August 10, 2013 9:25 AM WinStore specific
    Saturday, August 10, 2013 8:01 AM

Answers

  • BitmapEncoder cannot rotate to arbitrary angles. You will need to rotate the buffer yourself before passing it to the BitmapEncoder. You may be able to find third party code to help with this or interop with Direct2d. If you want to stay purely within Xaml you will have to perform the rotation yourself: iterate through the target pixel buffer and calculate the new value for each pixel based on its rotated position from the original.

    --Rob

    • Marked as answer by Anne Jing Friday, August 16, 2013 1:10 AM
    Sunday, August 11, 2013 2:32 PM
    Moderator

All replies

  • BitmapEncoder only supports the BitmapRotation rotations: it can rotate by multiples of 90 degrees.

    If you want to rotate to an arbitrary angle you will need to perform the rotation yourself before passing the pixel buffer to the BitmapEncoder. There is no built in code to implement this in Xaml, but third party libraries such as WriteableBitmapEx may help. The math isn't hard, but you will need to decide how to deal with the the rotated image being a different size from the original.

    --Rob

    Saturday, August 10, 2013 3:28 PM
    Moderator
  • Hi,

    I have performed rotation on image using following code

    var x = this.RightRotateTransform.CenterX - e.Position.X;
                var y = this.RightRotateTransform.CenterY - e.Position.Y;
    
                double a1 = Math.Atan(y / x);
                double a2 = Math.Atan((e.Delta.Translation.Y - y) / (x - e.Delta.Translation.X));
    
                this.RightRotateTransform.Angle += a1 - a2;

    and I got the angle in this.RightRotateTransform.Angle...

    I want to use this value for rotation

    Sunday, August 11, 2013 2:04 PM
  • BitmapEncoder cannot rotate to arbitrary angles. You will need to rotate the buffer yourself before passing it to the BitmapEncoder. You may be able to find third party code to help with this or interop with Direct2d. If you want to stay purely within Xaml you will have to perform the rotation yourself: iterate through the target pixel buffer and calculate the new value for each pixel based on its rotated position from the original.

    --Rob

    • Marked as answer by Anne Jing Friday, August 16, 2013 1:10 AM
    Sunday, August 11, 2013 2:32 PM
    Moderator