在Win2D中旋转图像 [英] Rotate Image in Win2D

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

问题描述

我正在尝试旋转图像,但无法获得预期的结果. 我已经尝试过WIn2D,但无法按预期制作图像.

I'm trying to rotate image but couldn't get expected result. I've tried with WIn2D but couldn't make image as expected.

我尝试过的代码

public async void Rotate(string originalImagepath, Rect originalImageRect, float degrees)
    {
        int height = (int)Math.Sqrt(originalImageRect.Width * originalImageRect.Width + originalImageRect.Height * originalImageRect.Height);

        CanvasDevice device = CanvasDevice.GetSharedDevice();
        CanvasRenderTarget webCardImage = null;
        CanvasBitmap bitmap = null;
        var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
        Vector2 endpoint = new Vector2((float)originalImageRect.Width / 2, (float)originalImageRect.Height / 2);

        try
        {
            webCardImage = new CanvasRenderTarget(device, height, height, logicalDpi);
            using (var ds = webCardImage.CreateDrawingSession())
            {
                ds.Clear(Colors.Transparent);

                using (FileStream imageStream = new FileStream(originalImagepath, FileMode.Open))
                {
                    IRandomAccessStream fileStream = imageStream.AsRandomAccessStream();
                    bitmap = await CanvasBitmap.LoadAsync(device, fileStream);
                }

                ICanvasImage image = new Transform2DEffect
                {
                    Source = bitmap,
                    TransformMatrix = Matrix3x2.CreateRotation(degrees, endpoint),
                };
                var sourceRect = image.GetBounds(ds);
                ds.DrawImage(image, new Rect(originalImageRect.X, originalImageRect.Y, originalImageRect.Width, originalImageRect.Height), sourceRect, 1, CanvasImageInterpolation.HighQualityCubic);

            }
        }
        catch (Exception ex)
        {

        }
        //Save Image Code here
    }

预期输出:

我生成的图像:

注意:Rect originalImageRect指的是我要旋转的主图像.

NB: Rect originalImageRect refers main image rect which i want to rotate.

推荐答案

发生这种情况时,这意味着旋转图像后,目标渲染矩形的大小(可以是高度或宽度)小于的大小.旋转的图像,从而导致图像压缩.

When this happens, it means that after your image is rotated, the size of the target rendering rectangle (may be height or width) is smaller than the size of the rotated image, resulting in compression of the image.

我分析了您的代码,您尝试创建一个以originalImageRect对角线长度为边长的正方形,以确保绕中心旋转时图像不会超出渲染范围.

I analyzed your code, you tried to create a square with the originalImageRect diagonal length as the side length, to ensure that the image will not exceed the rendering range when rotating around the center.

这很好,但是渲染时会有一些偏差.

This is good, but there are some deviations when rendering.

默认情况下,图像位于CanvasRenderTarget的左上角.如果不执行位移,则旋转时中心点将位于左上方,这将仍然超出渲染范围.

The image is in the upper left corner of CanvasRenderTarget by default. If no displacement is performed, the center point is on the upper left when rotating, which means it will still exceed the rendering range.

您可以尝试以下操作:

double height = Math.Sqrt(originalImageRect.Width * originalImageRect.Width + originalImageRect.Height * originalImageRect.Height);
float y = (float)((height - originalImageRect.Height) / 2.0f);
float x = (float)((height - originalImageRect.Width) / 2.0f);
Vector2 endpoint = new Vector2((float)height / 2, (float)height / 2);

try
{
    webCardImage = new CanvasRenderTarget(MyCanvas, (float)height, (float)height, logicalDpi);
    using (var ds = webCardImage.CreateDrawingSession())
    {
        // init CanvasBitmap

        ICanvasImage image = new Transform2DEffect
        {
            Source = bitmap,
            TransformMatrix = Matrix3x2.CreateTranslation(new Vector2(x,y))
                              * Matrix3x2.CreateRotation(degrees,endpoint)
        };
        var sourceRect = image.GetBounds(ds);
        ds.DrawImage(image);
    }

}
catch (Exception ex)
{

}

我们首先移动图像,将其移动到画布的中心,然后围绕画布的中心旋转,以实现我们的目标.

We first shift the image, move it to the center of the canvas, and then rotate it around the center of the canvas, so that we can achieve our goal.

此外,使用DrawImage()方法时,无需为渲染约束额外定义Rect.

In addition, when using the DrawImage() method, there is no need to additionally define Rect for rendering constraints.

谢谢.

这篇关于在Win2D中旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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