旋转后图像失真

时间:2019-04-12 08:31:52

标签: c# system.drawing

我正在使用以下代码旋转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阅读器库读取旋转的QR码时,它不会解码图像(除非旋转是90的倍数)。我认为这可能是由于旋转后图像变形所致。当我放大旋转的图像时,图像上有非常浅的可见灰色部分,并且图像看起来像是混叠的(具有阶梯效果)。有没有办法解决这种失真?

以下是旋转至-80度的示例图像: enter image description here

这是图像放大的样子: enter image description here

1 个答案:

答案 0 :(得分:0)

我猜您只需要提高图像的分辨率即可。

由于QR码是矩形的,因此即使原始图像很小,也可以将其放大,然后旋转它。

相关问题