旋转后的图像失真

问题描述 投票:-1回答:1

我正在使用以下代码旋转QR码:

private static Image RotateImage(Image image, float angle)
    {
        float height = image.Height;
        float width = image.Width;
        int hypotenuse = System.Convert.ToInt32(System.Math.Floor(Math.Sqrt(height * height + width * width)));
        Bitmap rotatedImage = new Bitmap(hypotenuse, hypotenuse);
        using (Graphics g = Graphics.FromImage(rotatedImage))
        {
            g.TranslateTransform((float)rotatedImage.Width / 2, (float)rotatedImage.Height / 2); //set the rotation point as the center into the matrix
            g.RotateTransform(angle); //rotate
            g.TranslateTransform(-(float)rotatedImage.Width / 2, -(float)rotatedImage.Height / 2); //restore rotation point into the matrix
            g.DrawImage(image, (hypotenuse - width) / 2, (hypotenuse - height) / 2, width, height);
        }
        return rotatedImage;
    }

然后,我将此旋转的QR码绘制在纯白色矩形区域(图像中QR码两侧的区域)上。当我尝试使用ZXing Reader库读取旋转的QR码时,它不会解码图像(除非旋转是90的倍数)。我想这可能是由于旋转后图像失真造成的。当我放大旋转的图像时,图像中有非常明显的灰色部分,图像看起来有别名(具有阶梯效果)。有没有办法解决这种扭曲?

这是一个旋转到-80度的示例图像:enter image description here

以下是图像放大的方式:enter image description here

c# system.drawing
1个回答
0
投票

我想你只需要增加图像的分辨率。

由于QR码是矩形的,您可以放大原始图像,即使它非常小,然后旋转它。

© www.soinside.com 2019 - 2024. All rights reserved.