UWP/C#旋转BMP [英] UWP / C# Rotating BMP

查看:87
本文介绍了UWP/C#旋转BMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在将BMP图像加载到UWP应用程序中的内存中,我想将其旋转90、180或270,但我只是找不到解决方法.

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.

imgSource.rotate()似乎不再存在 RotateTransform与xaml一起使用 ....

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

推荐答案

RotateTransform与xaml一起使用

The RotateTransform works with xaml

您知道, RotateTransform 用于uwp应用程序中的旋转变换XAML. RotateTransform角度,使对象绕圆点CenterX,CenterY通过圆弧旋转.但是通常使用转换来填充 UIElement.RenderTransform 属性,因此如果您将图像源加载到

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"/> 

我们可以简单地通过代码的angle属性将其旋转为:

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

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

如果您需要旋转图像文件而不是UIElement,则可能需要像以前一样对图像文件进行解码,然后通过设置

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

有关图像文件旋转的更多功能,您可以在 Windows.Graphics.Imaging 名称空间.而 SimpleImaging 官方示例的方案2提供了完整的示例您可以参考有关图像旋转的示例.

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.

这篇关于UWP/C#旋转BMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆