Picturebox旋转 [英] Picturebox rotation

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

问题描述

I have been trying for some time to rotate the image in a picturebox without success.

我查看了RotateFlip函数,但由于我需要按用户指定的度数旋转图像,所以它没用。经过大量的观察后,我在回复此论坛时遇到了以下代码.....

I have looked at the RotateFlip function but it is useless as I need to rotate the image by a user-specified number of degrees. After lots of looking I came across the following code in a reply to this forum.....

Private Sub Rotation(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        With e.Graphics
            .TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
            .RotateTransform(dgr)
            .DrawImage(PictureBox1.Image, (-PictureBox1.Width \ 2), (-PictureBox1.Height \ 2))
        End With    
End Sub

此代码几乎可以正常工作。我得到的问题是它留下原始图像,所以我最终看到原始图像的旋转版本。

This code almost works. The problem I get is that it leaves behind the original image so I end up seeing the original image with the rotated version on top of it.

我不知道为什么会发生这种情况,也不知道如何解决它。有人可以帮忙吗?

I have no idea why this happens, nor how to fix it. Can anyone help please?




推荐答案

这不应该发生在Paint方法 - Paint方法是渲染图像的地方,而不是操纵它。

This should not be happening in the Paint method - the Paint method is where you render the image, not manipulate it.

在这种情况下,你需要从原始图像创建一个新的(旋转的)图像,然后将其渲染油漆事件。 下面的示例使用图片框图像作为图像旋转的来源 - 您的来源可能不同。

In this case you need to create a new (rotated) image from the original, then render it in the paint event.  The example below uses the picture box image as the source for the image to rotate - your source might be different.

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim BMP As New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Dim g As Graphics = Graphics.FromImage(BMP)
        With g
            .TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
            .RotateTransform(dgr)
            .DrawImage(PictureBox1.Image, (-PictureBox1.Width \ 2), (-PictureBox1.Height \ 2))
        End With
        PictureBox1.Image = BMP
    End Sub


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

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