繁体   English   中英

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

RotateTransform与xaml一起使用

如您所知, RotateTransform用于uwp应用XAML中的旋转变换。 RotateTransform由一个角度定义,该角度使对象绕RotateTransform CenterX,CenterY通过圆弧旋转。 但是通常使用转换来填充UIElement.RenderTransform属性,因此,如果将图像源加载到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);
      }
  }

有关图像文件旋转的更多功能,可以在Windows.Graphics.Imaging命名空间下使用其他APIS。 SimpleImaging官方示例的场景2提供了有关图像旋转的完整示例,您可以参考该图像旋转。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM