UWP / C# Rotating BMP

2019-07-13 02:15发布

The question seems to be asked already, however I cannot find a relevant answer.

I am loading a BMP image to memory in a UWP app, and I would like to rotate it by either 90, 180 or 270, but I just cannot find the way to do this.

The imgSource.rotate() does not seem to exist anymore The RotateTransform works with xaml ....

Could anyone add the missing code by a chance please?

public async Task LoadImage()
    {
        StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("test.bmp");
        using (var stream = await file.OpenAsync(FileAccessMode.Read))
        {
            var decoder = await BitmapDecoder.CreateAsync(stream);
            bitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
            var imgSource = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);

            // Code to rotate image by 180 to be added

            bitmap.CopyToBuffer(imgSource.PixelBuffer);
        }
    }

1条回答
你好瞎i
2楼-- · 2019-07-13 02:32

The RotateTransform works with xaml

As you known, RotateTransform is for rotate transform in uwp app XAML. A RotateTransform is defined by an Angle that rotates an object through an arc around the point CenterX, CenterY. But a transform is typically used to fill the UIElement.RenderTransform property, so if you load the image source to an ImageControl, you can rotate the ImageControl since it is a UIElement. For example, if we have ImageControl as follows:

<Image x:Name="PreviewImage" Height="400" Width="300" AutomationProperties.Name="Preview of the image"  Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center"/> 

We can simply rotate it by angle property by code as:

RotateTransform m_transform = new RotateTransform(); 
PreviewImage.RenderTransform = m_transform;
m_transform.Angle = 180;

If you need rotate an image file not a UIElement, you may need to decode the image file as what you already did and then encode the file with setting the BitmapTransform.Rotation property. Code as follows:

  double m_scaleFactor;
  private async void btnrotatefile_Click(object sender, RoutedEventArgs e)
  {
      StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("test.bmp"); 
      using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite),
                                         memStream = new InMemoryRandomAccessStream())
      {
          BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); 
          uint originalWidth = decoder.PixelWidth;
          uint originalHeight = decoder.PixelHeight;
          BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
          if (m_scaleFactor != 1.0)
          {
              encoder.BitmapTransform.ScaledWidth = (uint)(originalWidth * m_scaleFactor);
              encoder.BitmapTransform.ScaledHeight = (uint)(originalHeight * m_scaleFactor);
              encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
          }

         //Rotate 180
          encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
          await encoder.FlushAsync(); 
          memStream.Seek(0);
          fileStream.Seek(0);
          fileStream.Size = 0;
          await RandomAccessStream.CopyAsync(memStream, fileStream);
      }
  }

More features about the image file rotation you can use other APIS under Windows.Graphics.Imaging namespace. And the scenario 2 of SimpleImaging official sample provides a complete sample about image rotation you can reference.

查看更多
登录 后发表回答