c# - 通过代码C# UWP旋转位图

标签 c# bitmap rotation windows-10 uwp

如何将位图顺时针旋转 90 度?我正在使用下一个代码从存储中读取图像:

public async void GetImage()
    {
        string filename = code + ".jpg";

        Windows.Storage.StorageFile sampleFile = 
            await Windows.Storage.KnownFolders.CameraRoll.GetFileAsync(filename);


        BitmapImage img = new BitmapImage();

        img = await LoadImage(sampleFile);

        imageMain.Source = img;

    }

        private static async Task<BitmapImage> LoadImage(StorageFile file)
    {
        BitmapImage bitmapImage = new BitmapImage();
        FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);

        bitmapImage.SetSource(stream);

        return bitmapImage;
    }

我想要旋转图像。 bitmapImage.Rotate 在 UWP 上不起作用。什么解决方案?

最佳答案

这可以通过RotateTransform来实现类

代码隐藏

RotateTransform _rotateTransform = new RotateTransform()
{
    CenterX = imageMain.ActualWidth/2,
    CenterY = imageMain.ActualHeight/2,
    Angle = 90
};
imageMain.RenderTransform = _rotateTransform;

XAML

<Image Stretch="Fill" Source="Assets/Windows.jpg" x:Name="imageMain" Width="500" Height="500">
    <Image.RenderTransform>
        <RotateTransform CenterX="250" CenterY="250" Angle="90"/>
    </Image.RenderTransform>
</Image>

** 如果您注意到,您会发现 CenterXCenterY 大小是实际图像的一半。

祝你好运。

关于c# - 通过代码C# UWP旋转位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39209084/

相关文章:

java - 通知远程 View 中的位图无法回收

java - 如何在android中将位图转换为字节数组,将字节数组转换为位图?

c# - 尝试将图像保存到 MemoryStream 时 GDI+ 异常中发生一般错误

HTML + CSS : How to create a auto-resizing rotated background?

iphone - 将加速度计输出旋转到新的 "neutral",适用于 iOS(和其他)。

SVG以厘米为单位旋转

c# - C# 中的 connection.Dispose() 是什么意思?

c# - 当用户连接到集线器类时,公共(public)任务 Connect() 不会触发

c# - 使用 OleDB 读取 CSV 数据的文件访问错误

c# - WPF 资源覆盖