UWP / C# 旋转 BMP

UWP / C# Rotating BMP

这个问题好像已经有人问过了,但是我找不到相关的答案。

我正在将 BMP 图像加载到 UWP 应用程序的内存中,我想将其旋转 90、180 或 270,但我找不到执行此操作的方法。

imgSource.rotate() 好像不存在了 RotateTransform 适用于 xaml ....

谁能偶然添加缺少的代码吗?

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);
        }
    }

The RotateTransform works with xaml

如您所知,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,您可以旋转 ImageControl,因为它是 UIElement。例如,如果我们有 ImageControl 如下:

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

我们可以简单地旋转angle 属性,代码如下:

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

如果您需要旋转图像文件而不是 UIElement,您可能需要像您已经做的那样解码图像文件,然后通过设置 BitmapTransform.Rotation 属性.代码如下:

  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);
      }
  }

更多关于图像文件旋转的功能你可以使用其他APIs在Windows.Graphics.Imaging namespace. And the scenario 2 of SimpleImaging 官方示例中提供了关于图像旋转的完整示例你可以参考。