任意角度旋转Bitmap

        /// <summary>
        /// 任意角度旋转
        /// </summary>
        /// <param name="bmp">原始图Bitmap </param>
        /// <param name="angle">旋转角度 </param>
        /// <returns>输出Bitmap </returns>
        public static Bitmap BmpRotate(Bitmap bmp, double angle)
        {
            int w = bmp.Width;
            int h = bmp.Height;
            int mx = w / 2;
            int my = h / 2;
            int r = mx < my ? mx : my;
            double cos = Math.Cos(angle / 360 * (Math.PI * 2));
            double sin = Math.Sin(angle / 360 * (Math.PI * 2));
            Bitmap bitmap = new Bitmap(w, h);
            for (int x = 0; x < w; x += 1)
            {
                for (int y = 0; y < h; y += 1)
                {
                    if ((x - mx) * (x - mx) + (y - my) * (y - my) > r * r)
                        bitmap.SetPixel(x, y, bmp.GetPixel(x, y));
                    else
                    {
                        double m = (x - mx) * cos + (y - my) * sin;
                        double n = (y - my) * cos - (x - mx) * sin;
                        bitmap.SetPixel(x, y, bmp.GetPixel((int)m + mx, (int)n + my));
                    }
                }
            }
            return bitmap;
        }

在VB中可以使用图片控件(PictureBox)和 Graphics 类来实现图片旋转功能。 首先,在VB的窗体中添加一个图片控件(PictureBox),并将其命名为picRotate。 然后,在窗体的Load事件中,通过代码将图片加载到图片控件中: ``` Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load picRotate.Image = Image.FromFile("图片路径") End Sub ``` 接下来,使用Graphics对象来旋转图片。在按钮的Click事件中添加以下代码: ``` Private Sub btnRotate_Click(sender As Object, e As EventArgs) Handles btnRotate.Click Dim image As Image = picRotate.Image ' 获取图片控件中的图片 ' 创建一个空白的位图,并设置其尺寸与原图片相同 Dim bitmap As New Bitmap(image.Width, image.Height) ' 通过Graphics对象绘制旋转后的图片 Using g As Graphics = Graphics.FromImage(bitmap) g.TranslateTransform(image.Width / 2, image.Height / 2) ' 以图片中心点为坐标原点 g.RotateTransform(旋转角度) ' 设置旋转角度 g.TranslateTransform(-image.Width / 2, -image.Height / 2) ' 还原坐标原点 g.DrawImage(image, New Point(0, 0)) ' 绘制旋转后的图片 End Using ' 将旋转后的图片赋值给图片控件 picRotate.Image = bitmap End Sub ``` 注意,上述代码中的“旋转角度”需要替换为具体的角度值,可以是正值或负值,代表顺时针或逆时针旋转角度。 最后,按钮按钮控件(Button)来触发旋转图片的功能。用户点击该按钮后,图片将按照设置的角度进行旋转
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值