【问题标题】:UWP / C# Rotating BMPUWP / C# 旋转 BMP
【发布时间】:2017-06-22 19:36:33
【问题描述】:

这个问题似乎已经被问过了,但是我找不到相关的答案。

我在 UWP 应用程序中将 BMP 图像加载到内存中,我想将其旋转 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);
        }
    }

【问题讨论】:

    标签: c# image rotation uwp bmp


    【解决方案1】:

    RotateTransform 适用于 xaml

    如您所知,RotateTransform 用于 uwp 应用 XAML 中的旋转变换。 RotateTransformAngle 定义,它通过围绕点 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 命名空间下的其他 API。而SimpleImaging 官方示例的场景二提供了完整的图片旋转示例,大家可以参考。

    【讨论】:

    • 这太棒了,按预期工作。事实上,场景 2 是需要的。非常感谢孙婷。
    • 这个解决方案很棒!但是,您知道如何仅使用 XAML 定义和绑定来完成吗? (您可能会在我的问题中看到更多详细信息:stackoverflow.com/q/53176985/997940
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2013-11-16
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多