1

In a Windows Store app I want to crop a rotated rectangle part of a WriteableBitmap like Case 2 in the following image.

Image

  • I have P0, Width, Height and P1 and rotation angle of the rectangle.
  • Rotation is based on center of each rectangle

I am using Crop extension method available in WriteableBitmapEx.WinRT for Cropping.

In Case 1 I am doing these:

            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(PhotoGrid);

            WriteableBitmap bitmapImage = new WriteableBitmap(renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight);

            IBuffer pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
            using (var stream = new InMemoryRandomAccessStream())
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, 96, 96, pixelBuffer.ToArray());

                await encoder.FlushAsync();
                stream.Seek(0);

                bitmapImage.SetSource(stream);
            }

            // Redraw the WriteableBitmap
            bitmapImage.Invalidate();

            SampleImage.Source = bitmapImage.Crop(new Rect(p0.X, p0.Y, width, height));

But when rotation come in place I don't know what calculation should be applied do to crop like case 2.

Would any of you be kind enough to assist?

Thanks!

Clemens
  • 123,504
  • 12
  • 155
  • 268
Ali
  • 83
  • 3
  • 10

1 Answers1

2

The WriteableBitmapEx library implements the RotateFree method.

bitmapImage.RotateFree(70);

http://writeablebitmapex.codeplex.com/SourceControl/changeset/82055

Olivier Payen
  • 15,198
  • 7
  • 41
  • 70
  • Thanks for the reply. I know it has a such a rotate method but my question was how to crop that rotated area not to rotate. Consider that you have an image, there is an adorner on top like the rectangles that can be rotated. then how to crop that rotate area inside the rectangle was the question. – Ali Jan 16 '14 at 00:13
  • You could rotate the full image and then crop the rectangle. – Olivier Payen Jan 16 '14 at 16:16
  • My first approach is to find a way without need to rotate the image itself. Let me clarify my question better: The Question is How to calculate the points position after rotation to be able to use them for the crop method. In fact, if you try to do a sample for yourself the challenge/question will get obvious. – Ali Jan 16 '14 at 17:28